@esfx/cancelable Package

    The @esfx/cancelable package provides a low-level Symbol-based API for defining a common cancellation protocol.

    Note

    This package does not contain an implementation of cancellation signals, but rather provides only a protocol for interoperable libraries that depend on cancellation.

    For an implementation of this protocol, please consider the following packages:

    • @esfx/canceltoken
    • @esfx/cancelable-dom
    • @esfx/cancelable-dom-shim
    • prex (version 0.4.6 or later)

    Installation

    npm i @esfx/cancelable
    

    Usage

    • TypeScript
    • JavaScript (CommonJS)
    import { Cancelable } from "@esfx/cancelable";
    import { fork } from "child_process";
    
    function doSomeWork(cancelable: Cancelable) {
        return new Promise<void>((resolve, reject) => {
            const cancelSignal = cancelable[Cancelable.cancelSignal]();
            if (cancelSignal.signaled) throw new Error("Operation canceled.");
    
            const worker = fork("worker.js");
            const subscription = cancelSignal.subscribe(() => {
                // cancellation requested, abort worker
                worker.kill();
                reject(new Error("Operation canceled."));
            });
    
            worker.on("exit", () => {
                subscription.unsubscribe();
                resolve();
            });
        });
    }
    
    const { Cancelable } = require("@esfx/cancelable");
    const { fork } = require("child_process");
    
    function doSomeWork(cancelable) {
        return new Promise((resolve, reject) => {
            const cancelSignal = cancelable[Cancelable.cancelSignal]();
            if (cancelSignal.signaled) throw new Error("Operation canceled.");
    
            const worker = fork("worker.js");
            const subscription = cancelSignal.subscribe(() => {
                // cancellation requested, abort worker
                worker.kill();
                reject(new Error("Operation canceled."));
            });
    
            worker.on("exit", () => {
                subscription.unsubscribe();
                resolve();
            });
        });
    }
    

    Classes

    CancelError

    Interfaces

    Cancelable

    An object that can be canceled from an external source.

    CancelableCancelSignal

    CancelableSource

    Represents an object that is a source for cancelation.

    CancelSignal

    An object that represents a cancellation signal.

    CancelSubscription

    An object used to unsubscribe from a cancellation signal

    Namespaces

    Cancelable

    CancelableSource

    CancelSubscription

    • Improve this Doc
    Generated by DocFX