Last active
March 23, 2024 13:52
-
-
Save nuintun/d851f276d511b710c6bf7a026f4d3cb2 to your computer and use it in GitHub Desktop.
koa-compose TypeScript 官方实现优化版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @module compose | |
*/ | |
export interface Next { | |
(): Promise<void>; | |
} | |
export interface Composed<C> { | |
(context: C, next?: Next): Promise<void>; | |
} | |
export interface Middleware<C> { | |
(context: C, next: Next): Promise<void> | void; | |
} | |
/** | |
* @function compose | |
* @description 生成融合中间件 | |
* @param middlewares 中间件数组 | |
*/ | |
export default function compose<C>(middlewares: Middleware<C>[]): Composed<C> { | |
/** | |
* @function middleware | |
* @description 融合中间件 | |
* @param context 执行上下文 | |
* @param next 下一个中间件 | |
*/ | |
return (context, next) => { | |
let current = -1; | |
const dispatch = async (index: number, next?: Next): Promise<void> => { | |
if (index <= current) { | |
throw new Error('next() called multiple times'); | |
} | |
current = index; | |
const { length } = middlewares; | |
if (index < length) { | |
const middleware = middlewares[index]; | |
await middleware(context, () => { | |
return dispatch(index + 1, next); | |
}); | |
} else if (next) { | |
await next(); | |
} | |
}; | |
return dispatch(0, next); | |
}; | |
} |
Author
nuintun
commented
Sep 17, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment