Dont emit to debounced listener if off was called

This commit is contained in:
2026-08-10 02:32:22 +00:00
parent bc28a7e96a
commit 57e809157b
2 changed files with 53 additions and 19 deletions
+28
View File
@@ -261,6 +261,30 @@ const testEventEmitterOnceOffCallbackRemovesListener = (): void => {
expect(listener).not.toHaveBeenCalled();
};
/**
* Tests that debounced listeners do not receive the debounced event if the listener is removed.
*/
const testEventEmitterOffCancelsPendingDebouncedCallback = async (): Promise<void> => {
vi.useFakeTimers();
try {
const emitter = new EventEmitter<TestEvents>();
const listener = vi.fn();
const off = emitter.on('message', listener, 100);
emitter.emit('message', 'first');
off();
emitter.emit('message', 'second');
await vi.advanceTimersByTimeAsync(100);
expect(listener).not.toHaveBeenCalled();
} finally {
vi.useRealTimers();
vi.restoreAllMocks();
}
};
/**
* Tests that removeAllListeners() clears every registered listener.
*/
@@ -629,6 +653,10 @@ const runTests = async (): Promise<void> => {
test('EventEmitter: calls a once listener only one time', testEventEmitterOnceListenerFiresOnce);
test('EventEmitter: registers once when listeners already exist', testEventEmitterOnceWorksWithExistingListeners);
test('EventEmitter: stops a once listener after its off callback is invoked', testEventEmitterOnceOffCallbackRemovesListener);
test(
'EventEmitter: debounced listeners do not receive the debounced event if the listener is removed',
testEventEmitterOffCancelsPendingDebouncedCallback,
);
test('EventEmitter: removes all listeners when removeAllListeners is called', testEventEmitterRemoveAllListeners);
test('EventEmitter: resolves waitFor when a matching event is emitted', testEventEmitterWaitForResolvesOnMatch);
test('EventEmitter: ignores non-matching events while waiting with waitFor', testEventEmitterWaitForIgnoresNonMatchingEvents);