@daysnap/utils
Version:
44 lines (36 loc) • 1 kB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _class;
var _chunkXL3QSME6cjs = require('./chunk-XL3QSME6.cjs');
// src/poller.ts
var Poller = (_class = class {
__init() {this.count = 0}
__init2() {this.currentCount = -1}
get isRunning() {
return this.count === this.currentCount;
}
constructor(task, options = {}) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);
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 (e) {
} finally {
await _chunkXL3QSME6cjs.sleep.call(void 0, this.options.delay);
}
}
}
stop() {
this.count++;
return this;
}
start(forceUpdate = true) {
if (forceUpdate || !this.isRunning) {
this.run(++this.count);
}
return this;
}
}, _class);
exports.Poller = Poller;