text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.CancelToken = void 0;
  const EventEmitter = require("eventemitter3");
  const errors = require("./errors");
  // reference-style links don't seem to work here, for whatever reason
  /**
   * A cancellation token loosely based on [the proposal](https://github.com/tc39/proposal-cancellation/blob/master/stage0/README.md).
   */
  class CancelToken extends EventEmitter {
      /**
       * Create a new cancellation token.
       *
       * The `executor` callback is called immediately by this constructor.
       * It gets passed the `cancel` callback, which should be called on a cancellation request.
       *
       * @example this token is cancelled after a second passes:
       * ```typescript
       * const token = new CancelToken((cancel) => setTimeout(cancel, 1000));
       * ```
       */
      constructor(executor) {
          super();
          this._isCanceled = false;
          executor(() => this._cancel());
      }
      get isCanceled() {
          return this._isCanceled;
      }
      _cancel() {
          this._isCanceled = true;
          this._error = new errors.CancelError();
          this.emit("_cancel");
      }
      /** @internal */
      _onCancel(log, handler) {
          this.once("_cancel", () => {
              log.info(this._error);
              handler(this._error);
          });
      }
      /** @internal */
      _throwIfCanceled(log) {
          if (this._isCanceled) {
              log.info(this._error);
              throw this._error;
          }
      }
      /** @internal */
      _rejectOnCancel(log) {
          return new Promise((_, reject) => {
              this.once("_cancel", () => {
                  log.info(this._error);
                  reject(this._error);
              });
          });
      }
      /**
       * Create a token that is automatically cancelled after `ms` milliseconds.
      */
      static cancelAfter(ms) {
          return new CancelToken((cancel) => setTimeout(cancel, ms));
      }
  }
  exports.CancelToken = CancelToken;
  //# sourceMappingURL=cancel.js.map