Skip to content

Commit

Permalink
feat: around hooks deprecate await option
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Sep 8, 2023
1 parent 5df4033 commit 4c7c9f0
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/declare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IHookOptions,
IAfterReturningAspectHookFunction,
IAfterThrowingAspectHookFunction,
IAroundHookOptions,
} from './declare';
import { markAsAspect, markAsHook } from './helper';

Expand Down Expand Up @@ -239,7 +240,7 @@ export function After<ThisType = any, Args extends any[] = any, Result = any>(
export function Around<ThisType = any, Args extends any[] = any, Result = any>(
token: Token,
method: MethodName,
options: IHookOptions = {},
options: IAroundHookOptions = {},
) {
return <T extends Record<K, IAroundAspectHookFunction<ThisType, Args, Result>>, K extends MethodName>(
target: T,
Expand Down
7 changes: 1 addition & 6 deletions src/helper/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export interface Composed<C> {

export interface Middleware<C> {
(ctx: Context<C>): Promise<void> | void;
awaitPromise?: boolean;
}

function dispatch<C>(
Expand All @@ -36,14 +35,10 @@ function dispatch<C>(
if (idx < middlewareList.length) {
const middleware = middlewareList[idx];

const t = middleware({
maybePromise = middleware({
...ctx,
proceed: () => dispatch(middlewareList, idx + 1, stack, ctx),
} as Context<C>);

if (middleware.awaitPromise) {
maybePromise = Promise.resolve(t);
}
} else if (ctx.proceed) {
maybePromise = ctx.proceed();
}
Expand Down
4 changes: 0 additions & 4 deletions src/helper/hook-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ export function createHookedFunction<ThisType, Args extends any[], Result>(
function runAroundHooks(): Promise<void> | void {
const hooks = aroundHooks.map((v) => {
const fn = v.hook as Middleware<IAroundJoinPoint<ThisType, Args, Result>>;
if (v.awaitPromise) {
fn.awaitPromise = true;
}
return fn;
});
const composed = compose<IAroundJoinPoint<ThisType, Args, Result>>(hooks);
Expand Down Expand Up @@ -396,7 +393,6 @@ function runOneHook<
P extends IJoinPoint,
>(hook: T, joinPoint: P, promise: Promise<any> | undefined): Promise<void> | undefined {
if (hook.awaitPromise) {
// 如果要求await hook,如果之前有promise,直接用,不然创建Promise给下一个使用
promise = promise || Promise.resolve();
promise = promise.then(() => {
return hook.hook(joinPoint);
Expand Down
4 changes: 1 addition & 3 deletions test/helper/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('di compose', () => {
console.log(`middleware1 result: ${result}`);
console.log(`middleware1 after: ${name}`);
};
middleware1.awaitPromise = true;

const middleware2: Middleware<ExampleContext> = async (ctx) => {
const name = ctx.getName();
Expand All @@ -28,7 +27,6 @@ describe('di compose', () => {
console.log(`middleware2 result: ${result}`);
console.log(`middleware2 after: ${name}`);
};
middleware2.awaitPromise = true;
const all = compose<ExampleContext>([middleware1, middleware2]);
let ret = undefined as any;
const result = all({
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion test/injector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4c7c9f0

Please sign in to comment.