Merge from vscode e3c4990c67c40213af168300d1cfeb71d680f877 (#16569)

This commit is contained in:
Cory Rivera
2021-08-25 16:28:29 -07:00
committed by GitHub
parent ab1112bfb3
commit cb7b7da0a4
1752 changed files with 59525 additions and 33878 deletions

View File

@@ -68,6 +68,7 @@ export namespace Event {
* Given an event and a `filter` function, returns another event which emits those
* elements for which the `filter` function returns `true`.
*/
export function filter<T, U>(event: Event<T | U>, filter: (e: T | U) => e is T): Event<T>;
export function filter<T>(event: Event<T>, filter: (e: T) => boolean): Event<T>;
export function filter<T, R>(event: Event<T | R>, filter: (e: T | R) => e is R): Event<R>;
export function filter<T>(event: Event<T>, filter: (e: T) => boolean): Event<T> {
@@ -188,18 +189,29 @@ export namespace Event {
* Given an event, it returns another event which fires only when the event
* element changes.
*/
export function latch<T>(event: Event<T>): Event<T> {
export function latch<T>(event: Event<T>, equals: (a: T, b: T) => boolean = (a, b) => a === b): Event<T> {
let firstCall = true;
let cache: T;
return filter(event, value => {
const shouldEmit = firstCall || value !== cache;
const shouldEmit = firstCall || !equals(value, cache);
firstCall = false;
cache = value;
return shouldEmit;
});
}
/**
* Given an event, it returns another event which fires only when the event
* element changes.
*/
export function split<T, U>(event: Event<T | U>, isT: (e: T | U) => e is T): [Event<T>, Event<U>] {
return [
Event.filter(event, isT),
Event.filter(event, e => !isT(e)) as Event<U>,
];
}
/**
* Buffers the provided event until a first listener comes
* along, at which point fire all the events at once and
@@ -633,10 +645,13 @@ export class Emitter<T> {
}
dispose() {
this._listeners?.clear();
this._deliveryQueue?.clear();
this._leakageMon?.dispose();
this._disposed = true;
if (!this._disposed) {
this._disposed = true;
this._listeners?.clear();
this._deliveryQueue?.clear();
this._options?.onLastListenerRemove?.();
this._leakageMon?.dispose();
}
}
}