All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.build.esm.syncpromise.js.map Maven / Gradle / Ivy

There is a newer version: 8.38.0
Show newest version
{"version":3,"file":"syncpromise.js","sources":["../../src/syncpromise.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-function-return-type */\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { isThenable } from './is';\n\n/** SyncPromise internal states */\nconst enum States {\n  /** Pending */\n  PENDING = 0,\n  /** Resolved / OK */\n  RESOLVED = 1,\n  /** Rejected / Error */\n  REJECTED = 2,\n}\n\n// Overloads so we can call resolvedSyncPromise without arguments and generic argument\nexport function resolvedSyncPromise(): PromiseLike;\nexport function resolvedSyncPromise(value: T | PromiseLike): PromiseLike;\n\n/**\n * Creates a resolved sync promise.\n *\n * @param value the value to resolve the promise with\n * @returns the resolved sync promise\n */\nexport function resolvedSyncPromise(value?: T | PromiseLike): PromiseLike {\n  return new SyncPromise(resolve => {\n    resolve(value);\n  });\n}\n\n/**\n * Creates a rejected sync promise.\n *\n * @param value the value to reject the promise with\n * @returns the rejected sync promise\n */\nexport function rejectedSyncPromise(reason?: any): PromiseLike {\n  return new SyncPromise((_, reject) => {\n    reject(reason);\n  });\n}\n\n/**\n * Thenable class that behaves like a Promise and follows it's interface\n * but is not async internally\n */\nclass SyncPromise implements PromiseLike {\n  private _state: States;\n  private _handlers: Array<[boolean, (value: T) => void, (reason: any) => any]>;\n  private _value: any;\n\n  public constructor(\n    executor: (resolve: (value?: T | PromiseLike | null) => void, reject: (reason?: any) => void) => void,\n  ) {\n    this._state = States.PENDING;\n    this._handlers = [];\n\n    try {\n      executor(this._resolve, this._reject);\n    } catch (e) {\n      this._reject(e);\n    }\n  }\n\n  /** JSDoc */\n  public then(\n    onfulfilled?: ((value: T) => TResult1 | PromiseLike) | null,\n    onrejected?: ((reason: any) => TResult2 | PromiseLike) | null,\n  ): PromiseLike {\n    return new SyncPromise((resolve, reject) => {\n      this._handlers.push([\n        false,\n        result => {\n          if (!onfulfilled) {\n            // TODO: ¯\\_(ツ)_/¯\n            // TODO: FIXME\n            resolve(result as any);\n          } else {\n            try {\n              resolve(onfulfilled(result));\n            } catch (e) {\n              reject(e);\n            }\n          }\n        },\n        reason => {\n          if (!onrejected) {\n            reject(reason);\n          } else {\n            try {\n              resolve(onrejected(reason));\n            } catch (e) {\n              reject(e);\n            }\n          }\n        },\n      ]);\n      this._executeHandlers();\n    });\n  }\n\n  /** JSDoc */\n  public catch(\n    onrejected?: ((reason: any) => TResult | PromiseLike) | null,\n  ): PromiseLike {\n    return this.then(val => val, onrejected);\n  }\n\n  /** JSDoc */\n  public finally(onfinally?: (() => void) | null): PromiseLike {\n    return new SyncPromise((resolve, reject) => {\n      let val: TResult | any;\n      let isRejected: boolean;\n\n      return this.then(\n        value => {\n          isRejected = false;\n          val = value;\n          if (onfinally) {\n            onfinally();\n          }\n        },\n        reason => {\n          isRejected = true;\n          val = reason;\n          if (onfinally) {\n            onfinally();\n          }\n        },\n      ).then(() => {\n        if (isRejected) {\n          reject(val);\n          return;\n        }\n\n        resolve(val as unknown as any);\n      });\n    });\n  }\n\n  /** JSDoc */\n  private readonly _resolve = (value?: T | PromiseLike | null) => {\n    this._setResult(States.RESOLVED, value);\n  };\n\n  /** JSDoc */\n  private readonly _reject = (reason?: any) => {\n    this._setResult(States.REJECTED, reason);\n  };\n\n  /** JSDoc */\n  private readonly _setResult = (state: States, value?: T | PromiseLike | any) => {\n    if (this._state !== States.PENDING) {\n      return;\n    }\n\n    if (isThenable(value)) {\n      void (value as PromiseLike).then(this._resolve, this._reject);\n      return;\n    }\n\n    this._state = state;\n    this._value = value;\n\n    this._executeHandlers();\n  };\n\n  /** JSDoc */\n  private readonly _executeHandlers = () => {\n    if (this._state === States.PENDING) {\n      return;\n    }\n\n    const cachedHandlers = this._handlers.slice();\n    this._handlers = [];\n\n    cachedHandlers.forEach(handler => {\n      if (handler[0]) {\n        return;\n      }\n\n      if (this._state === States.RESOLVED) {\n        handler[1](this._value as unknown as any);\n      }\n\n      if (this._state === States.REJECTED) {\n        handler[2](this._value);\n      }\n\n      handler[0] = true;\n    });\n  };\n}\n\nexport { SyncPromise };\n"],"names":[],"mappings":";;AAAA;AACA;AAEA;AACA;AACA,IAAkB,MAAA,CAAA,CAAA,CAAA,UAAA,MAAA,EAAA;AAClB;AACA,EAAE,MAAA,OAAA,GAAU,CAAC,CAAA,CAAA,MAAA,CAAA,MAAA,CAAA,SAAA,CAAA,GAAA,OAAA,CAAA,GAAA,SAAA,CAAA;AACb;AACA,EAAE,MAAA,QAAA,GAAW,CAAC,CAAA,CAAA,MAAA,CAAA,MAAA,CAAA,UAAA,CAAA,GAAA,QAAA,CAAA,GAAA,UAAA,CAAA;AACd;AACA,EAAE,MAAA,QAAA,GAAW,CAAC,CAAA,CAAA,MAAA,CAAA,MAAA,CAAA,UAAA,CAAA,GAAA,QAAA,CAAA,GAAA,UAAA,CAAA;AACd,CAAA,EAAA,MAAA,KAAA,MAAA,GAAA,EAAA,CAAA,CAAA,CAAA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,mBAAmB,CAAI,KAAK,EAAuC;AACnF,EAAE,OAAO,IAAI,WAAW,CAAC,WAAW;AACpC,IAAI,OAAO,CAAC,KAAK,CAAC,CAAA;AAClB,GAAG,CAAC,CAAA;AACJ,CAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,mBAAmB,CAAY,MAAM,EAAwB;AAC7E,EAAE,OAAO,IAAI,WAAW,CAAC,CAAC,CAAC,EAAE,MAAM,KAAK;AACxC,IAAI,MAAM,CAAC,MAAM,CAAC,CAAA;AAClB,GAAG,CAAC,CAAA;AACJ,CAAA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,WAAW,CAA8B;;AAK/C,GAAS,WAAW;AACpB,IAAI,QAAQ;AACZ,IAAI,CAAA,WAAA,CAAA,SAAA,CAAA,MAAA,CAAA,IAAA,CAAA,IAAA,CAAA,CAAA,WAAA,CA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