Skip to content

Commit

Permalink
fixup! onWillAddFirstListener/onDidRemoveLastListener get called corr…
Browse files Browse the repository at this point in the history
…ectly
  • Loading branch information
connor4312 committed Jun 22, 2023
1 parent 432179a commit 30e2bd1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/vs/base/common/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,35 +1027,38 @@ export class Emitter<T> {
return; // expected if a listener gets disposed
}

if (this._listeners instanceof UniqueContainer) {
if (this._size === 1) {
this._listeners = undefined;
this._options?.onDidRemoveLastListener?.(this);
this._size = 0;
return;
}

const index = this._listeners.indexOf(listener);
// size > 1 which requires that listeners be a list:
const listeners = this._listeners as (ListenerContainer<T> | undefined)[];

const index = listeners.indexOf(listener);
if (index === -1) {
throw new Error('Attempted to dispose unknown listener');
}

this._size--;
this._listeners[index] = undefined;
listeners[index] = undefined;

const adjustDeliveryQueue = this._deliveryQueue!.current === this;
if (this._size * compactionThreshold <= this._listeners.length) {
if (this._size * compactionThreshold <= listeners.length) {
let n = 0;
for (let i = 0; i < this._listeners.length; i++) {
if (this._listeners[i]) {
this._listeners[n++] = this._listeners[i];
for (let i = 0; i < listeners.length; i++) {
if (listeners[i]) {
listeners[n++] = listeners[i];
} else if (adjustDeliveryQueue) {
this._deliveryQueue!.end--;
if (n < this._deliveryQueue!.i) {
this._deliveryQueue!.i--;
}
}
}
this._listeners.length = n;
listeners.length = n;
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/vs/base/test/common/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,20 @@ suite('Event', function () {
assert.strictEqual(firstCount, 0);
assert.strictEqual(lastCount, 0);

let subscription = a.event(function () { });
let subscription1 = a.event(function () { });
const subscription2 = a.event(function () { });
assert.strictEqual(firstCount, 1);
assert.strictEqual(lastCount, 0);

subscription.dispose();
subscription1.dispose();
assert.strictEqual(firstCount, 1);
assert.strictEqual(lastCount, 0);

subscription2.dispose();
assert.strictEqual(firstCount, 1);
assert.strictEqual(lastCount, 1);

subscription = a.event(function () { });
subscription1 = a.event(function () { });
assert.strictEqual(firstCount, 2);
assert.strictEqual(lastCount, 1);
});
Expand Down

0 comments on commit 30e2bd1

Please sign in to comment.