Use EventEmitter in MultiCancellationToken
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user