UNPKG

@skyway-sdk/analytics-client

Version:

The official Next Generation JavaScript SDK for SkyWay

50 lines 1.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BackOff = void 0; class BackOff { /**20.4 sec {var sum=0;for(i=0;i<=8;i++){sum +=i ** 2 * 100}} */ constructor(props = {}) { this.count = 0; this.times = 8; /**ms */ this.interval = 100; /**ms */ this.jitter = 0; Object.assign(this, props); } wait() { return __awaiter(this, void 0, void 0, function* () { if (this.exceeded) { return false; } const timeout = this.timeout; this.count++; yield new Promise((r) => setTimeout(r, timeout)); return true; }); } get timeout() { const timeout = Math.pow(this.count, 2) * this.interval + Math.pow(this.count, 2) * this.jitter * Math.random(); return timeout; } get exceeded() { return this.count >= this.times; } reset() { this.count = 0; } stop() { this.count = this.times; } } exports.BackOff = BackOff; //# sourceMappingURL=backoff.js.map