Throw error if lazy proxies get cyclical

This commit is contained in:
thepaperpilot 2023-04-27 20:41:20 -05:00
parent 36fa4ece65
commit 81058e10b4

View file

@ -36,8 +36,13 @@ export function createLazyProxy<T extends object, S extends T>(
): T { ): T {
const obj: S & Partial<T> = baseObject; const obj: S & Partial<T> = baseObject;
let calculated = false; let calculated = false;
let calculating = false;
function calculateObj(): T { function calculateObj(): T {
if (!calculated) { if (!calculated) {
if (calculating) {
throw new Error("Cyclical dependency detected. Cannot evaluate lazy proxy.");
}
calculating = true;
Object.assign(obj, objectFunc.call(obj, obj)); Object.assign(obj, objectFunc.call(obj, obj));
calculated = true; calculated = true;
} }