'use strict'; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; class Hook { constructor() { /** @internal */ __publicField(this, "_interceptor", []); /** @internal */ __publicField(this, "_taps", []); } /** @internal */ _wrap(data) { const { fn } = data; const interceptorHook = (...args) => this._interceptor?.forEach((item) => item?.call?.call(null, ...args)); const wrappedData = { ...data, fn: function(...args) { if (wrappedData.once) { wrappedData._frozen = true; } interceptorHook?.(...args); return fn(...args); }.bind(fn) }; return wrappedData; } /** @internal */ _tap(data) { data = this._wrap(data); this._interceptor?.forEach((item) => item?.register?.call(null, data)); this._interceptor?.forEach((item) => item?.tap?.call(null, data)); const isTargetTap = (item) => { if (data.before) { if (Array.isArray(data.before) ? data.before.includes(item.name) : data.before === item.name) { data._before_tap = item; data._fixed = true; return true; } } if (item._fixed) { return false; } return data.stage < item.stage; }; const index = this._taps.findIndex(isTargetTap); switch (index) { case -1: this._taps.push(data); break; case 0: this._taps.unshift(data); break; default: this._taps.splice(Math.max(0, index), 0, data); } } get taps() { return this._taps.filter((item) => !item._frozen); } tap(name, fn) { if (typeof name === "string") { this._tap({ stage: 0, name, fn }); return; } this._tap({ stage: 0, ...name, fn }); } intercept(options = {}) { this._interceptor.push({ ...options }); } } class SyncHook extends Hook { dispatch(...args) { return this.taps.map(({ fn }) => fn(...args)); } } class SyncBailHook extends Hook { dispatch(...args) { for (const tap of this.taps) { const result = tap.fn(...args); if (result !== void 0) { break; } } } } class SyncLoopHook extends Hook { dispatch(...args) { const results = []; for (const { fn } of this.taps) { const ret = fn(...args); if (ret !== void 0) { return this.dispatch(...args); } results.push(ret); } return results; } } function waterfall$1(items) { const next = (prevResult, index = 0) => { const fn = items[index]; if (index >= items.length) { return prevResult; } if (!fn || typeof fn !== "function") { throw new Error(`Unexcepted type of args[${index}]: ${typeof fn}.`); } const result = fn(prevResult); return next(result, index + 1); }; return (arg) => next(arg); } class SyncWaterfallHook extends Hook { dispatch(arg0) { const taps = this.taps.map(({ fn }) => fn); return waterfall$1(taps)(arg0); } } class AsyncParallelHook extends Hook { async dispatch(...args) { return Promise.all(this.taps.map(({ fn }) => fn(...args))); } } class AsyncParallelBailHook extends Hook { async dispatch(...args) { return Promise.race( this.taps.map( ({ fn }) => new Promise((resolve) => { return Promise.resolve(fn(...args)).then((result) => { if (result !== void 0) { return resolve(); } }); }) ) ); } } class AsyncSeriesHook extends Hook { async dispatch(...args) { const returns = []; for (const { fn } of this.taps) { returns.push(await fn(...args)); } return returns; } } class AsyncSeriesBailHook extends Hook { async dispatch(...args) { for (const tap of this.taps) { const result = await tap.fn(...args); if (result !== void 0) { break; } } } } class AsyncSeriesLoopHook extends Hook { async dispatch(...args) { const results = []; for (const { fn } of this.taps) { const ret = await fn(...args); if (ret !== void 0) { return this.dispatch(...args); } results.push(ret); } return results; } } function waterfall(items) { const next = async (prevResult, index = 0) => { const fn = items[index]; if (index >= items.length) { return prevResult; } if (!fn || typeof fn !== "function") { throw new Error(`Unexcepted type of args[${index}]: ${typeof fn}.`); } const result = await fn(prevResult); return next(result, index + 1); }; return (arg) => next(arg); } class AsyncSeriesWaterfallHook extends Hook { async dispatch(arg0) { const taps = this.taps.map(({ fn }) => fn); return waterfall(taps)(arg0); } } exports.AsyncHook = Hook; exports.AsyncParallelBailHook = AsyncParallelBailHook; exports.AsyncParallelHook = AsyncParallelHook; exports.AsyncSeriesBailHook = AsyncSeriesBailHook; exports.AsyncSeriesHook = AsyncSeriesHook; exports.AsyncSeriesLoopHook = AsyncSeriesLoopHook; exports.AsyncSeriesWaterfallHook = AsyncSeriesWaterfallHook; exports.Hook = Hook; exports.SyncBailHook = SyncBailHook; exports.SyncHook = SyncHook; exports.SyncLoopHook = SyncLoopHook; exports.SyncWaterfallHook = SyncWaterfallHook;