|
| 1 | +import { ITryCatchFinallyHook, TryCatchFinallyHook, DecoratorArgsOf, ITryCatchFinallyHooksCollection, IHooksFunc } from "./TryCatchFinallyHook"; |
| 2 | +import { FunctionContext, WrappableFunction, createTryCatchFinally } from "./tryCatchFinally"; |
| 3 | + |
| 4 | + |
| 5 | + /** @inheritDoc {@link ITryCatchFinallyHooksCollection} */ |
| 6 | +export class Hooks<FullHookContext extends FunctionContext = FunctionContext> implements ITryCatchFinallyHooksCollection<FullHookContext> |
| 7 | +{ |
| 8 | + private hooks:ITryCatchFinallyHook<any, any>[] = [] |
| 9 | + |
| 10 | + add<TNewContext extends {}={}, TryReturnContext={}>(hook: TryCatchFinallyHook<FullHookContext & TNewContext, TryReturnContext>): Hooks<FullHookContext & TNewContext & TryReturnContext> |
| 11 | + { |
| 12 | + if(hook instanceof Function) |
| 13 | + return this.add({onTry: hook} as any) |
| 14 | + |
| 15 | + this.hooks.push(hook); |
| 16 | + return this as any; |
| 17 | + } |
| 18 | + |
| 19 | + wrap<TFunc extends WrappableFunction>(args: DecoratorArgsOf<FullHookContext>|undefined, func:TFunc): TFunc |
| 20 | + wrap<TFunc extends WrappableFunction>(func:TFunc):TFunc |
| 21 | + //wrap(args?: DecoratorArgsOf<FullHookContext>): <TFunc extends WrappableFunction>(func:TFunc)=>TFunc // conflicts with decor |
| 22 | + wrap() |
| 23 | + { |
| 24 | + const _this = this |
| 25 | + const beforeHooksTry = this.beforeHooksTry.bind(this) |
| 26 | + const afterHooksTry = this.afterHooksTry?.bind(this) |
| 27 | + const [args, func] = arguments.length >= 2 |
| 28 | + ? [arguments[0] as DecoratorArgsOf<FullHookContext>|undefined, arguments[1] as WrappableFunction] |
| 29 | + : arguments[0] instanceof Function? [undefined, arguments[0] as WrappableFunction] |
| 30 | + : [arguments[0] as DecoratorArgsOf<FullHookContext>|undefined, undefined] |
| 31 | + |
| 32 | + if(!func) |
| 33 | + return (func: WrappableFunction)=> this.wrap(args, func) |
| 34 | + |
| 35 | + return createTryCatchFinally<typeof func, {args?: DecoratorArgsOf<FullHookContext>}>(func, { |
| 36 | + onTry(ctxTry) { |
| 37 | + if(args) (ctxTry as any).args = args |
| 38 | + const hooksSorted = _this.hooks |
| 39 | + const bht = beforeHooksTry(ctxTry as any) |
| 40 | + let onTryHooks = hooksSorted.map(hook=> hook.onTry(ctxTry)!).filter(h=>h) |
| 41 | + |
| 42 | + onTryHooks = onTryHooks.reduce( |
| 43 | + ([first,last], h)=>('lastInQueue' in h && h.lastInQueue? last.push(h): first.push(h), [first, last]), |
| 44 | + [[] as typeof onTryHooks,[] as typeof onTryHooks] |
| 45 | + ).flat() |
| 46 | + |
| 47 | + return { |
| 48 | + onFinally(ctxFinally) { |
| 49 | + for (const hr of onTryHooks) { |
| 50 | + hr.onFinally?.(ctxFinally) |
| 51 | + } |
| 52 | + bht.onFinally?.() |
| 53 | + }, |
| 54 | + onCatch(ctxCatch) { |
| 55 | + for (const hr of onTryHooks) { |
| 56 | + hr.onCatch?.(ctxCatch) |
| 57 | + } |
| 58 | + bht.onCatch?.() |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param args |
| 67 | + * @returns |
| 68 | + */ |
| 69 | + decor(args?: DecoratorArgsOf<FullHookContext>): MethodDecorator & (<TFunc extends WrappableFunction>(func:TFunc)=>TFunc) |
| 70 | + { |
| 71 | + return ((_target:any, _propertyKey, descriptor) => { |
| 72 | + if(arguments.length==1 && typeof _target === 'function') |
| 73 | + return this.wrap(args, _target as any) |
| 74 | + const originalMethod = descriptor.value as any; |
| 75 | + descriptor.value = this.wrap(args, originalMethod) |
| 76 | + }) as MethodDecorator as any |
| 77 | + } |
| 78 | + |
| 79 | + scope<TResult>(args: DecoratorArgsOf<FullHookContext>, scope:()=>TResult):TResult |
| 80 | + { |
| 81 | + return this.wrap(args, scope)() |
| 82 | + } |
| 83 | + |
| 84 | + protected beforeHooksTry(ctx: FullHookContext):{ |
| 85 | + onCatch?(): void |
| 86 | + onFinally?(): void |
| 87 | + } |
| 88 | + { |
| 89 | + const _this = this |
| 90 | + const prevContext = _this._currentContext |
| 91 | + this._currentContext = ctx as any |
| 92 | + |
| 93 | + return { |
| 94 | + onFinally() { |
| 95 | + _this._currentContext = prevContext |
| 96 | + }, |
| 97 | + } |
| 98 | + } |
| 99 | + protected afterHooksTry?(ctx: FullHookContext):{ |
| 100 | + onCatch?(): void |
| 101 | + onFinally?(): void |
| 102 | + } |
| 103 | + |
| 104 | + private _currentContext:(FullHookContext & FunctionContext)|undefined = undefined |
| 105 | + |
| 106 | + |
| 107 | + /** |
| 108 | + * Only safe to use in sync functions or in async functions before any awaited code. |
| 109 | + * Otherwise, the context may be changed by another async function - in that case use callStack hook instead |
| 110 | + */ |
| 111 | + get context(){ |
| 112 | + return this._currentContext |
| 113 | + } |
| 114 | + |
| 115 | + create():IHooksFunc<this> |
| 116 | + { |
| 117 | + const hookFunc:IHooksFunc<Hooks<FullHookContext>> = function() |
| 118 | + { |
| 119 | + const hooks = hookFunc.hooks |
| 120 | + |
| 121 | + if(arguments[0] instanceof Function || arguments[1] instanceof Function) |
| 122 | + return hooks.wrap.apply(hooks, arguments as any) |
| 123 | + //decor or wrap |
| 124 | + return hooks.decor.apply(hooks, arguments as any) |
| 125 | + } as any |
| 126 | + hookFunc.hooks = this |
| 127 | + hookFunc.scope = this.scope.bind(this) |
| 128 | + Object.defineProperty(hookFunc, 'context', {get: ()=>hookFunc.hooks.context}) |
| 129 | + return hookFunc as any //IHooksFunc<Hooks<FullHookContext>> |
| 130 | + } |
| 131 | +} |
0 commit comments