@daysnap/utils
Version:
44 lines (41 loc) • 756 B
JavaScript
import {
sleep
} from "./chunk-TNIIDDN4.js";
// src/poller.ts
var Poller = class {
count = 0;
task;
options;
currentCount = -1;
get isRunning() {
return this.count === this.currentCount;
}
constructor(task, options = {}) {
this.task = task;
this.options = Object.assign({ delay: 1e3 }, options);
}
async run(count) {
this.currentCount = count;
while (this.count === count) {
try {
await this.task(this);
} catch {
} finally {
await sleep(this.options.delay);
}
}
}
stop() {
this.count++;
return this;
}
start(forceUpdate = true) {
if (forceUpdate || !this.isRunning) {
this.run(++this.count);
}
return this;
}
};
export {
Poller
};