Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/util/cancelable.ts
2500 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { Disposable } from "./disposable";
8
9
export class Cancelable<T> implements Disposable {
10
protected canceled: boolean;
11
12
constructor(protected readonly activity: (cancel: boolean) => Promise<T> | undefined) {}
13
14
public async run(): Promise<T | undefined> {
15
for (let r = await this.activity(this.canceled); ; r = await this.activity(this.canceled)) {
16
if (this.canceled) {
17
return;
18
} else if (r !== undefined) {
19
return r;
20
}
21
}
22
}
23
24
public cancel() {
25
this.canceled = true;
26
}
27
28
dispose(): void {
29
this.cancel();
30
}
31
}
32
33