cockatiel
Version:
A resilience and transient-fault-handling library that allows developers to express policies such as Backoff, Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. Inspired by .NET Polly.
30 lines • 901 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DelegateBackoff = void 0;
class DelegateBackoff {
/**
* Backoff that delegates to a user-provided function. The function takes
* the backoff context, and can optionally take (and return) a state value
* that will be passed into subsequent backoff requests.
*/
constructor(fn) {
this.fn = fn;
}
/**
* @inheritdoc
*/
next(context) {
return instance(this.fn).next(context);
}
}
exports.DelegateBackoff = DelegateBackoff;
const instance = (fn, state, current = 0) => ({
duration: current,
next(context) {
const result = fn(context, state);
return typeof result === 'number'
? instance(fn, state, result)
: instance(fn, result.state, result.delay);
},
});
//# sourceMappingURL=DelegateBackoff.js.map