buddy

node MVC discord bot
Log | Files | Refs | README

abort-controller.js (3610B)


      1 /**
      2  * @author Toru Nagashima <https://github.com/mysticatea>
      3  * See LICENSE file in root directory for full license.
      4  */
      5 'use strict';
      6 
      7 Object.defineProperty(exports, '__esModule', { value: true });
      8 
      9 var eventTargetShim = require('event-target-shim');
     10 
     11 /**
     12  * The signal class.
     13  * @see https://dom.spec.whatwg.org/#abortsignal
     14  */
     15 class AbortSignal extends eventTargetShim.EventTarget {
     16     /**
     17      * AbortSignal cannot be constructed directly.
     18      */
     19     constructor() {
     20         super();
     21         throw new TypeError("AbortSignal cannot be constructed directly");
     22     }
     23     /**
     24      * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
     25      */
     26     get aborted() {
     27         const aborted = abortedFlags.get(this);
     28         if (typeof aborted !== "boolean") {
     29             throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this === null ? "null" : typeof this}`);
     30         }
     31         return aborted;
     32     }
     33 }
     34 eventTargetShim.defineEventAttribute(AbortSignal.prototype, "abort");
     35 /**
     36  * Create an AbortSignal object.
     37  */
     38 function createAbortSignal() {
     39     const signal = Object.create(AbortSignal.prototype);
     40     eventTargetShim.EventTarget.call(signal);
     41     abortedFlags.set(signal, false);
     42     return signal;
     43 }
     44 /**
     45  * Abort a given signal.
     46  */
     47 function abortSignal(signal) {
     48     if (abortedFlags.get(signal) !== false) {
     49         return;
     50     }
     51     abortedFlags.set(signal, true);
     52     signal.dispatchEvent({ type: "abort" });
     53 }
     54 /**
     55  * Aborted flag for each instances.
     56  */
     57 const abortedFlags = new WeakMap();
     58 // Properties should be enumerable.
     59 Object.defineProperties(AbortSignal.prototype, {
     60     aborted: { enumerable: true },
     61 });
     62 // `toString()` should return `"[object AbortSignal]"`
     63 if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
     64     Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
     65         configurable: true,
     66         value: "AbortSignal",
     67     });
     68 }
     69 
     70 /**
     71  * The AbortController.
     72  * @see https://dom.spec.whatwg.org/#abortcontroller
     73  */
     74 class AbortController {
     75     /**
     76      * Initialize this controller.
     77      */
     78     constructor() {
     79         signals.set(this, createAbortSignal());
     80     }
     81     /**
     82      * Returns the `AbortSignal` object associated with this object.
     83      */
     84     get signal() {
     85         return getSignal(this);
     86     }
     87     /**
     88      * Abort and signal to any observers that the associated activity is to be aborted.
     89      */
     90     abort() {
     91         abortSignal(getSignal(this));
     92     }
     93 }
     94 /**
     95  * Associated signals.
     96  */
     97 const signals = new WeakMap();
     98 /**
     99  * Get the associated signal of a given controller.
    100  */
    101 function getSignal(controller) {
    102     const signal = signals.get(controller);
    103     if (signal == null) {
    104         throw new TypeError(`Expected 'this' to be an 'AbortController' object, but got ${controller === null ? "null" : typeof controller}`);
    105     }
    106     return signal;
    107 }
    108 // Properties should be enumerable.
    109 Object.defineProperties(AbortController.prototype, {
    110     signal: { enumerable: true },
    111     abort: { enumerable: true },
    112 });
    113 if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
    114     Object.defineProperty(AbortController.prototype, Symbol.toStringTag, {
    115         configurable: true,
    116         value: "AbortController",
    117     });
    118 }
    119 
    120 exports.AbortController = AbortController;
    121 exports.AbortSignal = AbortSignal;
    122 exports.default = AbortController;
    123 
    124 module.exports = AbortController
    125 module.exports.AbortController = module.exports["default"] = AbortController
    126 module.exports.AbortSignal = AbortSignal
    127 //# sourceMappingURL=abort-controller.js.map