UNPKG

surrogate

Version:

Object method hooks made easy

147 lines (132 loc) 5.23 kB
var $bc83b7d30080d617$exports = require("./surrogate.dd8f7a43.js"); var $d77730d1baea24ca$exports = require("./surrogate.d7db5f28.js"); var $5da807cc202126ea$exports = require("./surrogate.68dbcc9f.js"); var $173529f42333421b$exports = require("./surrogate.014ad084.js"); var $69e6bb98e7bc51ac$exports = require("./surrogate.5de640eb.js"); var $d87b5a514cc0e7ec$exports = require("./surrogate.7ec9dd6c.js"); var $fc578d3576b0d8ef$exports = require("./surrogate.80a1d055.js"); var $dxCXC$jfrazxasarray = require("@jfrazx/asarray"); var $dxCXC$uuid = require("uuid"); function $parcel$export(e, n, v, s) { Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); } $parcel$export(module.exports, "ExecutionContext", () => $317e915756ffda0f$export$f7c638676c8af91d); class $317e915756ffda0f$export$f7c638676c8af91d { constructor(context){ this.context = context; this.utilizeLatest = false; this.hasThrown = false; this.timeTracker = (0, $bc83b7d30080d617$exports.TimeTrackable).fetch(); this.correlationId = (0, $dxCXC$uuid.v4)(); } static for(context, typeContainers) { const hasAsync = this.hasAsync(typeContainers); const TargetContext = hasAsync || (0, $69e6bb98e7bc51ac$exports.isAsync)(context.original) ? $317e915756ffda0f$export$354c9ee217516037 : $317e915756ffda0f$export$a9787f5a4efadfdd; return new TargetContext(context); } setupPipeline(proxy, typeContainers) { const which = [ (0, $173529f42333421b$exports.HookType).PRE, (0, $173529f42333421b$exports.HookType).POST ]; which.forEach((type)=>{ const containers = [ ...typeContainers[type] ]; containers.sort((0, $d87b5a514cc0e7ec$exports.containerSorter)).forEach((container)=>new (0, $d77730d1baea24ca$exports.Next)(this, proxy, container, type)); (0, $fc578d3576b0d8ef$exports.Tail).for(this, proxy, type); }); return this; } static hasAsync(typeContainers) { return Object.values(typeContainers).flat().some(({ handler: handler, options: options })=>(0, $69e6bb98e7bc51ac$exports.isAsync)(handler) || options.wrapper === (0, $5da807cc202126ea$exports.MethodWrapper).Async); } setReturnValue(value) { this.returnValue ??= value; } setNext(next) { this.nextNode = next; } addNext(next) { this.nextNode ? this.nextNode.addNext(next) : this.setNext(next); return this; } updateLatestArgs(updatedArgs) { this.utilizeLatest = true; this.latestArgs = (0, $dxCXC$jfrazxasarray.asArray)(updatedArgs); } get currentArgs() { return this.utilizeLatest ? this.latestArgs : this.context.originalArguments; } runNext(next) { const node = next ?? this.nextNode; this.setNext(node.nextNode); return node.handleNext(); } logError(node, error) { const { silenceErrors: silenceErrors } = node.container.options; const silence = (0, $69e6bb98e7bc51ac$exports.isFunction)(silenceErrors) ? silenceErrors(error) : silenceErrors; if (!silence && !this.hasThrown) { this.hasThrown = true; console.error(`SurrogateError: ${error?.message ? error.message : JSON.stringify(error)}`); } } } class $317e915756ffda0f$export$354c9ee217516037 extends $317e915756ffda0f$export$f7c638676c8af91d { async start() { return new Promise((resolve, reject)=>{ this.resolver = resolve; this.rejecter = reject; try { this.runNext(this.nextNode); } catch (error) { this.handleError(this.nextNode, error); } }); } complete() { this.resolver(this.returnValue); } async runOriginal(node) { const { container: container, context: context } = node; const handler = container.getHandler(context); try { const result = await handler.apply(context.receiver, this.currentArgs); this.setReturnValue(result); this.runNext(node.nextNode); } catch (error) { this.handleError(this.nextNode, error); } } handleError(node, error) { this.logError(node, error); this.rejecter(error); } bail(bailWith) { this.resolver(bailWith ?? this.returnValue); } } class $317e915756ffda0f$export$a9787f5a4efadfdd extends $317e915756ffda0f$export$f7c638676c8af91d { start() { this.runNext(); return this.complete(); } complete() { return this.returnValue; } runOriginal(node) { const { container: container, context: context } = node; const handler = container.getHandler(context); const result = handler.apply(context.receiver, this.currentArgs); this.setReturnValue(result); this.runNext(node.nextNode); } bail(bailWith) { this.setReturnValue(bailWith); } handleError(node, error) { this.logError(node, error); throw error; } } //# sourceMappingURL=surrogate.c7b3fd50.js.map