Path: blob/main/components/gitpod-protocol/src/util/cancelable.ts
2500 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { Disposable } from "./disposable";78export class Cancelable<T> implements Disposable {9protected canceled: boolean;1011constructor(protected readonly activity: (cancel: boolean) => Promise<T> | undefined) {}1213public async run(): Promise<T | undefined> {14for (let r = await this.activity(this.canceled); ; r = await this.activity(this.canceled)) {15if (this.canceled) {16return;17} else if (r !== undefined) {18return r;19}20}21}2223public cancel() {24this.canceled = true;25}2627dispose(): void {28this.cancel();29}30}313233