From 108d5b0b0a7ab1f11b85a070f8b8c2dca1be7e52 Mon Sep 17 00:00:00 2001 From: Emilio Almansi Date: Mon, 29 Mar 2021 15:47:53 +0200 Subject: [PATCH] Updated docs --- docs/SetIntervalAsyncError.html | 4 +- docs/SetIntervalAsyncTimer.html | 4 +- docs/clear.js.html | 37 +++++++-------- docs/dynamic.js.html | 72 ++++++++++++++++++++--------- docs/error.js.html | 4 +- docs/fixed.js.html | 68 +++++++++++++++++++-------- docs/global.html | 80 +++----------------------------- docs/index.html | 4 +- docs/legacy.js.html | 82 ++++++++++++++++++++++----------- docs/timer.js.html | 5 +- 10 files changed, 189 insertions(+), 171 deletions(-) diff --git a/docs/SetIntervalAsyncError.html b/docs/SetIntervalAsyncError.html index bde44bc..a88e205 100644 --- a/docs/SetIntervalAsyncError.html +++ b/docs/SetIntervalAsyncError.html @@ -155,13 +155,13 @@


- Documentation generated by JSDoc 3.6.5 on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time) + Documentation generated by JSDoc 3.6.5 on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
diff --git a/docs/SetIntervalAsyncTimer.html b/docs/SetIntervalAsyncTimer.html index 19ebf24..dc2d33f 100644 --- a/docs/SetIntervalAsyncTimer.html +++ b/docs/SetIntervalAsyncTimer.html @@ -156,13 +156,13 @@


- Documentation generated by JSDoc 3.6.5 on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time) + Documentation generated by JSDoc 3.6.5 on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
diff --git a/docs/clear.js.html b/docs/clear.js.html index 4a5cdaa..8020ec6 100644 --- a/docs/clear.js.html +++ b/docs/clear.js.html @@ -27,12 +27,12 @@

Source: clear.js

/**
- * Copyright (c) 2019 Emilio Almansi. All rights reserved.
+ * Copyright (c) 2019-2021 Emilio Almansi. All rights reserved.
  * This work is licensed under the terms of the MIT license.
  * For a copy, see the file LICENSE in the root directory.
  */
 
