@actyx/sdk
Version:
Actyx SDK
54 lines • 2.19 kB
JavaScript
;
/*
* Actyx SDK: Functions for writing distributed apps
* deployed on peer-to-peer networks, without any servers.
*
* Copyright (C) 2021 Actyx AG
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("../../node_modules/rxjs");
const operators_1 = require("../../node_modules/rxjs/operators");
function logReconnectAttempt(delayMs, attempt, name) {
// Don't log during test runs
if (typeof jest !== 'undefined') {
return;
}
console.info('Connection issue %s, will retry in %d s (Attempt: %d).', name, delayMs / 1000, attempt);
}
const retry = (config) => (e) => {
const delayMs = (config && config.delayMs) || 1000;
const name = (config && config.name && ` connecting to ${config.name}`) || '';
const attempts = config && config.attempts;
if (attempts && attempts > 0) {
return e.pipe((0, operators_1.map)((v, i) => {
const attempt = i + 1;
if (attempt > attempts)
throw new Error(`Giving up after ${attempts} retries!`);
logReconnectAttempt(delayMs, attempt, name);
return v;
}), (0, operators_1.delay)(delayMs));
}
return e.pipe((0, operators_1.delay)(delayMs));
};
const exponentialBackoff = (config) => (e) => {
const minDelay = (config && config.minDelay) || 1000;
const maxDelay = (config && config.maxDelay) || 60000;
const attempts = (config && config.attempts) || 0;
const name = (config && config.name && ` connecting to ${config.name}`) || '';
return e.pipe((0, operators_1.mergeMap)((v, i) => {
const attempt = i + 1;
if (attempts > 0 && attempt > attempts) {
throw new Error(`Giving up after ${attempts} retries!`);
}
const delayMs = Math.min(minDelay * 2 ** (attempt - 1), maxDelay);
logReconnectAttempt(delayMs, attempt, name);
return (0, rxjs_1.of)(v).pipe((0, operators_1.delay)(delayMs));
}));
};
const reconnectStrategies = {
exponentialBackoff,
retry,
};
exports.default = reconnectStrategies;
//# sourceMappingURL=reconnectStrategies.js.map