UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

29 lines 979 B
/** * @module Async */ import { TimeSpan } from "../../../utilities/_module-exports.js"; import { withJitter } from "../../../async/backof-policies/_shared.js"; /** * Linear backoff policy with jitter * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group BackoffPolicies */ export function linearBackoffPolicy(settings = {}) { return (attempt, error) => { if (typeof settings === "function") { settings = settings(error); } let { maxDelay = 6000, minDelay = 1_000 } = settings; if (maxDelay instanceof TimeSpan) { maxDelay = maxDelay.toMilliseconds(); } if (minDelay instanceof TimeSpan) { minDelay = minDelay.toMilliseconds(); } const { jitter = 0.5, _mathRandom = Math.random } = settings; const linear = Math.min(maxDelay, minDelay * attempt); return withJitter(jitter, linear, _mathRandom); }; } //# sourceMappingURL=linear-backoff-policy.js.map