Skip to content

Commit 3699f0b

Browse files
committed
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.
1 parent ccbe4ee commit 3699f0b

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

extensions/ql-vscode/src/common/disposable-object.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,12 @@ export abstract class DisposableObject implements Disposable {
8282
}
8383
}
8484
}
85+
86+
export class BasicDisposableObject extends DisposableObject {
87+
constructor(...dispoables: Disposable[]) {
88+
super();
89+
for (const d of dispoables) {
90+
this.push(d);
91+
}
92+
}
93+
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import { CancellationToken, Event, EventEmitter } from "vscode";
1+
import { CancellationToken, Disposable } from "vscode";
2+
import { BasicDisposableObject } from "../disposable-object";
23

34
/**
45
* A cancellation token that cancels when any of its constituent
56
* cancellation tokens are cancelled.
67
*/
78
export class MultiCancellationToken implements CancellationToken {
89
private readonly tokens: CancellationToken[];
9-
private readonly onCancellationRequestedEvent = new EventEmitter<void>();
1010

1111
constructor(...tokens: CancellationToken[]) {
1212
this.tokens = tokens;
13-
tokens.forEach((t) =>
14-
t.onCancellationRequested(() => this.onCancellationRequestedEvent.fire()),
15-
);
1613
}
1714

1815
get isCancellationRequested(): boolean {
1916
return this.tokens.some((t) => t.isCancellationRequested);
2017
}
2118

22-
get onCancellationRequested(): Event<any> {
23-
return this.onCancellationRequestedEvent.event;
19+
onCancellationRequested<T>(listener: (e: T) => any): Disposable {
20+
const disposables = this.tokens.map((t) =>
21+
t.onCancellationRequested(listener),
22+
);
23+
return new BasicDisposableObject(...disposables);
2424
}
2525
}

0 commit comments

Comments
 (0)