// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import { explodePromise } from './explodePromise.std.ts'; export type IController = { abort(): void; }; export class AbortableProcess implements IController { readonly #name: string; readonly #controller: IController; readonly #abortReject: (error: Error) => void; public readonly resultPromise: Promise; constructor( name: string, controller: IController, resultPromise: Promise ) { this.#name = name; this.#controller = controller; const { promise: abortPromise, reject: abortReject } = explodePromise(); this.#abortReject = abortReject; this.resultPromise = Promise.race([abortPromise, resultPromise]); } public abort(): void { this.#controller.abort(); this.#abortReject(new Error(`Process "${this.#name}" was aborted`)); } public getResult(): Promise { return this.resultPromise; } }