Skip to content

Commit

Permalink
Merge pull request #173823 from microsoft/tyriar/173794
Browse files Browse the repository at this point in the history
Prevent undefined being fired when a listener is disposed
  • Loading branch information
Tyriar committed Feb 8, 2023
2 parents beb9ed3 + 9e5544b commit c148581
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/vs/base/common/event.ts
Expand Up @@ -254,7 +254,7 @@ export namespace Event {
});
},
onWillRemoveListener() {
if (flushOnListenerRemove) {
if (flushOnListenerRemove && numDebouncedCalls > 0) {
doFire?.();
}
},
Expand Down
21 changes: 20 additions & 1 deletion src/vs/base/test/common/event.test.ts
Expand Up @@ -1048,6 +1048,26 @@ suite('Event utils', () => {
});

suite('accumulate', () => {
test('should not fire after a listener is disposed with undefined or []', async () => {
const eventEmitter = new Emitter<number>();
const event = eventEmitter.event;
const accumulated = Event.accumulate(event, 0);

const calls1: number[][] = [];
const calls2: number[][] = [];
const listener1 = accumulated((e) => calls1.push(e));
accumulated((e) => calls2.push(e));

eventEmitter.fire(1);
await timeout(1);
assert.deepStrictEqual(calls1, [[1]]);
assert.deepStrictEqual(calls2, [[1]]);

listener1.dispose();
await timeout(1);
assert.deepStrictEqual(calls1, [[1]]);
assert.deepStrictEqual(calls2, [[1]], 'should not fire after a listener is disposed with undefined or []');
});
test('should accumulate a single event', async () => {
const eventEmitter = new Emitter<number>();
const event = eventEmitter.event;
Expand Down Expand Up @@ -1234,7 +1254,6 @@ suite('Event utils', () => {
assert.deepStrictEqual(calls, [1], 'should fire with the first event, not the second (after listener dispose)');
});


test('should flush events when the emitter is disposed', async () => {
const emitter = new Emitter<number>();
const debounced = Event.debounce(emitter.event, (l, e) => l ? l + 1 : 1, 0);
Expand Down

0 comments on commit c148581

Please sign in to comment.