UNPKG

surrogate

Version:

Object method hooks made easy

74 lines (66 loc) 3.4 kB
import {SurrogateProxy as $ed4ccce8673a8baf$export$98c317ab89d9c127} from "./surrogate.06e5809d.js"; import {MethodIdentifier as $5373dd1107ba7dc2$export$1f0bc0d8e047b444} from "./surrogate.d934c73c.js"; import {HookType as $96586206eab52612$export$b3a402ce9f993bc7} from "./surrogate.5d082a84.js"; import {wrapDefaults as $k8lmF$wrapDefaults} from "@status/defaults"; class $2d7781ab0a000147$export$192df6a0a4406daf { static{ this.decoratorMap = new Map(); } constructor(options){ this.options = options; } getPrototypeOf(target) { // Inserting the target into its own proxy's prototype chain lets // reflect-metadata (and similar libraries) find class-level metadata // that other decorators stored on the original class. The Proxy // invariant requires this to match Reflect.getPrototypeOf(target) when // the target is non-extensible, so fall back in that case — metadata // interop is lost, but the lookup will not throw. return Object.isExtensible(target) ? target : Reflect.getPrototypeOf(target); } construct(Klass, args, Target) { const { locateWith: locateWith = Klass } = this.options; const decoratorMap = $2d7781ab0a000147$export$192df6a0a4406daf.retrieveTargetDecoratorMap(locateWith); const wrappedInstance = (0, $ed4ccce8673a8baf$export$98c317ab89d9c127).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.185456ee.js.map