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:
Andrew Eisenberg
2023-10-03 21:42:00 +00:00
parent ccbe4ee974
commit 3699f0b3b3
2 changed files with 16 additions and 7 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}