test havenod 1.0.2, update dist

This commit is contained in:
woodser 2024-04-29 08:36:22 -04:00
parent d0da85167a
commit 0d51340033
7 changed files with 17 additions and 68 deletions

View file

@ -32,15 +32,17 @@ class TaskLooper {
/**
* Start the task loop.
*
* @param {int} periodInMs the loop period in milliseconds
* @param {number} periodInMs the loop period in milliseconds
* @param {boolean} targetFixedPeriod specifies if the task should target a fixed period by accounting for run time (default false)
* @return {TaskLooper} this instance for chaining
*/
start(periodInMs) {
start(periodInMs, targetFixedPeriod) {
if (periodInMs <= 0)
throw new Error("Looper period must be greater than 0 ms");
if (this._isStarted)
return;
this._isStarted = true;
this._runLoop(periodInMs);
this._runLoop(periodInMs, targetFixedPeriod);
}
/**
* Stop the task loop.
@ -52,13 +54,13 @@ class TaskLooper {
clearTimeout(this._timeout);
this._timeout = undefined;
}
async _runLoop(periodInMs) {
async _runLoop(periodInMs, targetFixedPeriod) {
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)); });
await new Promise((resolve) => { this._timeout = setTimeout(resolve, periodInMs - (targetFixedPeriod ? (Date.now() - startTime) : 0)); });
}
this._isLooping = false;
}