mirror of
https://github.com/haveno-dex/haveno-ts.git
synced 2025-07-23 23:11:12 -04:00
commit dist
This commit is contained in:
parent
65868f21e5
commit
4b1565050e
22 changed files with 79754 additions and 1 deletions
47
dist/utils/TaskLooper.js
vendored
Normal file
47
dist/utils/TaskLooper.js
vendored
Normal file
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* Run a task in a fixed period loop.
|
||||
*/
|
||||
export default class TaskLooper {
|
||||
/**
|
||||
* Build the looper with a function to invoke on a fixed period loop.
|
||||
*
|
||||
* @param {function} fn - the async function to invoke
|
||||
*/
|
||||
constructor(fn) {
|
||||
this._fn = fn;
|
||||
this._isStarted = false;
|
||||
this._isLooping = false;
|
||||
}
|
||||
/**
|
||||
* Start the task loop.
|
||||
*
|
||||
* @param {int} periodInMs the loop period in milliseconds
|
||||
*/
|
||||
start(periodInMs) {
|
||||
if (this._isStarted)
|
||||
return;
|
||||
this._isStarted = true;
|
||||
this._runLoop(periodInMs);
|
||||
}
|
||||
/**
|
||||
* Stop the task loop.
|
||||
*/
|
||||
stop() {
|
||||
if (!this._isStarted)
|
||||
throw new Error("Cannot stop TaskLooper because it's not started");
|
||||
this._isStarted = false;
|
||||
clearTimeout(this._timeout);
|
||||
this._timeout = undefined;
|
||||
}
|
||||
async _runLoop(periodInMs) {
|
||||
this._isLooping = true;
|
||||
while (this._isStarted) {
|
||||
const startTime = Date.now();
|
||||
await this._fn();
|
||||
if (this._isStarted)
|
||||
await new Promise((resolve) => { this._timeout = setTimeout(resolve, periodInMs - (Date.now() - startTime)); });
|
||||
}
|
||||
this._isLooping = false;
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=TaskLooper.js.map
|
Loading…
Add table
Add a link
Reference in a new issue