mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
change: Refactor AllVoidCompositeHandler into SequenceHandler.
This commit is contained in:
committed by
Joachim Van Herwegen
parent
7cae14acf7
commit
ba47ce7951
32
src/util/SequenceHandler.ts
Normal file
32
src/util/SequenceHandler.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { AsyncHandler } from './AsyncHandler';
|
||||
|
||||
/**
|
||||
* A composite handler that will try to run all supporting handlers sequentially
|
||||
* and return the value of the last supported handler.
|
||||
* The `canHandle` check of this handler will always succeed.
|
||||
*/
|
||||
export class SequenceHandler<TIn = void, TOut = void> extends AsyncHandler<TIn, TOut | undefined> {
|
||||
private readonly handlers: AsyncHandler<TIn, TOut>[];
|
||||
|
||||
public constructor(handlers: AsyncHandler<TIn, TOut>[]) {
|
||||
super();
|
||||
this.handlers = [ ...handlers ];
|
||||
}
|
||||
|
||||
public async handle(input: TIn): Promise<TOut | undefined> {
|
||||
let result: TOut | undefined;
|
||||
for (const handler of this.handlers) {
|
||||
let supported: boolean;
|
||||
try {
|
||||
await handler.canHandle(input);
|
||||
supported = true;
|
||||
} catch {
|
||||
supported = false;
|
||||
}
|
||||
if (supported) {
|
||||
result = await handler.handle(input);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user