wallet-core/packages/taler-wallet-core/src/util/timer.ts

201 lines
4.3 KiB
TypeScript
Raw Normal View History

2017-05-27 22:56:35 +02:00
/*
This file is part of GNU Taler
(C) 2017-2019 Taler Systems S.A.
2017-05-27 22:56:35 +02:00
GNU Taler is free software; you can redistribute it and/or modify it under the
2017-05-27 22:56:35 +02:00
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
2017-05-27 22:56:35 +02:00
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
2017-05-27 22:56:35 +02:00
*/
/**
* Cross-platform timers.
*
* NodeJS and the browser use slightly different timer API,
* this abstracts over these differences.
*/
2020-04-06 21:53:29 +02:00
/**
* Imports.
*/
2021-06-08 20:58:13 +02:00
import { Logger, Duration } from "@gnu-taler/taler-util";
const logger = new Logger("timer.ts");
2020-04-06 21:53:29 +02:00
2017-05-27 22:56:35 +02:00
/**
* Cancelable timer.
*/
export interface TimerHandle {
clear(): void;
/**
* Make sure the event loop exits when the timer is the
* only event left. Has no effect in the browser.
*/
unref(): void;
2017-05-27 22:56:35 +02:00
}
class IntervalHandle {
2020-03-30 12:39:32 +02:00
constructor(public h: any) {}
2017-05-27 22:56:35 +02:00
2020-04-06 21:53:29 +02:00
clear(): void {
2019-07-31 01:33:56 +02:00
clearInterval(this.h);
2017-05-27 22:56:35 +02:00
}
/**
* Make sure the event loop exits when the timer is the
* only event left. Has no effect in the browser.
*/
unref(): void {
if (typeof this.h === "object") {
this.h.unref();
}
}
2017-05-27 22:56:35 +02:00
}
class TimeoutHandle {
2020-03-30 12:39:32 +02:00
constructor(public h: any) {}
2017-05-27 22:56:35 +02:00
2020-04-06 21:53:29 +02:00
clear(): void {
2017-05-27 22:56:35 +02:00
clearTimeout(this.h);
}
/**
* Make sure the event loop exits when the timer is the
* only event left. Has no effect in the browser.
*/
unref(): void {
if (typeof this.h === "object") {
this.h.unref();
}
}
2017-05-27 22:56:35 +02:00
}
/**
* Get a performance counter in milliseconds.
*/
2018-09-20 02:56:13 +02:00
export const performanceNow: () => number = (() => {
// @ts-ignore
2017-05-30 14:38:29 +02:00
if (typeof process !== "undefined" && process.hrtime) {
2017-05-27 22:56:35 +02:00
return () => {
const t = process.hrtime();
return t[0] * 1e9 + t[1];
2017-05-28 01:10:54 +02:00
};
}
// @ts-ignore
if (typeof performance !== "undefined") {
// @ts-ignore
2017-05-27 22:56:35 +02:00
return () => performance.now();
}
return () => 0;
2017-05-27 22:56:35 +02:00
})();
/**
* Call a function every time the delay given in milliseconds passes.
*/
export function every(delayMs: number, callback: () => void): TimerHandle {
return new IntervalHandle(setInterval(callback, delayMs));
}
/**
* Call a function after the delay given in milliseconds passes.
*/
export function after(delayMs: number, callback: () => void): TimerHandle {
2017-06-06 13:12:24 +02:00
return new TimeoutHandle(setTimeout(callback, delayMs));
2017-05-27 22:56:35 +02:00
}
2017-06-05 02:00:03 +02:00
const nullTimerHandle = {
clear() {
2017-10-15 19:28:35 +02:00
// do nothing
return;
},
unref() {
// do nothing
return;
2020-08-12 09:11:00 +02:00
},
2017-06-05 02:00:03 +02:00
};
/**
* Group of timers that can be destroyed at once.
*/
export class TimerGroup {
2020-04-06 17:45:41 +02:00
private stopped = false;
2017-06-05 02:00:03 +02:00
private timerMap: { [index: number]: TimerHandle } = {};
private idGen = 1;
2020-04-06 21:53:29 +02:00
stopCurrentAndFutureTimers(): void {
2017-06-05 02:00:03 +02:00
this.stopped = true;
for (const x in this.timerMap) {
if (!this.timerMap.hasOwnProperty(x)) {
continue;
}
this.timerMap[x].clear();
delete this.timerMap[x];
}
}
resolveAfter(delayMs: Duration): Promise<void> {
2019-12-02 17:35:47 +01:00
return new Promise<void>((resolve, reject) => {
if (delayMs.d_ms !== "forever") {
this.after(delayMs.d_ms, () => {
resolve();
});
}
2019-12-02 17:35:47 +01:00
});
}
2017-06-05 02:00:03 +02:00
after(delayMs: number, callback: () => void): TimerHandle {
if (this.stopped) {
logger.warn("dropping timer since timer group is stopped");
2017-06-05 02:00:03 +02:00
return nullTimerHandle;
}
const h = after(delayMs, callback);
2017-10-15 19:28:35 +02:00
const myId = this.idGen++;
2017-06-05 02:00:03 +02:00
this.timerMap[myId] = h;
const tm = this.timerMap;
return {
clear() {
h.clear();
delete tm[myId];
},
unref() {
h.unref();
2020-08-12 09:11:00 +02:00
},
2017-06-05 02:00:03 +02:00
};
}
every(delayMs: number, callback: () => void): TimerHandle {
if (this.stopped) {
logger.warn("dropping timer since timer group is stopped");
2017-06-05 02:00:03 +02:00
return nullTimerHandle;
}
const h = every(delayMs, callback);
2017-10-15 19:28:35 +02:00
const myId = this.idGen++;
2017-06-05 02:00:03 +02:00
this.timerMap[myId] = h;
const tm = this.timerMap;
return {
clear() {
h.clear();
delete tm[myId];
},
unref() {
h.unref();
2020-08-12 09:11:00 +02:00
},
2017-06-05 02:00:03 +02:00
};
}
}