@bigmi/core
Version:
TypeScript library for Bitcoin apps.
30 lines • 861 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.poll = poll;
const sleep_js_1 = require("./sleep.js");
function poll(fn, { emitOnBegin, initialWaitTime, interval }) {
let active = true;
const unwatch = () => {
active = false;
};
const watch = async () => {
let data;
if (emitOnBegin) {
data = await fn({ unpoll: unwatch });
}
const initialWait = (await initialWaitTime?.(data)) ?? interval;
await (0, sleep_js_1.sleep)(initialWait);
const poll = async () => {
if (!active) {
return;
}
await fn({ unpoll: unwatch });
await (0, sleep_js_1.sleep)(interval);
poll();
};
poll();
};
watch();
return unwatch;
}
//# sourceMappingURL=poll.js.map