UNPKG

surrogate

Version:

Object method hooks made easy

65 lines (57 loc) 2.8 kB
import {MethodIdentifier as $5373dd1107ba7dc2$export$1f0bc0d8e047b444} from "./surrogate.d934c73c.js"; import {HookType as $96586206eab52612$export$b3a402ce9f993bc7} from "./surrogate.5d082a84.js"; import {SurrogateProxy as $k8lmF$SurrogateProxy} from "./surrogate.7186bc9b.js"; import {wrapDefaults as $k8lmF$wrapDefaults} from "@status/defaults"; class $2d7781ab0a000147$export$192df6a0a4406daf { static{ this.decoratorMap = new Map(); } constructor(options){ this.options = options; } construct(Klass, args, Target) { const { locateWith: locateWith = Klass } = this.options; const decoratorMap = $2d7781ab0a000147$export$192df6a0a4406daf.retrieveTargetDecoratorMap(locateWith); const wrappedInstance = (0, $k8lmF$SurrogateProxy).wrap(Reflect.construct(Klass, args, Target), this.options); const eventManager = wrappedInstance.getSurrogate(); const identifier = new (0, $5373dd1107ba7dc2$export$1f0bc0d8e047b444)(wrappedInstance); this.applyDecorators(eventManager, decoratorMap, identifier); return wrappedInstance; } applyDecorators(eventManager, decoratorMap, identifier) { const methods = identifier.instanceMethodNames(); Object.entries(decoratorMap).forEach(([action, options])=>{ const events = identifier.getApplicableMethods(action, methods); Object.entries(options).forEach(([which, handlerOptions])=>{ events.forEach((event)=>{ this.registerHandlers(eventManager, handlerOptions, which, event); }); }); }); } registerHandlers(eventManager, handlerOptions, hook, event) { handlerOptions.forEach(({ handler: handler, options: options })=>{ eventManager.registerHook(event, hook, handler, options); }); } static wrap(klass, options) { return new Proxy(klass, new $2d7781ab0a000147$export$192df6a0a4406daf(options)); } static addDecorators(klass, type, event, surrogateDecoratorOptions) { const decoratorMap = this.retrieveTargetDecoratorMap(klass); decoratorMap[event][type].push(...surrogateDecoratorOptions); return this.decoratorMap.set(klass, decoratorMap); } static retrieveTargetDecoratorMap(klass) { return this.decoratorMap.get(klass) ?? (0, $k8lmF$wrapDefaults)({ defaultValue: { [(0, $96586206eab52612$export$b3a402ce9f993bc7).PRE]: [], [(0, $96586206eab52612$export$b3a402ce9f993bc7).POST]: [] }, setUndefined: true, shallowCopy: false }); } } export {$2d7781ab0a000147$export$192df6a0a4406daf as SurrogateClassWrapper}; //# sourceMappingURL=surrogate.2bacf83e.js.map