UNPKG

surrogate

Version:

Object method hooks made easy

86 lines (78 loc) 2.85 kB
import {WithArgsRule as $637Qo$WithArgsRule, WithoutArgsRule as $637Qo$WithoutArgsRule} from "./surrogate.3448e8fd.js"; import {MethodWrapper as $66c0dea2f60cbf63$export$9255060c057a2446} from "./surrogate.a243fd60.js"; import {NextProvider as $637Qo$NextProvider} from "./surrogate.7156328e.js"; import {asArray as $637Qo$asArray} from "@jfrazx/asarray"; class $3759c83d5e65e080$export$60ce066c927074a9 { constructor(node){ this.node = node; this.argsHandlers = [ (0, $637Qo$WithArgsRule), (0, $637Qo$WithoutArgsRule) ]; } static for(node, options) { return options.wrapper === (0, $66c0dea2f60cbf63$export$9255060c057a2446).Async ? new $3759c83d5e65e080$var$AsyncHandlerRunner(node) : new $3759c83d5e65e080$var$SyncHandlerRunner(node); } run(args, error) { const nextProvider = new (0, $637Qo$NextProvider)(this.node, args, error); const { timeTracker: timeTracker } = this.node.controller; const runner = this.shouldRunWithNext() ? this.runWithNext : this.runWithoutNext; timeTracker.setHookStart(); runner.call(this, nextProvider); } findRule() { return this.argsHandlers.map((Rule)=>new Rule(this.node)).find((rule)=>rule.shouldHandle()); } shouldRunWithNext() { const { useNext: useNext, noArgs: noArgs } = this.node.container.options; return useNext && !noArgs; } } class $3759c83d5e65e080$var$AsyncHandlerRunner extends $3759c83d5e65e080$export$60ce066c927074a9 { async runWithoutNext(nextProvider) { try { const result = await this.findRule().run(nextProvider); this.node.next({ using: (0, $637Qo$asArray)(result) }); } catch (error) { this.node.next({ error: error }); } } async runWithNext(nextProvider) { try { await this.findRule().run(nextProvider); } catch (error) { this.node.next({ error: error }); } } } class $3759c83d5e65e080$var$SyncHandlerRunner extends $3759c83d5e65e080$export$60ce066c927074a9 { runWithoutNext(nextProvider) { try { const result = this.findRule().run(nextProvider); this.node.next({ using: (0, $637Qo$asArray)(result) }); } catch (error) { this.node.next({ error: error }); } } runWithNext(nextProvider) { try { this.findRule().run(nextProvider); } catch (error) { this.node.next({ error: error }); } } } export {$3759c83d5e65e080$export$60ce066c927074a9 as HandlerRunner}; //# sourceMappingURL=surrogate.432c697b.js.map