surrogate
Version:
Object method hooks made easy
65 lines (56 loc) • 2.78 kB
JavaScript
import {FetchRuleRunner as $8Z7PJ$FetchRuleRunner, InternalMethods as $8Z7PJ$InternalMethods} from "./surrogate.47ecf467.js";
import {ExecutionContext as $8Z7PJ$ExecutionContext} from "./surrogate.1447e218.js";
import {EventManager as $6ff15ac1913fdb95$export$cc74486daaaef499} from "./surrogate.1b0659b6.js";
import {isFunction as $8c13fb1fbdc14b86$export$f6e2535fb5126e54} from "./surrogate.68fc8564.js";
import {Context as $b996795923f57ee4$export$841858b892ce1f4c} from "./surrogate.60aff1dc.js";
class $ed4ccce8673a8baf$export$98c317ab89d9c127 {
constructor(target, { useSingleton: useSingleton = true, ...globalOptions } = {}){
this.targets = new WeakMap();
return this.useInstance(useSingleton).setTarget(target, globalOptions);
}
get(target, event, receiver) {
return (0, $8Z7PJ$FetchRuleRunner).fetchRule(this, target, event, receiver).returnableValue();
}
getEventManager(target) {
return this.targets.get(target);
}
static wrap(object, options) {
return this.hasTarget(object) ? object : new Proxy(object, new $ed4ccce8673a8baf$export$98c317ab89d9c127(object, options));
}
static hasTarget(target) {
const methods = [
(0, $8Z7PJ$InternalMethods).Dispose,
(0, $8Z7PJ$InternalMethods).EventManager
];
return methods.every((key)=>(0, $8c13fb1fbdc14b86$export$f6e2535fb5126e54)(target[key]));
}
bindHandler(event, target, receiver) {
const func = Reflect.get(target, event, receiver);
return ({
[event]: (...args)=>this.surrogateHandler(new (0, $b996795923f57ee4$export$841858b892ce1f4c)(target, receiver, event, func, args))
})[event];
}
surrogateHandler(context) {
const { target: target, event: event } = context;
const typeContainers = this.getEventManager(target).getEventHandlers(event);
return (0, $8Z7PJ$ExecutionContext).for(context, typeContainers).setupPipeline(this, typeContainers).start();
}
dispose(target) {
this.getEventManager(target).deregisterHooks();
this.targets.delete(target);
return target;
}
setTarget(target, globalOptions) {
if (!this.targets.has(target)) this.targets.set(target, new (0, $6ff15ac1913fdb95$export$cc74486daaaef499)(globalOptions));
return this;
}
useInstance(useSingleton) {
if (useSingleton) {
if ($ed4ccce8673a8baf$export$98c317ab89d9c127.instance) return $ed4ccce8673a8baf$export$98c317ab89d9c127.instance;
$ed4ccce8673a8baf$export$98c317ab89d9c127.instance = this;
}
return this;
}
}
export {$ed4ccce8673a8baf$export$98c317ab89d9c127 as SurrogateProxy};
//# sourceMappingURL=surrogate.06e5809d.js.map