Files
SnowLuma/.pnpm-store/v11/files/dc/6b42b302a5d585cb03ffe449a575b5bb4104fab07028ff3ccf98747a64999b326eef397ee2a26169b7e024ca2ec334d6bf53b3b4a513c370657e294b094e37
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

35 lines
911 B
Plaintext

import { IDelay } from "./delay.interface";
import { IBackOffOptions } from "../options";
import { JitterFactory } from "../jitter/jitter.factory";
export abstract class Delay implements IDelay {
protected attempt = 0;
constructor(private options: IBackOffOptions) {}
public apply() {
return new Promise(resolve => setTimeout(resolve, this.jitteredDelay));
}
public setAttemptNumber(attempt: number) {
this.attempt = attempt;
}
private get jitteredDelay() {
const jitter = JitterFactory(this.options);
return jitter(this.delay);
}
private get delay() {
const constant = this.options.startingDelay;
const base = this.options.timeMultiple;
const power = this.numOfDelayedAttempts;
const delay = constant * Math.pow(base, power);
return Math.min(delay, this.options.maxDelay);
}
protected get numOfDelayedAttempts() {
return this.attempt;
}
}