UNPKG

envio

Version:

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

62 lines (56 loc) 1.71 kB
// Generated by ReScript, PLEASE EDIT WITH CARE 'use strict'; var Pino = require("./bindings/Pino.res.js"); var Utils = require("./Utils.res.js"); var Caml_js_exceptions = require("rescript/lib/js/caml_js_exceptions.js"); function make(intervalMillis, logger) { return { lastRunTimeMillis: 0, isRunning: false, isAwaitingInterval: false, scheduled: undefined, intervalMillis: intervalMillis, logger: logger }; } async function startInternal(throttler) { var match = throttler.isRunning; if (match) { return ; } var match$1 = throttler.isAwaitingInterval; if (match$1) { return ; } var fn = throttler.scheduled; if (fn === undefined) { return ; } var timeSinceLastRun = Date.now() - throttler.lastRunTimeMillis; if (timeSinceLastRun >= throttler.intervalMillis) { throttler.isRunning = true; throttler.scheduled = undefined; throttler.lastRunTimeMillis = Date.now(); try { await fn(); } catch (raw_exn){ var exn = Caml_js_exceptions.internalToOCamlException(raw_exn); throttler.logger.error(Pino.createPinoMessageWithError("Scheduled action failed in throttler", Utils.prettifyExn(exn))); } throttler.isRunning = false; return await startInternal(throttler); } throttler.isAwaitingInterval = true; setTimeout((function () { throttler.isAwaitingInterval = false; startInternal(throttler); }), throttler.intervalMillis - timeSinceLastRun | 0); } function schedule(throttler, fn) { throttler.scheduled = fn; startInternal(throttler); } exports.make = make; exports.schedule = schedule; /* Pino Not a pure module */