mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add CompositeAsyncHandler to support multiple handlers
This commit is contained in:
@@ -1,17 +1,35 @@
|
||||
/**
|
||||
* Simple interface for classes that can potentially handle a specific kind of data asynchronously.
|
||||
*/
|
||||
export interface AsyncHandler<TInput, TOutput = void> {
|
||||
export abstract class AsyncHandler<TInput, TOutput = void> {
|
||||
/**
|
||||
* Checks if the input data can be handled by this class.
|
||||
* Throws an error if it can't handle the data.
|
||||
* @param input - Input data that would be handled potentially.
|
||||
* @returns A promise resolving to if this input can be handled.
|
||||
*
|
||||
* @returns A promise resolving if this input can be handled, rejecting with an Error if not.
|
||||
*/
|
||||
canHandle: (input: TInput) => Promise<boolean>;
|
||||
public abstract canHandle (input: TInput): Promise<void>;
|
||||
|
||||
/**
|
||||
* Handles the given input. This should only be done if the {@link canHandle} function returned `true`.
|
||||
* @param input - Input data that needs to be handled.
|
||||
*
|
||||
* @returns A promise resolving when the handling is finished. Return value depends on the given type.
|
||||
*/
|
||||
handle: (input: TInput) => Promise<TOutput>;
|
||||
public abstract handle (input: TInput): Promise<TOutput>;
|
||||
|
||||
/**
|
||||
* Helper function that first runs the canHandle function followed by the handle function.
|
||||
* Throws the error of the {@link canHandle} function if the data can't be handled,
|
||||
* or returns the result of the {@link handle} function otherwise.
|
||||
* @param data - The data to handle.
|
||||
*
|
||||
* @returns The result of the handle function of the handler.
|
||||
*/
|
||||
public async handleSafe (data: TInput): Promise<TOutput> {
|
||||
await this.canHandle(data);
|
||||
|
||||
return this.handle(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user