X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fchores.js;h=08edfcde2350ca16ecb21cbb2a267e67c2fdaa36;hb=3b0ce05a7797f1d61da67150e54cb0fd6dcc120c;hp=13842653a2f5d63cedfaec663526032bdc7d5e35;hpb=d71fcf409f4cfccb5e3866cca2ef134383d97816;p=squeep-chores diff --git a/lib/chores.js b/lib/chores.js index 1384265..08edfcd 100644 --- a/lib/chores.js +++ b/lib/chores.js @@ -5,17 +5,36 @@ const { fileScope } = require('@squeep/log-helper'); const _fileScope = fileScope(__filename); /** - * + * @typedef {object} ConsoleLike + * @property {Function} debug debug + * @property {Function} error error + */ +/** + * @typedef {object} Chore + * @property {boolean} isRunning actively being executed + * @property {Function} choreFn task handler to invoke + * @property {number} intervalMs period to wait between invocations + * @property {NodeJS.Timeout=} timeoutObj active timeout + * @property {Date=} nextSchedule time of next invocation + */ + +/** + * Thin wrapper for wrangling periodic tasks with setTimeout. */ class Chores { /** - * @param {ConsoleLike} logger + * @param {ConsoleLike} logger logger object */ constructor(logger) { this.logger = logger; this.chores = {}; } + /** + * + * @param {string} choreName chore name + * @returns {Chore} chore + */ _getChore(choreName) { const chore = this.chores[choreName]; // eslint-disable-line security/detect-object-injection if (!chore) { @@ -27,9 +46,9 @@ class Chores { /** * Register a chore task to be called every intervalMs. * Zero interval will only register task, will not schedule any calls. - * @param {String} choreName - * @param {() => Promise(void)} choreFn - * @param {Number} intervalMs + * @param {string} choreName chore name + * @param {() => Promise} choreFn chore function + * @param {number} intervalMs invocation period */ establishChore(choreName, choreFn, intervalMs = 0) { const _scope = _fileScope('establishChore'); @@ -52,9 +71,9 @@ class Chores { /** * Invoke a chore task off-schedule, with any arguments. * Resets timer of any next scheduled call. - * @param {String} choreName - * @param {...any} choreArgs - * @returns {Promise} + * @param {string} choreName chore name + * @param {...any} choreArgs additional args to pass to chore function + * @returns {Promise} nothing */ async runChore(choreName, ...choreArgs) { const _scope = _fileScope('runChore'); @@ -86,7 +105,7 @@ class Chores { /** * Stop a chore from being scheduled to run. - * @param {String} choreName + * @param {string} choreName chore name */ stopChore(choreName) { const _scope = _fileScope('stopChore');