UNPKG

surrogate

Version:

Object method hooks made easy

79 lines (69 loc) 3.46 kB
var $4bb808266ae73c8a$exports = require("./surrogate.e6214bab.js"); var $dc3c4ef945810111$exports = require("./surrogate.f2c14ea0.js"); var $173529f42333421b$exports = require("./surrogate.014ad084.js"); var $f7reW$statusdefaults = require("@status/defaults"); function $parcel$export(e, n, v, s) { Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); } $parcel$export(module.exports, "SurrogateClassWrapper", () => $8f5bd6cc0f9bb93b$export$192df6a0a4406daf); class $8f5bd6cc0f9bb93b$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 = $8f5bd6cc0f9bb93b$export$192df6a0a4406daf.retrieveTargetDecoratorMap(locateWith); const wrappedInstance = (0, $4bb808266ae73c8a$exports.SurrogateProxy).wrap(Reflect.construct(Klass, args, Target), this.options); const eventManager = wrappedInstance.getSurrogate(); const identifier = new (0, $dc3c4ef945810111$exports.MethodIdentifier)(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 $8f5bd6cc0f9bb93b$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, $f7reW$statusdefaults.wrapDefaults)({ defaultValue: { [(0, $173529f42333421b$exports.HookType).PRE]: [], [(0, $173529f42333421b$exports.HookType).POST]: [] }, setUndefined: true, shallowCopy: false }); } } //# sourceMappingURL=surrogate.1545c2f2.js.map