Use EventEmitter in MultiCancellationToken

This commit is contained in:
Andrew Eisenberg
2023-10-02 10:21:17 -07:00
parent 04dfc4e647
commit 0fa3cf5d8e

View File

@@ -1,4 +1,4 @@
import { CancellationToken, Disposable } from "vscode";
import { CancellationToken, Event, EventEmitter } from "vscode";
/**
* A cancellation token that cancels when any of its constituent
@@ -6,23 +6,20 @@ import { CancellationToken, Disposable } from "vscode";
*/
export class MultiCancellationToken implements CancellationToken {
private readonly tokens: CancellationToken[];
private readonly onCancellationRequestedEvent = new EventEmitter<void>();
constructor(...tokens: CancellationToken[]) {
this.tokens = tokens;
tokens.forEach((t) =>
t.onCancellationRequested(() => this.onCancellationRequestedEvent.fire()),
);
}
get isCancellationRequested(): boolean {
return this.tokens.some((t) => t.isCancellationRequested);
}
onCancellationRequested<T>(listener: (e: T) => any): Disposable {
this.tokens.forEach((t) => t.onCancellationRequested(listener));
return {
dispose: () => {
this.tokens.forEach((t) =>
t.onCancellationRequested(listener).dispose(),
);
},
};
get onCancellationRequested(): Event<any> {
return this.onCancellationRequestedEvent.event;
}
}