Handle errors in Predicate function
This commit is contained in:
@@ -181,7 +181,12 @@ export class EventEmitter<T extends EventMap> {
|
|||||||
|
|
||||||
// Create a listener function.
|
// Create a listener function.
|
||||||
const listener = (payload: Readonly<T[K]>): void => {
|
const listener = (payload: Readonly<T[K]>): void => {
|
||||||
if (predicate(payload)) {
|
try {
|
||||||
|
// If the event payload does not match the predicate condition, return.
|
||||||
|
if (!predicate(payload)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
this.off(type, listener);
|
this.off(type, listener);
|
||||||
if (timeoutId !== undefined) {
|
if (timeoutId !== undefined) {
|
||||||
@@ -189,6 +194,8 @@ export class EventEmitter<T extends EventMap> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resolve(payload);
|
resolve(payload);
|
||||||
|
} catch (error) {
|
||||||
|
reject(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -491,6 +491,23 @@ const testEventEmitterDebouncedOnceListener = async (): Promise<void> => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that the `waitFor` method rejects if the predicate function throws
|
||||||
|
*/
|
||||||
|
const testEventEmitterWaitForRejectsOnPredicateError = async (): Promise<void> => {
|
||||||
|
const emitter = new EventEmitter<TestEvents>();
|
||||||
|
const listener = vi.fn();
|
||||||
|
|
||||||
|
const waitPromise = emitter.waitFor('message', () => {
|
||||||
|
throw new Error('predicate error');
|
||||||
|
});
|
||||||
|
|
||||||
|
emitter.emit('message', 'hello');
|
||||||
|
|
||||||
|
await expect(waitPromise).rejects.toThrow('predicate error');
|
||||||
|
expect(listener).not.toHaveBeenCalled();
|
||||||
|
};
|
||||||
|
|
||||||
const runTests = async (): Promise<void> => {
|
const runTests = async (): Promise<void> => {
|
||||||
test('EventEmitter: calls listeners when an event is emitted', testEventEmitterCallsListeners);
|
test('EventEmitter: calls listeners when an event is emitted', testEventEmitterCallsListeners);
|
||||||
test('EventEmitter: calls multiple listeners for the same event', testEventEmitterCallsMultipleListeners);
|
test('EventEmitter: calls multiple listeners for the same event', testEventEmitterCallsMultipleListeners);
|
||||||
@@ -516,6 +533,7 @@ const runTests = async (): Promise<void> => {
|
|||||||
test('EventEmitter: resets the debounce timer on repeated emits', testEventEmitterDebouncedTimerResetsOnRepeatedEmits);
|
test('EventEmitter: resets the debounce timer on repeated emits', testEventEmitterDebouncedTimerResetsOnRepeatedEmits);
|
||||||
test('EventEmitter: does not debounce when debounceMilliseconds is zero', testEventEmitterZeroDebounceDoesNotDebounce);
|
test('EventEmitter: does not debounce when debounceMilliseconds is zero', testEventEmitterZeroDebounceDoesNotDebounce);
|
||||||
test('EventEmitter: debounces once listeners and invokes them only once', testEventEmitterDebouncedOnceListener);
|
test('EventEmitter: debounces once listeners and invokes them only once', testEventEmitterDebouncedOnceListener);
|
||||||
|
test('EventEmitter: rejects waitFor when the predicate function throws', testEventEmitterWaitForRejectsOnPredicateError);
|
||||||
};
|
};
|
||||||
|
|
||||||
await runTests();
|
await runTests();
|
||||||
|
|||||||
Reference in New Issue
Block a user