2017-11-03 02:09:34 +02:00
|
|
|
const { time } = require('lib/time-utils.js');
|
2017-07-22 19:35:39 +02:00
|
|
|
|
|
|
|
class PoorManIntervals {
|
|
|
|
static setInterval(callback, interval) {
|
|
|
|
PoorManIntervals.intervalId_++;
|
|
|
|
|
|
|
|
PoorManIntervals.intervals_.push({
|
|
|
|
id: PoorManIntervals.intervalId_,
|
|
|
|
callback: callback,
|
|
|
|
interval: interval,
|
|
|
|
lastIntervalTime: time.unixMs(),
|
|
|
|
});
|
|
|
|
|
|
|
|
return PoorManIntervals.intervalId_;
|
|
|
|
}
|
|
|
|
|
2017-11-28 20:58:04 +02:00
|
|
|
static setTimeout(callback, interval) {
|
|
|
|
PoorManIntervals.intervalId_++;
|
|
|
|
|
|
|
|
PoorManIntervals.intervals_.push({
|
|
|
|
id: PoorManIntervals.intervalId_,
|
|
|
|
callback: callback,
|
|
|
|
interval: interval,
|
|
|
|
lastIntervalTime: time.unixMs(),
|
|
|
|
oneOff: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
return PoorManIntervals.intervalId_;
|
|
|
|
}
|
|
|
|
|
2017-07-22 19:35:39 +02:00
|
|
|
static intervalById(id) {
|
|
|
|
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
|
|
|
|
if (PoorManIntervals.intervals_[i].id == id) return PoorManIntervals.intervals_[id];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static clearInterval(id) {
|
|
|
|
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
|
|
|
|
if (PoorManIntervals.intervals_[i].id == id) {
|
|
|
|
PoorManIntervals.intervals_.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static update() {
|
2017-07-25 20:41:53 +02:00
|
|
|
// Don't update more than once a second
|
|
|
|
if (PoorManIntervals.lastUpdateTime_ + 1000 > time.unixMs()) return;
|
|
|
|
|
2017-07-22 19:35:39 +02:00
|
|
|
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const interval = PoorManIntervals.intervals_[i];
|
2017-07-22 19:35:39 +02:00
|
|
|
const now = time.unixMs();
|
|
|
|
if (now - interval.lastIntervalTime >= interval.interval) {
|
|
|
|
interval.lastIntervalTime = now;
|
|
|
|
interval.callback();
|
2017-11-28 20:58:04 +02:00
|
|
|
if (interval.oneOff) {
|
|
|
|
this.clearInterval(interval.id);
|
|
|
|
}
|
2017-07-22 19:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 20:41:53 +02:00
|
|
|
|
|
|
|
PoorManIntervals.lastUpdateTime_ = time.unixMs();
|
2017-07-22 19:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 20:41:53 +02:00
|
|
|
PoorManIntervals.lastUpdateTime_ = 0;
|
2017-07-22 19:35:39 +02:00
|
|
|
PoorManIntervals.intervalId_ = 0;
|
|
|
|
PoorManIntervals.intervals_ = [];
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { PoorManIntervals };
|