How to use the @esfx/cancelable.Cancelable.cancelSignal function in @esfx/cancelable

To help you get started, we’ve selected a few @esfx/cancelable examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github esfx / esfx / packages / cancelable-dom-shim / src / index.ts View on Github external
abortSignal.removeEventListener("abort", callback);
                        callback = undefined!;
                    }
                });
            }
        };
    }

    function getOrCreateCancelSignal(abortSignal: AbortSignal) {
        let cancelSignal = weakCancelSignal.get(abortSignal);
        if (!cancelSignal) weakCancelSignal.set(abortSignal, cancelSignal = createCancelSignal(abortSignal));
        return cancelSignal;
    }

    Object.defineProperties(AbortSignal.prototype, {
        [Cancelable.cancelSignal]: {
            ...methodBase,
            value(this: AbortSignal) {
                return getOrCreateCancelSignal(this);
            }
        }
    });

    Object.defineProperties(AbortController.prototype, {
        [CancelableSource.cancelSignal]: {
            ...methodBase,
            value(this: AbortController) {
                return getOrCreateCancelSignal(this.signal);
            }
        },
        [CancelableSource.cancel]: {
            ...methodBase,
github esfx / esfx / packages / async-canceltoken / src / index.ts View on Github external
setImmediate(() => { throw e; });
    }
}

function isSignaled(signal: CancelSignal) {
    return signal.signaled;
}

function canBeSignaled(signal: CancelSignal) {
    return signal !== Cancelable.none && (!(signal instanceof CancelToken) || signal.canBeSignaled);
}

const disposablePrototype: object = Object.getPrototypeOf(Disposable.create(() => { }));

const cancelSourcePrototype: object = {
    [Cancelable.cancelSignal](this: CancelSource) { return this.token; },
    [CancelableSource.cancel](this: CancelSource) { this.cancel(); },
};

Object.setPrototypeOf(cancelSourcePrototype, disposablePrototype);
defineTag(cancelSourcePrototype, "CancelSource");

function createCancelSource(links: CancelLinks | undefined): CancelSource {
    let state: "unsignaled" | "signaled" | "closed" = "unsignaled";
    let token: CancelToken | undefined;
    let subscriptions: LinkedList<() => void> | undefined;
    const source: CancelSource = Object.setPrototypeOf({
        get token() {
            return token || (token = createCancelToken({
                getState() {
                    return state === "unsignaled" && links ? links.getLinkedState() : state;
                },
github esfx / esfx / packages / async-canceltoken / src / index.ts View on Github external
static race(cancelables: Iterable) {
        if (!isIterable(cancelables)) {
            throw new TypeError("Object not iterable: cancelables");
        }
        const signals: CancelSignal[] = [];
        for (const cancelable of cancelables) {
            if (!Cancelable.hasInstance(cancelable)) throw new TypeError("Cancelable element expected: cancelables");
            signals.push(cancelable[Cancelable.cancelSignal]());
        }
        if (!signals.some(canBeSignaled)) {
            return CancelToken.none;
        }
        if (signals.some(isSignaled)) {
            return CancelToken.canceled;
        }
        const subscriptions: CancelSubscription[] = [];
        const source = createCancelSource({
            getLinkedState() {
                if (signals.some(isSignaled)) return "signaled";
                if (signals.every(canBeSignaled)) return "unsignaled";
                return "closed";
            },
            unlink() {
                if (subscriptions.length > 0) {