diff --git a/src/declare.ts b/src/declare.ts index 53d6a13..f16f49c 100644 --- a/src/declare.ts +++ b/src/declare.ts @@ -259,3 +259,12 @@ export interface IHookOptions { // Whether to wait for the hook (if the return value of the hook is a promise) await?: boolean; } + +export interface IAroundHookOptions { + /** + * @deprecated AroundHook will always await the promise, it act as the union model. + * + * Whether to wait for the hook (if the return value of the hook is a promise) + */ + await?: boolean; +} diff --git a/src/decorator.ts b/src/decorator.ts index 64861b1..a48da19 100644 --- a/src/decorator.ts +++ b/src/decorator.ts @@ -12,6 +12,7 @@ import { IHookOptions, IAfterReturningAspectHookFunction, IAfterThrowingAspectHookFunction, + IAroundHookOptions, } from './declare'; import { markAsAspect, markAsHook } from './helper'; @@ -239,7 +240,7 @@ export function After( export function Around( token: Token, method: MethodName, - options: IHookOptions = {}, + options: IAroundHookOptions = {}, ) { return >, K extends MethodName>( target: T, diff --git a/src/helper/compose.ts b/src/helper/compose.ts index 00e3e1c..ddaf1b4 100644 --- a/src/helper/compose.ts +++ b/src/helper/compose.ts @@ -16,7 +16,6 @@ export interface Composed { export interface Middleware { (ctx: Context): Promise | void; - awaitPromise?: boolean; } function dispatch( @@ -36,14 +35,10 @@ function dispatch( if (idx < middlewareList.length) { const middleware = middlewareList[idx]; - const t = middleware({ + maybePromise = middleware({ ...ctx, proceed: () => dispatch(middlewareList, idx + 1, stack, ctx), } as Context); - - if (middleware.awaitPromise) { - maybePromise = Promise.resolve(t); - } } else if (ctx.proceed) { maybePromise = ctx.proceed(); } diff --git a/src/helper/hook-helper.ts b/src/helper/hook-helper.ts index c01ed34..94cbcb5 100644 --- a/src/helper/hook-helper.ts +++ b/src/helper/hook-helper.ts @@ -146,9 +146,6 @@ export function createHookedFunction( function runAroundHooks(): Promise | void { const hooks = aroundHooks.map((v) => { const fn = v.hook as Middleware>; - if (v.awaitPromise) { - fn.awaitPromise = true; - } return fn; }); const composed = compose>(hooks); @@ -396,7 +393,6 @@ function runOneHook< P extends IJoinPoint, >(hook: T, joinPoint: P, promise: Promise | undefined): Promise | undefined { if (hook.awaitPromise) { - // 如果要求await hook,如果之前有promise,直接用,不然创建Promise给下一个使用 promise = promise || Promise.resolve(); promise = promise.then(() => { return hook.hook(joinPoint); diff --git a/test/helper/compose.test.ts b/test/helper/compose.test.ts index 03076cc..559d2ef 100644 --- a/test/helper/compose.test.ts +++ b/test/helper/compose.test.ts @@ -17,7 +17,6 @@ describe('di compose', () => { console.log(`middleware1 result: ${result}`); console.log(`middleware1 after: ${name}`); }; - middleware1.awaitPromise = true; const middleware2: Middleware = async (ctx) => { const name = ctx.getName(); @@ -28,7 +27,6 @@ describe('di compose', () => { console.log(`middleware2 result: ${result}`); console.log(`middleware2 after: ${name}`); }; - middleware2.awaitPromise = true; const all = compose([middleware1, middleware2]); let ret = undefined as any; const result = all({ @@ -86,7 +84,7 @@ describe('di compose', () => { return ret; }, }); - expect(result).toBeFalsy(); + expect(result).toBeInstanceOf(Promise); }); it('will throw error when call proceed twice', async () => { interface ExampleContext { diff --git a/test/injector.test.ts b/test/injector.test.ts index bf6757d..c533e25 100644 --- a/test/injector.test.ts +++ b/test/injector.test.ts @@ -544,7 +544,6 @@ describe('test injector work', () => { // Async hook on async target injector.createHooks([ { - awaitPromise: true, hook: async (joinPoint) => { joinPoint.proceed(); const result = await joinPoint.getResult();