Merge branch 'event-emitter' into sse-and-backoff

This commit is contained in:
2026-08-07 05:47:18 +00:00
4 changed files with 131 additions and 58 deletions
+37
View File
@@ -124,6 +124,21 @@ const testEventEmitterOffRemovesListenerByReference = (): void => {
expect(listener).not.toHaveBeenCalled();
};
/**
* Tests that off() removes all listeners for an event type when no listener is provided.
*/
const testEventEmitterOffRemovesAllListenersForEventType = (): void => {
const emitter = new EventEmitter<TestEvents>();
const listener = vi.fn();
emitter.on('message', listener);
emitter.off('message');
expect(listener).not.toHaveBeenCalled();
expect(emitter.emit('message', 'hello')).toBe(false);
expect(emitter.emit('count', 42)).toBe(false);
};
/**
* Tests that off() does nothing when given an unknown listener reference.
*/
@@ -476,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> => {
test('EventEmitter: calls listeners when an event is emitted', testEventEmitterCallsListeners);
test('EventEmitter: calls multiple listeners for the same event', testEventEmitterCallsMultipleListeners);
@@ -484,6 +516,10 @@ const runTests = async (): Promise<void> => {
test('EventEmitter: returns true when emitting with listeners', testEventEmitterEmitReturnsTrueWithListeners);
test('EventEmitter: stops calling a listener after its off callback is invoked', testEventEmitterOffCallbackRemovesListener);
test('EventEmitter: removes a listener when off is called with the same reference', testEventEmitterOffRemovesListenerByReference);
test(
'EventEmitter: removes all listeners for an event type when off is called with no listener',
testEventEmitterOffRemovesAllListenersForEventType,
);
test('EventEmitter: ignores off when the listener reference is unknown', testEventEmitterOffIgnoresUnknownListener);
test('EventEmitter: ignores off for an event type with no listeners', testEventEmitterOffIgnoresUnregisteredEventType);
test('EventEmitter: calls a once listener only one time', testEventEmitterOnceListenerFiresOnce);
@@ -500,6 +536,7 @@ const runTests = async (): Promise<void> => {
test('EventEmitter: resets the debounce timer on repeated emits', testEventEmitterDebouncedTimerResetsOnRepeatedEmits);
test('EventEmitter: does not debounce when debounceMilliseconds is zero', testEventEmitterZeroDebounceDoesNotDebounce);
test('EventEmitter: debounces once listeners and invokes them only once', testEventEmitterDebouncedOnceListener);
test('EventEmitter: rejects waitFor when the predicate function throws', testEventEmitterWaitForRejectsOnPredicateError);
};
await runTests();