-const MAX_INTERVAL_MS = Math.pow(2, 31) - 1
+import { validateTimer } from './validation'
 
 /**
  * Stops an execution cycle started by setIntervalAsync.<br>
@@ -43,25 +43,22 @@ 

Source: clear.js

* @returns {Promise} * A promise which resolves when all pending executions have finished. */ -async function clearIntervalAsync (timer) { +export async function clearIntervalAsync (timer) { + validateTimer(timer) timer.stopped = true - for (const timeout of Object.values(timer.timeouts)) { - clearTimeout(timeout) + for (const iterationId in timer.timeouts) { + clearTimeout(timer.timeouts[iterationId]) + delete timer.timeouts[iterationId] + } + for (const iterationId in timer.promises) { + try { + await timer.promises[iterationId] + } catch (_) { + // Do nothing. + } + delete timer.promises[iterationId] } - const noop = () => {} - const promises = Object - .values(timer.promises) - .map( - (promise) => { - promise.catch(noop) - } - ) - const noopInterval = setInterval(noop, MAX_INTERVAL_MS) - await Promise.all(promises) - clearInterval(noopInterval) } - -export { clearIntervalAsync }
@@ -72,13 +69,13 @@

Source: clear.js


diff --git a/docs/dynamic.js.html b/docs/dynamic.js.html index 2fcabe6..f0cb01e 100644 --- a/docs/dynamic.js.html +++ b/docs/dynamic.js.html @@ -27,15 +27,16 @@

Source: dynamic.js

/**
- * Copyright (c) 2019 Emilio Almansi. All rights reserved.
+ * Copyright (c) 2019-2021 Emilio Almansi. All rights reserved.
  * This work is licensed under the terms of the MIT license.
  * For a copy, see the file LICENSE in the root directory.
  */
 
 import { clearIntervalAsync } from './clear'
-import { validateHandler, validateInterval } from './validation'
 import SetIntervalAsyncError from './error'
 import SetIntervalAsyncTimer from './timer'
+import { getNextIterationId, noop } from './util'
+import { validateHandler, validateInterval } from './validation'
 
 /**
  * Attempts to execute the given handler at regular intervals, while preventing<br>
@@ -57,11 +58,12 @@ 

Source: dynamic.js

validateHandler(handler) validateInterval(interval) const timer = new SetIntervalAsyncTimer() - const id = timer.id - timer.timeouts[id] = setTimeout( + const iterationId = 0 + timer.timeouts[iterationId] = setTimeout( timeoutHandler, interval, timer, + iterationId, handler, interval, ...args @@ -69,34 +71,62 @@

Source: dynamic.js

return timer } -function timeoutHandler (timer, handler, interval, ...args) { - const id = timer.id - timer.promises[id] = (async () => { - const startTime = new Date() - try { - await handler(...args) - } catch (err) { - console.error(err) - } - const endTime = new Date() +/** + * @private + * + * @param {SetIntervalAsyncTimer} timer + * @param {number} iterationId + * @param {function} handler + * @param {number} interval + * @param {...*} args + */ +function timeoutHandler (timer, iterationId, handler, interval, ...args) { + delete timer.timeouts[iterationId] + timer.promises[iterationId] = runHandler( + timer, + iterationId, + handler, + interval, + ...args + ) +} + +/** + * @private + * + * @param {SetIntervalAsyncTimer} timer + * @param {number} iterationId + * @param {function} handler + * @param {number} interval + * @param {...*} args + */ +async function runHandler (timer, iterationId, handler, interval, ...args) { + // The next line ensures that timer.promises[iterationId] is set + // before running the handler. + await noop() + const startTime = new Date() + try { + await handler(...args) + } finally { if (!timer.stopped) { + const endTime = new Date() const executionTime = endTime - startTime const timeout = interval > executionTime ? interval - executionTime : 0 - timer.timeouts[id + 1] = setTimeout( + const nextIterationId = getNextIterationId(iterationId) + timer.timeouts[nextIterationId] = setTimeout( timeoutHandler, timeout, timer, + nextIterationId, handler, interval, ...args ) } - delete timer.timeouts[id] - delete timer.promises[id] - })() - timer.id = id + 1 + delete timer.promises[iterationId] + } } export { setIntervalAsync, clearIntervalAsync, SetIntervalAsyncTimer, SetIntervalAsyncError } @@ -110,13 +140,13 @@

Source: dynamic.js


- Documentation generated by JSDoc 3.6.5 on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time) + Documentation generated by JSDoc 3.6.5 on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
diff --git a/docs/error.js.html b/docs/error.js.html index e567e90..13a9fc7 100644 --- a/docs/error.js.html +++ b/docs/error.js.html @@ -57,13 +57,13 @@

Source: error.js


- Documentation generated by JSDoc 3.6.5 on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time) + Documentation generated by JSDoc 3.6.5 on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
diff --git a/docs/fixed.js.html b/docs/fixed.js.html index a8dbbb1..68d5ded 100644 --- a/docs/fixed.js.html +++ b/docs/fixed.js.html @@ -27,15 +27,16 @@

Source: fixed.js

/**
- * Copyright (c) 2019 Emilio Almansi. All rights reserved.
+ * Copyright (c) 2019-2021 Emilio Almansi. All rights reserved.
  * This work is licensed under the terms of the MIT license.
  * For a copy, see the file LICENSE in the root directory.
  */
 
 import { clearIntervalAsync } from './clear'
-import { validateHandler, validateInterval } from './validation'
 import SetIntervalAsyncError from './error'
 import SetIntervalAsyncTimer from './timer'
+import { getNextIterationId, noop } from './util'
+import { validateHandler, validateInterval } from './validation'
 
 /**
  * Executes the given handler at fixed intervals, while preventing<br>
@@ -57,11 +58,12 @@ 

Source: fixed.js

validateHandler(handler) validateInterval(interval) const timer = new SetIntervalAsyncTimer() - const id = timer.id - timer.timeouts[id] = setTimeout( + const iterationId = 0 + timer.timeouts[iterationId] = setTimeout( timeoutHandler, interval, timer, + iterationId, handler, interval, ...args @@ -69,28 +71,56 @@

Source: fixed.js

return timer } -function timeoutHandler (timer, handler, interval, ...args) { - const id = timer.id - timer.promises[id] = (async () => { - try { - await handler(...args) - } catch (err) { - console.error(err) - } +/** + * @private + * + * @param {SetIntervalAsyncTimer} timer + * @param {number} iterationId + * @param {function} handler + * @param {number} interval + * @param {...*} args + */ +function timeoutHandler (timer, iterationId, handler, interval, ...args) { + delete timer.timeouts[iterationId] + timer.promises[iterationId] = runHandler( + timer, + iterationId, + handler, + interval, + ...args + ) +} + +/** + * @private + * + * @param {SetIntervalAsyncTimer} timer + * @param {number} iterationId + * @param {function} handler + * @param {number} interval + * @param {...*} args + */ +async function runHandler (timer, iterationId, handler, interval, ...args) { + // The next line ensures that timer.promises[iterationId] is set + // before running the handler. + await noop() + try { + await handler(...args) + } finally { if (!timer.stopped) { - timer.timeouts[id + 1] = setTimeout( + const nextIterationId = getNextIterationId(iterationId) + timer.timeouts[nextIterationId] = setTimeout( timeoutHandler, interval, timer, + nextIterationId, handler, interval, ...args ) } - delete timer.timeouts[id] - delete timer.promises[id] - })() - timer.id = id + 1 + delete timer.promises[iterationId] + } } export { setIntervalAsync, clearIntervalAsync, SetIntervalAsyncTimer, SetIntervalAsyncError } @@ -104,13 +134,13 @@

Source: fixed.js


- Documentation generated by JSDoc 3.6.5 on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time) + Documentation generated by JSDoc 3.6.5 on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
diff --git a/docs/global.html b/docs/global.html index 61a77ff..95c565d 100644 --- a/docs/global.html +++ b/docs/global.html @@ -94,74 +94,6 @@

-

Members

- - - -

(constant) MAX_INTERVAL_MS

- - - - -
- Copyright (c) 2019 Emilio Almansi. All rights reserved. -This work is licensed under the terms of the MIT license. -For a copy, see the file LICENSE in the root directory. -
- - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - -

Methods

@@ -352,7 +284,7 @@
Parameters:
Source:
@@ -590,7 +522,7 @@
Parameters:
Source:
@@ -830,7 +762,7 @@
Parameters:
Source:
@@ -888,7 +820,7 @@
Returns:
-

(async) clearIntervalAsync(timer) → {Promise}

+

clearIntervalAsync(timer) → {Promise}

@@ -1059,13 +991,13 @@
Returns:

- Documentation generated by JSDoc 3.6.5 on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time) + Documentation generated by JSDoc 3.6.5 on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
diff --git a/docs/index.html b/docs/index.html index 9ee7d71..ffbad15 100644 --- a/docs/index.html +++ b/docs/index.html @@ -219,13 +219,13 @@

License


- Documentation generated by JSDoc 3.6.5 on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time) + Documentation generated by JSDoc 3.6.5 on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
diff --git a/docs/legacy.js.html b/docs/legacy.js.html index 1ee3797..ceed73e 100644 --- a/docs/legacy.js.html +++ b/docs/legacy.js.html @@ -27,15 +27,16 @@

Source: legacy.js

/**
- * Copyright (c) 2019 Emilio Almansi. All rights reserved.
+ * Copyright (c) 2019-2021 Emilio Almansi. All rights reserved.
  * This work is licensed under the terms of the MIT license.
  * For a copy, see the file LICENSE in the root directory.
  */
 
 import { clearIntervalAsync } from './clear'
-import { validateHandler, validateInterval } from './validation'
 import SetIntervalAsyncError from './error'
 import SetIntervalAsyncTimer from './timer'
+import { getNextIterationId, noop } from './util'
+import { validateHandler, validateInterval } from './validation'
 
 /**
  * Executes the given handler at fixed intervals; ie. the start time<br>
@@ -59,11 +60,12 @@ 

Source: legacy.js

validateHandler(handler) validateInterval(interval) const timer = new SetIntervalAsyncTimer() - const id = timer.id - timer.timeouts[id] = setTimeout( + const iterationId = 0 + timer.timeouts[iterationId] = setTimeout( timeoutHandler, interval, timer, + iterationId, handler, interval, ...args @@ -71,26 +73,54 @@

Source: legacy.js

return timer } -function timeoutHandler (timer, handler, interval, ...args) { - const id = timer.id - timer.promises[id] = (async () => { - timer.timeouts[id + 1] = setTimeout( - timeoutHandler, - interval, - timer, - handler, - interval, - ...args - ) - try { - await handler(...args) - } catch (err) { - console.error(err) - } - delete timer.timeouts[id] - delete timer.promises[id] - })() - timer.id = id + 1 +/** + * @private + * + * @param {SetIntervalAsyncTimer} timer + * @param {number} iterationId + * @param {function} handler + * @param {number} interval + * @param {...*} args + */ +function timeoutHandler (timer, iterationId, handler, interval, ...args) { + delete timer.timeouts[iterationId] + timer.promises[iterationId] = runHandler( + timer, + iterationId, + handler, + interval, + ...args + ) +} + +/** + * @private + * + * @param {SetIntervalAsyncTimer} timer + * @param {number} iterationId + * @param {function} handler + * @param {number} interval + * @param {...*} args + */ +async function runHandler (timer, iterationId, handler, interval, ...args) { + // The next line ensures that timer.promises[iterationId] is set + // before running the handler. + await noop() + const nextIterationId = getNextIterationId(iterationId) + timer.timeouts[nextIterationId] = setTimeout( + timeoutHandler, + interval, + timer, + nextIterationId, + handler, + interval, + ...args + ) + try { + await handler(...args) + } finally { + delete timer.promises[iterationId] + } } export { setIntervalAsync, clearIntervalAsync, SetIntervalAsyncTimer, SetIntervalAsyncError } @@ -104,13 +134,13 @@

Source: legacy.js


- Documentation generated by JSDoc 3.6.5 on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time) + Documentation generated by JSDoc 3.6.5 on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
diff --git a/docs/timer.js.html b/docs/timer.js.html index 778109e..9c0947e 100644 --- a/docs/timer.js.html +++ b/docs/timer.js.html @@ -39,7 +39,6 @@

Source: timer.js

class SetIntervalAsyncTimer { constructor () { this.stopped = false - this.id = 0 this.timeouts = {} this.promises = {} } @@ -56,13 +55,13 @@

Source: timer.js


- Documentation generated by JSDoc 3.6.5 on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time) + Documentation generated by JSDoc 3.6.5 on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)