bump package version to 1.0.2
[squeep-chores] / lib / chores.js
index e2f7b3381155ba78cc992bad1429ff0ce2e2b719..08edfcde2350ca16ecb21cbb2a267e67c2fdaa36 100644 (file)
@@ -1,21 +1,40 @@
 'use strict';
 
 const { ChoreError } = require('./errors');
-const { fileScope } = require('./common');
+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<void>} 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<void>}
+   * @param {string} choreName chore name
+   * @param  {...any} choreArgs additional args to pass to chore function
+   * @returns {Promise<void>} 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');
@@ -106,4 +125,4 @@ class Chores {
 
 } // Chores
 
-module.exports = Chores;
\ No newline at end of file
+module.exports = Chores;