@aptpod/iscp-ts
Version:
iSCP 2.0 client library for TypeScript
94 lines • 5.66 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Jitter_ratioCreator, _Jitter_min, _Jitter_max, _ExponentialBackoff_run, _ExponentialBackoff_signal, _ExponentialBackoff_baseTimeoutMs, _ExponentialBackoff_maxTimeoutMs, _ExponentialBackoff_numOfAttempts, _ExponentialBackoff_jitter;
import { AbortSignalListener } from '../signal-abort-listener';
import { Timeout } from '../timeout';
export class AbortedError extends Error {
}
export class Jitter {
constructor(config) {
_Jitter_ratioCreator.set(this, void 0);
_Jitter_min.set(this, void 0);
_Jitter_max.set(this, void 0);
__classPrivateFieldSet(this, _Jitter_ratioCreator, config.ratioCreator, "f");
__classPrivateFieldSet(this, _Jitter_min, config.min, "f");
__classPrivateFieldSet(this, _Jitter_max, config.max, "f");
}
create() {
const ratio = __classPrivateFieldGet(this, _Jitter_ratioCreator, "f").call(this);
const len = __classPrivateFieldGet(this, _Jitter_max, "f") - __classPrivateFieldGet(this, _Jitter_min, "f");
return __classPrivateFieldGet(this, _Jitter_min, "f") + ratio * len;
}
}
_Jitter_ratioCreator = new WeakMap(), _Jitter_min = new WeakMap(), _Jitter_max = new WeakMap();
export class ExponentialBackoff {
constructor(config) {
_ExponentialBackoff_run.set(this, void 0);
_ExponentialBackoff_signal.set(this, void 0);
_ExponentialBackoff_baseTimeoutMs.set(this, void 0);
_ExponentialBackoff_maxTimeoutMs.set(this, void 0);
_ExponentialBackoff_numOfAttempts.set(this, void 0);
_ExponentialBackoff_jitter.set(this, void 0);
__classPrivateFieldSet(this, _ExponentialBackoff_run, config.run, "f");
__classPrivateFieldSet(this, _ExponentialBackoff_signal, config.signal, "f");
__classPrivateFieldSet(this, _ExponentialBackoff_baseTimeoutMs, config.baseTimeoutMs, "f");
__classPrivateFieldSet(this, _ExponentialBackoff_maxTimeoutMs, config.maxTimeoutMs, "f");
__classPrivateFieldSet(this, _ExponentialBackoff_numOfAttempts, config.numOfAttempts, "f");
__classPrivateFieldSet(this, _ExponentialBackoff_jitter, config.jitter, "f");
this.onBeforeRetry = () => { };
}
async start() {
let aborted = false;
__classPrivateFieldGet(this, _ExponentialBackoff_signal, "f").addEventListener('abort', () => {
aborted = true;
}, { once: true });
let error = new Error('initial error');
const timeout = new Timeout();
const abortSignalListener = new AbortSignalListener({ signal: __classPrivateFieldGet(this, _ExponentialBackoff_signal, "f") });
for (let retryCount = 0; retryCount <= __classPrivateFieldGet(this, _ExponentialBackoff_numOfAttempts, "f"); retryCount++) {
if (aborted) {
error = new AbortedError();
break;
}
if (retryCount > 0) {
const jitter = __classPrivateFieldGet(this, _ExponentialBackoff_jitter, "f").create();
const timeoutMsExponential = Math.pow(2, retryCount - 1) * __classPrivateFieldGet(this, _ExponentialBackoff_baseTimeoutMs, "f");
const timeoutMsWithJitter = timeoutMsExponential * jitter;
const timeoutMs = Math.min(timeoutMsWithJitter, __classPrivateFieldGet(this, _ExponentialBackoff_maxTimeoutMs, "f"));
this.onBeforeRetry({
retryCount,
timeoutMs,
error,
});
const promiseTimeout = timeout.run(undefined, timeoutMs);
const promiseAbort = abortSignalListener.createPromise(undefined);
await Promise.race([promiseTimeout, promiseAbort.resolve()]).finally(() => {
timeout.cancel();
promiseAbort.cancel();
});
}
if (aborted) {
error = new AbortedError();
break;
}
try {
return await __classPrivateFieldGet(this, _ExponentialBackoff_run, "f").call(this);
}
catch (error_) {
error = error_ instanceof Error ? error_ : new Error(String(error_));
}
}
return error;
}
}
_ExponentialBackoff_run = new WeakMap(), _ExponentialBackoff_signal = new WeakMap(), _ExponentialBackoff_baseTimeoutMs = new WeakMap(), _ExponentialBackoff_maxTimeoutMs = new WeakMap(), _ExponentialBackoff_numOfAttempts = new WeakMap(), _ExponentialBackoff_jitter = new WeakMap();
//# sourceMappingURL=index.js.map