Skip to content

Commit

Permalink
feat: Allow ConditionalHandler to set the expected value
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Oct 6, 2023
1 parent 42a1ca7 commit fedd9e0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/util/handlers/ConditionalHandler.ts
Expand Up @@ -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;
}
Expand All @@ -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;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions test/unit/util/handlers/ConditionalHandler.test.ts
Expand Up @@ -45,6 +45,15 @@ describe('A ConditionalHandler', (): void => {
await expect(handler.handleSafe(input)).resolves.toBe('handledSafely');
expect(source.handleSafe).toHaveBeenCalledTimes(1);
expect(source.handleSafe).toHaveBeenLastCalledWith(input);
expect(storage.get(storageKey)).toBeUndefined();
});

it('can store the value itself if requested after a handleSafe call.', async(): Promise<void> => {
handler = new ConditionalHandler(source, storage, storageKey, storageValue, true);
await expect(handler.handleSafe(input)).resolves.toBe('handledSafely');
expect(source.handleSafe).toHaveBeenCalledTimes(1);
expect(source.handleSafe).toHaveBeenLastCalledWith(input);
expect(storage.get(storageKey)).toBe(storageValue);
});

it('rejects all handleSafe requests once the storage value matches.', async(): Promise<void> => {
Expand All @@ -57,6 +66,7 @@ describe('A ConditionalHandler', (): void => {
await expect(handler.handle(input)).resolves.toBe('handled');
expect(source.handle).toHaveBeenCalledTimes(1);
expect(source.handle).toHaveBeenLastCalledWith(input);
expect(storage.get(storageKey)).toBeUndefined();
});

it('does not reject handle requests once the storage value matches.', async(): Promise<void> => {
Expand All @@ -65,4 +75,12 @@ describe('A ConditionalHandler', (): void => {
expect(source.handle).toHaveBeenCalledTimes(1);
expect(source.handle).toHaveBeenLastCalledWith(input);
});

it('can store the value itself if requested after a handle call.', async(): Promise<void> => {
handler = new ConditionalHandler(source, storage, storageKey, storageValue, true);
await expect(handler.handle(input)).resolves.toBe('handled');
expect(source.handle).toHaveBeenCalledTimes(1);
expect(source.handle).toHaveBeenLastCalledWith(input);
expect(storage.get(storageKey)).toBe(storageValue);
});
});

0 comments on commit fedd9e0

Please sign in to comment.