mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Allow ConditionalHandler to set the expected value
This commit is contained in:
@@ -8,22 +8,27 @@ import { AsyncHandler } from './AsyncHandler';
|
||||
* After that all input will be rejected.
|
||||
* Once the value has been matched this behaviour will be cached,
|
||||
* so changing the value again afterwards will not enable this handler again.
|
||||
*
|
||||
* If `handleStorage` is set to `true`,
|
||||
* this handler will set the value itself in the given storage after the source handler successfully resolved.
|
||||
*/
|
||||
export class ConditionalHandler<TIn, TOut> extends AsyncHandler<TIn, TOut> {
|
||||
private readonly source: AsyncHandler<TIn, TOut>;
|
||||
private readonly storage: KeyValueStorage<string, unknown>;
|
||||
private readonly storageKey: string;
|
||||
private readonly storageValue: unknown;
|
||||
private readonly handleStorage: boolean;
|
||||
|
||||
private finished: boolean;
|
||||
|
||||
public constructor(source: AsyncHandler<TIn, TOut>, storage: KeyValueStorage<string, unknown>, storageKey: string,
|
||||
storageValue: unknown) {
|
||||
storageValue: unknown, handleStorage = false) {
|
||||
super();
|
||||
this.source = source;
|
||||
this.storage = storage;
|
||||
this.storageKey = storageKey;
|
||||
this.storageValue = storageValue;
|
||||
this.handleStorage = handleStorage;
|
||||
|
||||
this.finished = false;
|
||||
}
|
||||
@@ -35,11 +40,21 @@ export class ConditionalHandler<TIn, TOut> extends AsyncHandler<TIn, TOut> {
|
||||
|
||||
public async handleSafe(input: TIn): Promise<TOut> {
|
||||
await this.checkCondition();
|
||||
return this.source.handleSafe(input);
|
||||
const result = await this.source.handleSafe(input);
|
||||
if (this.handleStorage) {
|
||||
await this.storage.set(this.storageKey, this.storageValue);
|
||||
this.finished = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public async handle(input: TIn): Promise<TOut> {
|
||||
return this.source.handle(input);
|
||||
const result = await this.source.handle(input);
|
||||
if (this.handleStorage) {
|
||||
await this.storage.set(this.storageKey, this.storageValue);
|
||||
this.finished = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user