abort-controller.js 3.5 KB

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