@metamask/multichain-account-service
Version:
Service to manage multichain accounts
88 lines • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withTimeout = exports.withRetry = exports.isKeyringControllerLockedError = exports.isTimeoutError = exports.TimeoutError = void 0;
const keyring_controller_1 = require("@metamask/keyring-controller");
/** Timeout error. */
class TimeoutError extends Error {
constructor(message) {
super(message);
this.name = 'TimeoutError';
}
}
exports.TimeoutError = TimeoutError;
/**
* Check if an error is a `TimeoutError`.
*
* @param error - The error to check.
* @returns `true` if the error is a `TimeoutError`, otherwise `false`.
*/
function isTimeoutError(error) {
return error instanceof TimeoutError;
}
exports.isTimeoutError = isTimeoutError;
/**
* Check if an error is a `KeyringControllerLockedError`.
*
* @param error - The error to check.
* @returns `true` if the error is a `KeyringControllerLockedError`, otherwise `false`.
*/
function isKeyringControllerLockedError(error) {
return (error instanceof keyring_controller_1.KeyringControllerError &&
error.message === keyring_controller_1.KeyringControllerErrorMessage.ControllerLocked);
}
exports.isKeyringControllerLockedError = isKeyringControllerLockedError;
/**
* Execute a function with exponential backoff on transient failures.
*
* @param fnToExecute - The function to execute.
* @param options - The options for the retry.
* @param options.maxAttempts - The maximum number of attempts.
* @param options.backOffMs - The backoff in milliseconds.
* @throws An error if the transaction count cannot be retrieved.
* @returns The result of the function.
*/
async function withRetry(fnToExecute, { maxAttempts = 3, backOffMs = 500, } = {}) {
let lastError;
let backOff = backOffMs;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fnToExecute();
}
catch (error) {
lastError = error;
if (attempt >= maxAttempts) {
break;
}
const delay = backOff;
await new Promise((resolve) => setTimeout(resolve, delay));
backOff *= 2;
}
}
throw lastError;
}
exports.withRetry = withRetry;
/**
* Execute a promise with a timeout.
*
* @param fn - A callback that returns the promise to execute.
* @param timeoutMs - The timeout in milliseconds.
* @returns The result of the promise.
*/
async function withTimeout(fn, timeoutMs = 500) {
let timer;
try {
return await Promise.race([
fn(),
new Promise((_resolve, reject) => {
timer = setTimeout(() => reject(new TimeoutError(`Timed out after: ${timeoutMs}ms`)), timeoutMs);
}),
]);
}
finally {
if (timer) {
clearTimeout(timer);
}
}
}
exports.withTimeout = withTimeout;
//# sourceMappingURL=utils.cjs.map