salsify-experiences-sdk
Version:
SDK to be used by commerce websites to implement product experiences.
32 lines • 902 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const MAX_TIMEOUT = 300000;
/*
* Executes a callback with exponential backoff. The timer goes off at
* 5, 20, 45, 90, ... seconds, up until 5 minutes.
*/
class Timeout {
#counter = 0;
#timeout = null;
clear() {
if (this.#timeout) {
clearTimeout(this.#timeout);
this.#counter = 0;
this.#timeout = null;
}
}
start(callback) {
if (this.#timeout)
clearTimeout(this.#timeout);
this.#timeout = this.#setup(callback);
}
#setup(callback) {
return setTimeout(() => {
callback();
this.#counter++;
this.#timeout = this.#setup(callback);
}, Math.min(5000 * Math.pow(this.#counter + 1, 2), MAX_TIMEOUT));
}
}
exports.default = Timeout;
//# sourceMappingURL=timeout.js.map