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 24390df commit ca2d7be
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/helper/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,23 @@ function dispatch<C>(

stack.depth = idx;

const { length } = middlewareList;

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

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

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

if (middleware.awaitPromise) {
maybePromise = Promise.resolve(maybePromise);
}
} else if (ctx.proceed) {
maybePromise = ctx.proceed();
if (middleware.awaitPromise) {
maybePromise = Promise.resolve(maybePromise);
}
} else if (ctx.proceed) {
maybePromise = ctx.proceed();
}

return maybePromise;
Expand Down

0 comments on commit ca2d7be

Please sign in to comment.