overmind
Version:
Frictionless state management
39 lines (38 loc) • 1.31 kB
JavaScript
//#region src/proxyfyEffects.ts
function isObject(value) {
return typeof value === "object" && !Array.isArray(value) && value !== null;
}
let hasWarnedConstructor = false;
let currentEffectId = 0;
const ORIGIN_TARGET = Symbol("ORIGIN_TARGET");
function proxifyEffects(effects, cb, path = "") {
if (!isObject(effects) && !(typeof effects === "function")) return effects;
return new Proxy(effects, {
apply(target, thisArg, agumentsList) {
const effectId = currentEffectId++;
const name = path.split(".");
const method = name.pop();
return cb({
func: target.bind(thisArg ? thisArg[ORIGIN_TARGET] : void 0),
effectId,
name: name.join("."),
method,
args: agumentsList
});
},
construct(Target, args) {
if (!hasWarnedConstructor) {
console.warn(`EFFECTS - It is highly recommended to create a custom effect, exposing a method that deals with the instantiation of "${path}". It improves readability and debugability of your app`);
hasWarnedConstructor = true;
}
return new Target(...args);
},
get(target, prop) {
if (prop === ORIGIN_TARGET) return target;
return proxifyEffects(target[prop], cb, path ? path + "." + prop.toString() : prop.toString());
}
});
}
//#endregion
export { proxifyEffects };
//# sourceMappingURL=proxyfyEffects.js.map