UNPKG

envio

Version:

A latency and sync speed optimized, developer friendly blockchain data indexer.

67 lines (61 loc) 1.73 kB
// Generated by ReScript, PLEASE EDIT WITH CARE import * as Pino from "./bindings/Pino.res.mjs"; import * as Utils from "./Utils.res.mjs"; import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js"; function make(intervalMillis, logger) { return { lastRunTimeMillis: 0, isRunning: false, isAwaitingInterval: false, scheduled: undefined, intervalMillis: intervalMillis, logger: logger }; } function startInternal(throttler) { let match = throttler.isRunning; if (match) { return; } let match$1 = throttler.isAwaitingInterval; if (match$1) { return; } let fn = throttler.scheduled; if (fn === undefined) { return; } let timeSinceLastRun = Date.now() - throttler.lastRunTimeMillis; if (timeSinceLastRun >= throttler.intervalMillis) { throttler.isRunning = true; throttler.scheduled = undefined; throttler.lastRunTimeMillis = Date.now(); setImmediate(() => { (async () => { try { await fn(); } catch (raw_exn) { let exn = Primitive_exceptions.internalToException(raw_exn); throttler.logger.error(Pino.createPinoMessageWithError("Scheduled action failed in throttler", Utils.prettifyExn(exn))); } throttler.isRunning = false; return startInternal(throttler); })(); }); } else { throttler.isAwaitingInterval = true; setTimeout(() => { throttler.isAwaitingInterval = false; startInternal(throttler); }, throttler.intervalMillis - timeSinceLastRun | 0); } } function schedule(throttler, fn) { throttler.scheduled = fn; startInternal(throttler); } export { make, schedule, } /* Pino Not a pure module */