UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

38 lines (37 loc) 888 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Abortable = void 0; exports.abortable = abortable; /** * Similar to AbortController and AbortSignal. * Similar to pDefer and Promise. * Similar to Subject and Observable. * * Minimal interface for something that can be aborted in the future, * but not necessary. * Allows to listen to `onAbort` event. * * @experimental */ class Abortable { onAbort; constructor(onAbort) { this.onAbort = onAbort; } aborted = false; abort() { if (this.aborted) return; this.aborted = true; this.onAbort?.(); this.onAbort = undefined; // cleanup listener } clear() { this.onAbort = undefined; } } exports.Abortable = Abortable; // convenience function function abortable(onAbort) { return new Abortable(onAbort); }