-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Proposal] middleware support before handleNextMessage (#156)
* notes * WIP middlewares * comments * MR feedback * remove comments * changing the naming of the middlewares * added tests to cover the new middleware * removing my use of sleep Co-authored-by: edward <edward@optizmo.com>
- Loading branch information
Showing
5 changed files
with
140 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './sleep' | ||
export * from './class-constructor' | ||
export * from './assert-unreachable' | ||
export * from './middleware' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* This middleware pattern has been grabbed from here: | ||
* https://evertpot.com/generic-middleware/ | ||
*/ | ||
|
||
/** | ||
* 'next' function, passed to a middleware | ||
*/ | ||
export type Next = () => void | Promise<void>; | ||
|
||
/** | ||
* A middleware | ||
*/ | ||
export type Middleware<T> = | ||
(context: T, next: Next) => Promise<void> | void | ||
|
||
/** | ||
* A middleware container and invoker | ||
*/ | ||
export class MiddlewareDispatcher<T> { | ||
|
||
middlewares: Middleware<T>[]; | ||
|
||
finalMiddlewares: Middleware<T>[]; | ||
|
||
constructor() { | ||
this.middlewares = []; | ||
this.finalMiddlewares = []; | ||
} | ||
|
||
/** | ||
* Add a middleware function. | ||
*/ | ||
use(...middlewares: Middleware<T>[]): void { | ||
this.middlewares.push(...middlewares); | ||
} | ||
|
||
/** | ||
* Add 'final' middlewares that will be added to the end of the | ||
* regular middlewares. This allows for finer control when exposing | ||
* the @see use functionality to consumers but wanting to ensure that your | ||
* final middleware is last to run | ||
*/ | ||
useFinal(...middlewares: Middleware<T>[]): void { | ||
this.finalMiddlewares.push(...middlewares); | ||
} | ||
|
||
/** | ||
* Execute the chain of middlewares, in the order they were added on a | ||
* given Context. | ||
*/ | ||
dispatch(context: T): Promise<void> { | ||
return invokeMiddlewares(context, this.middlewares.concat(this.finalMiddlewares)) | ||
} | ||
} | ||
|
||
|
||
async function invokeMiddlewares<T>(context: T, middlewares: Middleware<T>[]): Promise<void> { | ||
|
||
if (!middlewares.length) { | ||
return | ||
} | ||
|
||
const middleware = middlewares[0] | ||
|
||
return middleware(context, async () => { | ||
await invokeMiddlewares(context, middlewares.slice(1)) | ||
}) | ||
} |