UNPKG

@configurator/ravendb

Version:
121 lines 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PromiseStatusTracker = exports.AsyncTimeout = exports.wrapWithTimeout = exports.delay = exports.defer = exports.raceToResolution = void 0; const BluebirdPromise = require("bluebird"); const Exceptions_1 = require("../Exceptions"); const timers_1 = require("timers"); function raceToResolution(promises, onErrorCallback) { const indexPromises = promises.map((p, index) => p.catch(() => { throw index; })); return BluebirdPromise.race(indexPromises).catch(index => { const p = promises.splice(index, 1)[0]; p.catch(err => { if (onErrorCallback) { onErrorCallback(err); } }); return raceToResolution(promises); }); } exports.raceToResolution = raceToResolution; function defer() { let resolve; let reject; const promise = new BluebirdPromise(function (res, rej) { resolve = res; reject = rej; }); return { resolve, reject, promise }; } exports.defer = defer; async function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } exports.delay = delay; async function wrapWithTimeout(promise, ms) { let timeoutHandle; const error = (0, Exceptions_1.getError)("TimeoutException", `Timeout after ${ms} ms.`); const timeoutPromise = new Promise((resolve) => { timeoutHandle = setTimeout(() => { resolve(error); }, ms); }); try { const raceWinner = await Promise.race([promise, timeoutPromise]); if (raceWinner === error) { throw raceWinner; } else { (0, timers_1.clearTimeout)(timeoutHandle); return raceWinner; } } catch (e) { (0, timers_1.clearTimeout)(timeoutHandle); throw e; } } exports.wrapWithTimeout = wrapWithTimeout; class AsyncTimeout { get promise() { return this._promise; } get timedOut() { return this._timedOut; } constructor(ms, op) { this._timedOut = false; this._op = op; this._promise = new Promise((resolve, reject) => { this._resolve = resolve; this._reject = reject; }); this._timer = setTimeout(() => { this._timedOut = true; this._reject(this._getTimeoutError(ms)); }, ms); } _getTimeoutError(ms) { const opText = this._op ? `Operation '${this._op}'` : `Operation`; const timeoutError = (0, Exceptions_1.getError)("TimeoutError", `${opText} timed out after ${ms} ms.`); return timeoutError; } cancel() { if (this._timer) { (0, timers_1.clearTimeout)(this._timer); } this._resolve(); } } exports.AsyncTimeout = AsyncTimeout; class PromiseStatusTracker { constructor(promise) { if (!promise) { throw new Error("Promise to track cannot be null."); } this._status = "PENDING"; this._promise = promise; this._promise .then(() => this._status = "RESOLVED") .catch(() => this._status = "REJECTED"); } static track(promise) { return new PromiseStatusTracker(promise); } isFullfilled() { return this._status === "REJECTED" || this._status === "RESOLVED"; } isResolved() { return this._status === "RESOLVED"; } isRejected() { return this._status === "REJECTED"; } } exports.PromiseStatusTracker = PromiseStatusTracker; //# sourceMappingURL=PromiseUtil.js.map