📦 mini-task-loop
📌 a simple javascript (node.js) library for managing scheduled async tasks for a loop.
Install
$ npm install mini-task-loop
Example
const TaskLoop = ; // create our task loop with strict modelet taskloop = true; { let counter = 0; // add first task run every 1000 milliseconds taskloop; // make this task execute immediately after it's add} { // using an object as the task id, run every 5 seconds. let obj = id: 'second' foo: 'bar' counter: 0 ; taskloop;} ;; // show all tasks we just addedconsole;
API
TaskLoop([strict_mode])
Constructor function of TaskLoop
class.
An optional boolean to set strict mode
of this object.
When strict mode
is enabled, any operation of task will throw a runtime error if task id not matching in the loop.
runTaskLoop([loop_polling_interval])
Start running the task loop.
- @number
loop_polling_interval
: an integer to specify how many milliseconds between each process frame of task loop. default100
.- set this value based on average interval of tasks, smaller value means lower process latency of task scheduling.
stopTaskLoop([remove_all_task])
Stop running the task loop, existing tasks will be retained.
- @boolean
remove_all_task
: specify whether to remove all tasks in the task loop. defaultfalse
.
addTask(id, interval, callback[, init_paused])
Add a new task to the task loop.
- @any
id
: an unique value or object used to identify the task. - @number
interval
: an integer to specify how many milliseconds between each execution of the task. - @function
callback
: called when task interval arrive in each times,id
will passed to this function as param. - @boolean
init_paused
: specify whether to pause this task when it added. defaultfalse
.
;
removeTask([id])
Remove task from the task loop.
- @any
id
: if not specified, all tasks will be removed.
pauseTask([id, reset_counter])
Pause a running task.
- @any
id
: if not specified, all tasks will be paused. - @boolean
reset_counter
: specify whether to reset task counter to zero. defaultfalse
.
resumeTask([id, reset_counter])
Resume a paused task.
- @any
id
: if not specified, all paused tasks will be resume. - @boolean
reset_counter
: specify whether to reset task counter to zero. defaultfalse
.
queryTask([id])
Query task status. If task id not found, undefined
will be returned.
- @any
id
: if not specified, a map iterator object that includes all task will be returned.
executeOnce([id, reset_counter, skip_paused])
Execute the task callback function.
- @any
id
: if not specified, all tasks callback will be executed. - @boolean
reset_counter
: specify whether to reset task counter to zero. defaultfalse
. - @boolean
skip_paused
: specify whether to skip task execution if task is paused. defaulttrue
.
Note
- The complete methods and description can be found in
module.js
- More example see
test.js
and run test usingnpm test
command.