Skip to content

Commit

Permalink
refactor: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Sep 7, 2023
1 parent 700cad3 commit 6d77ba2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
24 changes: 11 additions & 13 deletions src/helper/compose.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
interface CallStack {
index: number;
depth: number;
}

export type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

export type Context<T> = {
proceed(): Promise<void> | void;
} & T;

export type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

export type PureContext<T> = Optional<Context<T>, 'proceed'>;

export interface Composed<C> {
Expand All @@ -21,36 +21,34 @@ export interface Middleware<C> {

function dispatch<C>(
middlewareList: Middleware<C>[],
depth: number,
idx: number,
stack: CallStack,
ctx: PureContext<C>,
): Promise<void> | void {
if (depth <= stack.index) {
if (idx <= stack.depth) {
throw new Error('joinPoint.proceed() called multiple times');

Check warning on line 29 in src/helper/compose.ts

View check run for this annotation

Codecov / codecov/patch

src/helper/compose.ts#L29

Added line #L29 was not covered by tests
}

stack.index = depth;
stack.depth = idx;

const { length } = middlewareList;

let maybePromise: Promise<void> | void;
if (depth <= length) {
if (depth < length) {
const middleware = middlewareList[depth];
if (idx <= length) {
if (idx < length) {
const middleware = middlewareList[idx];

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

if (middleware.awaitPromise) {
maybePromise = Promise.resolve(maybePromise);
}
} else if (ctx.proceed) {
// 这里可以不用 Promise.resolve
// 但是为了兼容旧的表现,这里还是加上了
maybePromise = ctx.proceed();
}
}
Expand All @@ -60,7 +58,7 @@ function dispatch<C>(

export default function compose<C>(middlewareList: Middleware<C>[]): Composed<C> {
return (ctx) => {
const stack = { index: -1 };
const stack: CallStack = { depth: -1 };
return dispatch<C>(middlewareList, 0, stack, ctx);
};
}
7 changes: 5 additions & 2 deletions test/helper/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ describe('di compose', () => {
getResult(): any;
}

const mockFn = jest.fn();

const middleware1: Middleware<ExampleContext> = async (ctx) => {
const name = ctx.getName();
console.log(`middleware1: ${name}`);
Expand All @@ -27,18 +29,19 @@ describe('di compose', () => {

const all = compose<ExampleContext>([middleware1, middleware2]);
let ret = undefined as any;
all({
await all({
getName() {
return 'example';
},
async proceed() {
console.log('invoked');
mockFn();
ret = 'final result';
},
getResult(): any {
return ret;
},
});
expect(mockFn).toBeCalledTimes(1);
});

it('can worked with sync', async () => {
Expand Down

0 comments on commit 6d77ba2

Please sign in to comment.