Add BasicDisposableObject
Use it for `MultiCancellationToken`. And ensure that adding a cancellation requested listener to the `MultiCancellationToken` will forward any cancellation requests to all constituent tokens.
This commit is contained in:
@@ -82,3 +82,12 @@ export abstract class DisposableObject implements Disposable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class BasicDisposableObject extends DisposableObject {
|
||||
constructor(...dispoables: Disposable[]) {
|
||||
super();
|
||||
for (const d of dispoables) {
|
||||
this.push(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { CancellationToken, Event, EventEmitter } from "vscode";
|
||||
import { CancellationToken, Disposable } from "vscode";
|
||||
import { BasicDisposableObject } from "../disposable-object";
|
||||
|
||||
/**
|
||||
* A cancellation token that cancels when any of its constituent
|
||||
@@ -6,20 +7,19 @@ import { CancellationToken, Event, EventEmitter } 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);
|
||||
}
|
||||
|
||||
get onCancellationRequested(): Event<any> {
|
||||
return this.onCancellationRequestedEvent.event;
|
||||
onCancellationRequested<T>(listener: (e: T) => any): Disposable {
|
||||
const disposables = this.tokens.map((t) =>
|
||||
t.onCancellationRequested(listener),
|
||||
);
|
||||
return new BasicDisposableObject(...disposables);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user