11import { FunctionContext , createTryCatchFinally , FunctionInterceptors } from "./tryCatchFinally" ;
22
3- export interface ITryCatchFinallyHook < TContext = { } > {
4- onTry ( ctx : FunctionContext & TContext ) : void | {
5- onCatch ?( ) : void
6- onFinally ?( ) : void
3+ export interface ITryCatchFinallyHook < TParentContext extends FunctionContext = FunctionContext , TrtReturnContext = { } > {
4+ onTry ( ctx : TParentContext ) : void | {
5+ context ?: TrtReturnContext
6+ onCatch ?( ctx :TParentContext & TrtReturnContext ) : void
7+ onFinally ?( ctx :TParentContext & TrtReturnContext ) : void
78 /**
89 * If true, the hook will be executed after all other hooks
910 */
1011 lastInQueue ?: boolean
1112 }
1213}
1314
14- export type HookContextOf < THook > = THook extends ITryCatchFinallyHook < infer T > ? T : never
15+ export type HookContextOf < THook > = THook extends ITryCatchFinallyHook < infer T1 , infer T2 > ? T1 & T2 : never
1516
1617export type DecoratorArgsOf < THookContext > = THookContext extends { args ?: infer TArgs extends { } } ? TArgs : { }
1718
18- export class TryCatchFinallyHooksBuilder < THookContext extends { } , TDecoratorArgs extends { } = DecoratorArgsOf < THookContext > > //implements FunctionInterceptors
19+ export class TryCatchFinallyHooksBuilder < FullHookContext extends FunctionContext = FunctionContext >
1920{
20- // onTry():{
21- // context?: {} | undefined;
22- // onCatch?: ((ctx: FunctionContext<(...args: any[]) => any>) => void) | undefined;
23- // onFinally?: ((ctx: FunctionContext<(...args: any[]) => any>) => void) | undefined;
24- // }
25- // {
26- // const ctx = {}
27- // const hooksRes = this.forEachHook(hook=> hook.onTry(ctx))
28- // for (const hookRes of [...hooksRes]) {
29- // if(hookRes && hookRes.lastInQueue){
30- // const [itemToMove] =hooksRes.splice(hooksRes.indexOf(hookRes), 1)
31- // hooksRes.push(itemToMove)
32- // }
33- // }
34-
35- // return {
36- // onFinally(ctx) {
37- // for (const hr of hooksRes) {
38- // if(hr && hr.onFinally) hr.onFinally()
39- // }
40- // },
41- // onCatch(ctx) {
42- // for (const hr of hooksRes) {
43- // if(hr && hr.onCatch) hr.onCatch()
44- // }
45- // }
46- // }
47- // }
21+ private hooks :ITryCatchFinallyHook [ ] = [ ]
4822
49- private hooks :ITryCatchFinallyHook < any > [ ] = [ ]
50-
51- add < NewHookContext , NewArgs = DecoratorArgsOf < NewHookContext > > ( hook : ITryCatchFinallyHook < THookContext & NewHookContext > )
52- : TryCatchFinallyHooksBuilder < THookContext & NewHookContext , TDecoratorArgs & NewArgs >
23+ add < THookContext extends { } , TryReturnContext extends { } = { } > ( hook : ITryCatchFinallyHook < FullHookContext & THookContext , TryReturnContext > )
24+ : TryCatchFinallyHooksBuilder < THookContext & FullHookContext & TryReturnContext >
5325 {
5426 this . hooks . push ( hook ) ;
5527 return this as any ;
5628 }
5729
58- static createHook < HookContext > ( onTry : ITryCatchFinallyHook < HookContext > [ 'onTry' ] ) : ITryCatchFinallyHook < HookContext >
30+ static createHook < TryHookContext , TryReturnContext = { } > ( onTry : ITryCatchFinallyHook < TryHookContext & FunctionContext , TryReturnContext > [ 'onTry' ] ) : ITryCatchFinallyHook < TryHookContext & FunctionContext , TryReturnContext >
5931 {
6032 return {
6133 onTry
62- }
34+ } as any
6335 }
6436
65- createAndAdd < NewHookContext , NewArgs = NewHookContext extends { args ?: infer TNewArgs } ? TNewArgs : { } > ( onTry :ITryCatchFinallyHook < THookContext & NewHookContext > [ 'onTry' ] ) : TryCatchFinallyHooksBuilder < THookContext & NewHookContext , TDecoratorArgs & NewArgs >
37+ createAndAdd < TryReturnContext = { } > ( onTry :ITryCatchFinallyHook < FullHookContext , TryReturnContext > [ 'onTry' ] ) : TryCatchFinallyHooksBuilder < FullHookContext & TryReturnContext >
6638 {
67- this . add ( TryCatchFinallyHooksBuilder . createHook ( onTry ) )
39+ this . add ( TryCatchFinallyHooksBuilder . createHook ( onTry ) as any )
6840 return this as any
6941 }
7042
71- asFunctionWrapper ( args ?: TDecoratorArgs ) : < TFunc extends ( ...args :any [ ] ) => any > ( func :TFunc ) => TFunc
43+ asFunctionWrapper ( args ?: DecoratorArgsOf < FullHookContext > ) : < TFunc extends ( ...args :any [ ] ) => any > ( func :TFunc ) => TFunc
7244 {
7345 const _this = this
7446 const beforeHooksTry = this . beforeHooksTry . bind ( this )
7547 const afterHooksTry = this . afterHooksTry ?. bind ( this )
76- type TContext = THookContext & FunctionContext
48+ type TContext = FullHookContext & FunctionContext
7749 return ( func :any ) => {
7850 return createTryCatchFinally ( func , {
7951 onTry ( funcCtx ) {
@@ -90,15 +62,15 @@ export class TryCatchFinallyHooksBuilder<THookContext extends {}, TDecoratorArgs
9062 }
9163
9264 return {
93- onFinally ( ) {
65+ onFinally ( _ctx ) {
9466 for ( const hr of hooksRes ) {
95- if ( hr && hr . onFinally ) hr . onFinally ( )
67+ if ( hr && hr . onFinally ) hr . onFinally ( _ctx )
9668 }
9769 bht . onFinally ?.( )
9870 } ,
99- onCatch ( ) {
71+ onCatch ( _ctx ) {
10072 for ( const hr of hooksRes ) {
101- if ( hr && hr . onCatch ) hr . onCatch ( )
73+ if ( hr && hr . onCatch ) hr . onCatch ( _ctx )
10274 }
10375 bht . onCatch ?.( )
10476 }
@@ -108,18 +80,18 @@ export class TryCatchFinallyHooksBuilder<THookContext extends {}, TDecoratorArgs
10880 }
10981 }
11082
111- asDecorator ( args ?: TDecoratorArgs ) : MethodDecorator
83+ asDecorator ( args ?: DecoratorArgsOf < FullHookContext > ) : MethodDecorator
11284 {
11385 return ( target :any , propertyKey , descriptor ) => {
11486 descriptor . value = this . asFunctionWrapper ( args ) ( descriptor . value as any )
11587 }
11688 }
117- createDecorator ( ) : ( args :TDecoratorArgs ) => MethodDecorator
89+ createDecorator ( ) : ( args :DecoratorArgsOf < FullHookContext > ) => MethodDecorator
11890 {
11991 return args => this . asDecorator ( args )
12092 }
12193
122- protected beforeHooksTry ( ctx : THookContext ) :{
94+ protected beforeHooksTry ( ctx : FullHookContext ) :{
12395 onCatch ?( ) : void
12496 onFinally ?( ) : void
12597 }
@@ -134,12 +106,12 @@ export class TryCatchFinallyHooksBuilder<THookContext extends {}, TDecoratorArgs
134106 } ,
135107 }
136108 }
137- protected afterHooksTry ?( ctx : THookContext ) :{
109+ protected afterHooksTry ?( ctx : FullHookContext ) :{
138110 onCatch ?( ) : void
139111 onFinally ?( ) : void
140112 }
141113
142- asScope < TResult > ( args : TDecoratorArgs , scope :( ) => TResult ) :TResult
114+ asScope < TResult > ( args : DecoratorArgsOf < FullHookContext > , scope :( ) => TResult ) :TResult
143115 {
144116 return this . asFunctionWrapper ( args ) ( scope ) ( )
145117 }
@@ -149,11 +121,12 @@ export class TryCatchFinallyHooksBuilder<THookContext extends {}, TDecoratorArgs
149121 return this . hooks . map ( h => fn ( h ) ) ;
150122 }
151123
152- private _currentContext :( THookContext & FunctionContext ) | undefined = undefined
124+ private _currentContext :( FullHookContext & FunctionContext ) | undefined = undefined
153125 get current ( ) {
154126 return this . _currentContext
155127 }
156128
157129}
158130
159- export type ContextOf < TTryCatchFinallyHooksBuilder extends TryCatchFinallyHooksBuilder < any > > = TTryCatchFinallyHooksBuilder extends TryCatchFinallyHooksBuilder < infer T > ? T & FunctionContext : never
131+ export type ContextOf < TTryCatchFinallyHooksBuilder extends TryCatchFinallyHooksBuilder < any > >
132+ = TTryCatchFinallyHooksBuilder extends TryCatchFinallyHooksBuilder < infer T1 > ? T1 : never
0 commit comments