Skip to content

Commit

Permalink
Updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmansi committed Mar 29, 2021
1 parent a9c6bc7 commit 108d5b0
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 171 deletions.
4 changes: 2 additions & 2 deletions docs/SetIntervalAsyncError.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ <h4 class="name" id="SetIntervalAsyncError"><span class="type-signature"></span>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li><li><a href="global.html#MAX_INTERVAL_MS">MAX_INTERVAL_MS</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
4 changes: 2 additions & 2 deletions docs/SetIntervalAsyncTimer.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ <h4 class="name" id="SetIntervalAsyncTimer"><span class="type-signature"></span>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li><li><a href="global.html#MAX_INTERVAL_MS">MAX_INTERVAL_MS</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
37 changes: 17 additions & 20 deletions docs/clear.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ <h1 class="page-title">Source: clear.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* 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.&lt;br>
Expand All @@ -43,25 +43,22 @@ <h1 class="page-title">Source: clear.js</h1>
* @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 }
</code></pre>
</article>
</section>
Expand All @@ -72,13 +69,13 @@ <h1 class="page-title">Source: clear.js</h1>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li><li><a href="global.html#MAX_INTERVAL_MS">MAX_INTERVAL_MS</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
72 changes: 51 additions & 21 deletions docs/dynamic.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ <h1 class="page-title">Source: dynamic.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* 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&lt;br>
Expand All @@ -57,46 +58,75 @@ <h1 class="page-title">Source: dynamic.js</h1>
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
)
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 }
Expand All @@ -110,13 +140,13 @@ <h1 class="page-title">Source: dynamic.js</h1>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li><li><a href="global.html#MAX_INTERVAL_MS">MAX_INTERVAL_MS</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
4 changes: 2 additions & 2 deletions docs/error.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ <h1 class="page-title">Source: error.js</h1>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li><li><a href="global.html#MAX_INTERVAL_MS">MAX_INTERVAL_MS</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
68 changes: 49 additions & 19 deletions docs/fixed.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ <h1 class="page-title">Source: fixed.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* 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&lt;br>
Expand All @@ -57,40 +58,69 @@ <h1 class="page-title">Source: fixed.js</h1>
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
)
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 }
Expand All @@ -104,13 +134,13 @@ <h1 class="page-title">Source: fixed.js</h1>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li><li><a href="global.html#MAX_INTERVAL_MS">MAX_INTERVAL_MS</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="SetIntervalAsyncError.html">SetIntervalAsyncError</a></li><li><a href="SetIntervalAsyncTimer.html">SetIntervalAsyncTimer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%255BDynamic%255DsetIntervalAsync">[Dynamic] setIntervalAsync</a></li><li><a href="global.html#%255BFixed%255DsetIntervalAsync">[Fixed] setIntervalAsync</a></li><li><a href="global.html#%255BLegacy%255DsetIntervalAsync">[Legacy] setIntervalAsync</a></li><li><a href="global.html#clearIntervalAsync">clearIntervalAsync</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Fri Dec 25 2020 12:07:26 GMT+0100 (Central European Standard Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Mon Mar 29 2021 15:47:37 GMT+0200 (Central European Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down

0 comments on commit 108d5b0

Please sign in to comment.