make Event<T> accept DisposableStore in addition to IDisposable[], #74250

This commit is contained in:
Johannes Rieken 2019-06-07 17:15:58 +02:00
parent 2024296afc
commit a3ff1a4e22
2 changed files with 28 additions and 5 deletions

View file

@ -5,7 +5,7 @@
import { onUnexpectedError } from 'vs/base/common/errors';
import { once as onceFn } from 'vs/base/common/functional';
import { Disposable, IDisposable, toDisposable, combinedDisposable } from 'vs/base/common/lifecycle';
import { Disposable, IDisposable, toDisposable, combinedDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { LinkedList } from 'vs/base/common/linkedList';
/**
@ -13,7 +13,7 @@ import { LinkedList } from 'vs/base/common/linkedList';
* can be subscribed. The event is the subscriber function itself.
*/
export interface Event<T> {
(listener: (e: T) => any, thisArgs?: any, disposables?: IDisposable[]): IDisposable;
(listener: (e: T) => any, thisArgs?: any, disposables?: IDisposable[] | DisposableStore): IDisposable;
}
export namespace Event {
@ -476,7 +476,7 @@ export class Emitter<T> {
*/
get event(): Event<T> {
if (!this._event) {
this._event = (listener: (e: T) => any, thisArgs?: any, disposables?: IDisposable[]) => {
this._event = (listener: (e: T) => any, thisArgs?: any, disposables?: IDisposable[] | DisposableStore) => {
if (!this._listeners) {
this._listeners = new LinkedList();
}
@ -521,7 +521,9 @@ export class Emitter<T> {
}
}
};
if (Array.isArray(disposables)) {
if (disposables instanceof DisposableStore) {
disposables.add(result);
} else if (Array.isArray(disposables)) {
disposables.push(result);
}

View file

@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { Event, Emitter, EventBufferer, EventMultiplexer, AsyncEmitter, IWaitUntil, PauseableEmitter } from 'vs/base/common/event';
import { IDisposable } from 'vs/base/common/lifecycle';
import { IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import * as Errors from 'vs/base/common/errors';
import { timeout } from 'vs/base/common/async';
@ -73,6 +73,27 @@ suite('Event', function () {
while (bucket.length) {
bucket.pop()!.dispose();
}
doc.setText('boo');
// noop
subscription.dispose();
doc.setText('boo');
assert.equal(counter.count, 2);
});
test('Emitter, store', function () {
let bucket = new DisposableStore();
let doc = new Samples.Document3();
let subscription = doc.onDidChange(counter.onEvent, counter, bucket);
doc.setText('far');
doc.setText('boo');
// unhook listener
bucket.clear();
doc.setText('boo');
// noop
subscription.dispose();