-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Create multiple composite handlers
- Loading branch information
Showing
16 changed files
with
100 additions
and
44 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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { AsyncHandler } from './AsyncHandler'; | ||
|
||
/** | ||
* A composite handler that runs all of its handlers independent of their result. | ||
* The `canHandle` check of this handler will always succeed. | ||
*/ | ||
export class AllVoidCompositeHandler<TIn> extends AsyncHandler<TIn> { | ||
private readonly handlers: AsyncHandler<TIn>[]; | ||
|
||
public constructor(handlers: AsyncHandler<TIn>[]) { | ||
super(); | ||
this.handlers = handlers; | ||
} | ||
|
||
public async handle(input: TIn): Promise<void> { | ||
for (const handler of this.handlers) { | ||
try { | ||
await handler.handleSafe(input); | ||
} catch { | ||
// Ignore errors | ||
} | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { AllVoidCompositeHandler } from '../../../src/util/AllVoidCompositeHandler'; | ||
import type { AsyncHandler } from '../../../src/util/AsyncHandler'; | ||
|
||
describe('An AllVoidCompositeHandler', (): void => { | ||
let handler1: AsyncHandler<string>; | ||
let handler2: AsyncHandler<string>; | ||
let composite: AllVoidCompositeHandler<string>; | ||
|
||
beforeEach(async(): Promise<void> => { | ||
handler1 = { handleSafe: jest.fn() } as any; | ||
handler2 = { handleSafe: jest.fn() } as any; | ||
|
||
composite = new AllVoidCompositeHandler<string>([ handler1, handler2 ]); | ||
}); | ||
|
||
it('can handle all input.', async(): Promise<void> => { | ||
await expect(composite.canHandle('test')).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('runs all handlers without caring about their result.', async(): Promise<void> => { | ||
handler1.handleSafe = jest.fn(async(): Promise<void> => { | ||
throw new Error('error'); | ||
}); | ||
await expect(composite.handleSafe('test')).resolves.toBeUndefined(); | ||
expect(handler1.handleSafe).toHaveBeenCalledTimes(1); | ||
expect(handler1.handleSafe).toHaveBeenLastCalledWith('test'); | ||
expect(handler2.handleSafe).toHaveBeenCalledTimes(1); | ||
expect(handler2.handleSafe).toHaveBeenLastCalledWith('test'); | ||
}); | ||
}); |
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