2020-10-16 17:26:19 +02:00
|
|
|
// On mobile all the setTimeout and setInterval should go through this class
|
|
|
|
// as it will either use the native timeout/interval for short intervals or
|
|
|
|
// the custom one for long intervals.
|
|
|
|
|
|
|
|
// For custom intervals, they are triggered
|
|
|
|
// whenever the update() function is called, and in mobile it's called for
|
|
|
|
// example on the Redux action middleware or when the app gets focus.
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
import time from './time';
|
2020-10-16 17:26:19 +02:00
|
|
|
|
|
|
|
type IntervalId = number;
|
|
|
|
|
|
|
|
interface Interval {
|
2020-11-12 21:29:22 +02:00
|
|
|
id: IntervalId;
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
callback: Function;
|
|
|
|
interval: number;
|
|
|
|
lastIntervalTime: number;
|
|
|
|
isTimeout: boolean;
|
2020-10-16 17:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Intervals {
|
|
|
|
[key: number]: Interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class PoorManIntervals {
|
|
|
|
|
|
|
|
private static maxNativeTimerDuration_ = 10 * 1000;
|
2023-06-30 10:07:03 +02:00
|
|
|
private static lastUpdateTime_ = 0;
|
2020-11-12 21:13:28 +02:00
|
|
|
private static intervalId_: IntervalId = 0;
|
|
|
|
private static intervals_: Intervals = {};
|
2020-10-16 17:26:19 +02:00
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
public static setInterval(callback: Function, interval: number): IntervalId {
|
2020-10-16 17:26:19 +02:00
|
|
|
if (interval <= this.maxNativeTimerDuration_) return setInterval(callback, interval);
|
|
|
|
|
|
|
|
this.intervalId_++;
|
|
|
|
const id = this.intervalId_;
|
|
|
|
|
|
|
|
this.intervals_[id] = {
|
|
|
|
id: id,
|
|
|
|
callback: callback,
|
|
|
|
interval: interval,
|
|
|
|
lastIntervalTime: time.unixMs(),
|
|
|
|
isTimeout: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
public static setTimeout(callback: Function, interval: number): IntervalId {
|
2020-10-16 17:26:19 +02:00
|
|
|
if (interval <= this.maxNativeTimerDuration_) return setTimeout(callback, interval);
|
|
|
|
|
|
|
|
this.intervalId_++;
|
|
|
|
const id = this.intervalId_;
|
|
|
|
|
|
|
|
this.intervals_[id] = {
|
|
|
|
id: id,
|
|
|
|
callback: callback,
|
|
|
|
interval: interval,
|
|
|
|
lastIntervalTime: time.unixMs(),
|
|
|
|
isTimeout: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public static clearInterval(id: IntervalId) {
|
2020-10-16 17:26:19 +02:00
|
|
|
const r = this.intervals_[id];
|
|
|
|
if (!r) {
|
|
|
|
clearInterval(id);
|
|
|
|
} else {
|
|
|
|
delete this.intervals_[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public static clearTimeout(id: IntervalId) {
|
2020-10-16 17:26:19 +02:00
|
|
|
const r = this.intervals_[id];
|
|
|
|
if (!r) {
|
|
|
|
clearTimeout(id);
|
|
|
|
} else {
|
|
|
|
delete this.intervals_[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static update() {
|
|
|
|
// Don't update more than once a second
|
|
|
|
if (this.lastUpdateTime_ + 1000 > time.unixMs()) return;
|
|
|
|
|
|
|
|
for (const id in this.intervals_) {
|
|
|
|
const interval = this.intervals_[id];
|
|
|
|
const now = time.unixMs();
|
|
|
|
if (now - interval.lastIntervalTime >= interval.interval) {
|
|
|
|
interval.lastIntervalTime = now;
|
|
|
|
interval.callback();
|
|
|
|
if (interval.isTimeout) {
|
|
|
|
this.clearTimeout(interval.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastUpdateTime_ = time.unixMs();
|
|
|
|
}
|
|
|
|
}
|