workpublic
work
A first-in-first-out async task queue. Fill the queue with Task instances (using module:work.TaskQueue#push) then (when ready) call module:work.TaskQueue#process to settle each task in the order they were received. During processing, the queue will process module:work.TaskQueue#maxConcurrent tasks at a time. Each task comprises a name, deferred and list of resolvers - each of which will race to settle the deferred first.
Example
var tq = var q = var http = var queue = // get the football results - it will take between 0 and 2000ms// and resolve with the result{ } // but the timeout resolver will reject it after 1000ms{ } var task = name: "get result" resolver: resultResolver timeoutResolver queue taskpromise queue
- work
- .Queue
- new Queue([options])
- .name :
string
- .queued :
Array
- .active :
Array
- .maxConcurrent :
number
- .length :
number
- .freeSlotCount :
number
- .push(task) ↩︎
- .shift() ⇒
Task
- .process()
- .unshift(newTask) ⇒
Task
- .cancel() ⇒
Task
- .isEmpty() ⇒
- "occupied"
- "push"
- "empty"
- "complete"
- "shift"
- "occupied"
- "unshift"
- "empty"
- "cancel"
- .Task ⇐
module:state-machine
- new Task(executor, [options])
- instance
- .promise :
external:Promise
- .name :
string
- .context :
string
- .process()
- .cancel()
- "fulfilled"
- "resolved"
- "rejected"
- "running"
- .promise :
- static
- .eState :
enum
- .eState :
- .Queue
tq.Queue
queue class for processing promises
Kind: static class of work
- .Queue
- new Queue([options])
- .name :
string
- .queued :
Array
- .active :
Array
- .maxConcurrent :
number
- .length :
number
- .freeSlotCount :
number
- .push(task) ↩︎
- .shift() ⇒
Task
- .process()
- .unshift(newTask) ⇒
Task
- .cancel() ⇒
Task
- .isEmpty() ⇒
- "occupied"
- "push"
- "empty"
- "complete"
- "shift"
- "occupied"
- "unshift"
- "empty"
- "cancel"
new Queue([options])
Param | Type | Default | Description |
---|---|---|---|
[options] | object |
||
[options.maxConcurrent] | number |
1 |
|
[options.name] | string |
useful for debugging |
string
queue.name : useful for debugging
Kind: instance property of Queue
Array
queue.queued : the current task queue
Kind: instance property of Queue
Array
queue.active : The in-progress tasks
Kind: instance property of Queue
number
queue.maxConcurrent : when emptying, specifying the max number of tasks which may run simultaneously.
Kind: instance property of Queue
Default: 1
number
queue.length : queue length
Kind: instance property of Queue
number
queue.freeSlotCount : Kind: instance property of Queue
queue.push(task) ↩︎
add a task to the end of the queue
Kind: instance method of Queue
Chainable
Param | Type | Description |
---|---|---|
task | Task |
task instance |
Task
queue.shift() ⇒ returns the next task in the queue and adds it to the active
list.
Kind: instance method of Queue
queue.process()
process the queue - attempt to resolve each task.
Kind: instance method of Queue
Todo
- return a promise which resolves on completion
Task
queue.unshift(newTask) ⇒ insert a task at the front of the queue, returning the instance inserted
Kind: instance method of Queue
Param | Type | Description |
---|---|---|
newTask | Task |
the task to be inserted |
Task
queue.cancel() ⇒ Shifts the next task off the queue and calls .cancel()
on it
Kind: instance method of Queue
queue.isEmpty() ⇒
Test whether any tasks are queued or active
Kind: instance method of Queue
Returns: boolean
"occupied"
Emitted at the moment a queue becomes occupied (has at least 1 task)
Kind: event emitted by Queue
"push"
Emitted when a task is added
Kind: event emitted by Queue
Type | Description |
---|---|
Task |
the task that was pushed |
"empty"
Emitted after the final task is taken off the queue for processing
Kind: event emitted by Queue
"complete"
Emitted when the queue processing is complete
Kind: event emitted by Queue
"shift"
Emitted when a task is shifted
Kind: event emitted by Queue
Type | Description |
---|---|
Task |
the task that was pushed |
"occupied"
Emitted at the moment a queue becomes occupied (has at least 1 task)
Kind: event emitted by Queue
"unshift"
Emitted when a task is unshifted to the front of the queue
Kind: event emitted by Queue
Type | Description |
---|---|
Task |
the task that was pushed |
"empty"
Emitted after the final task is taken off the queue for processing
Kind: event emitted by Queue
"cancel"
Emitted when a task is cancelled
Kind: event emitted by Queue
Type | Description |
---|---|
Task |
the task that was cancelled |
module:state-machine
tq.Task ⇐ A task defines a piece of work which needs doing now, or in the future. When you create a task you receive a promise for its result. Process the task using task.process()
.
Kind: static class of work
Extends: module:state-machine
- .Task ⇐
module:state-machine
- new Task(executor, [options])
- instance
- .promise :
external:Promise
- .name :
string
- .context :
string
- .process()
- .cancel()
- "fulfilled"
- "resolved"
- "rejected"
- "running"
- .promise :
- static
- .eState :
enum
- .eState :
new Task(executor, [options])
Param | Type | Description |
---|---|---|
executor | function | Array.<function()> |
the resolver function |
[options] | object |
an object containing optional values |
[options.name] | string |
a name string, useful for debugging |
[options.data] | object |
data used by the resolver function |
external:Promise
task.promise : a promise for the completion of the task
Kind: instance property of Task
string
task.name : useful for debug output
Kind: instance property of Task
string
task.context : data for the task
Kind: instance property of Task
task.process()
Process the task - settled the deferred using the supplied resolver function(s). The resolver function is called in the context of the task and receives a deferred
, which must be resolved.
Kind: instance method of Task
task.cancel()
a cancelled task fulfils with the value -1
Kind: instance method of Task
"fulfilled"
Emitted when a task fulfills
Kind: event emitted by Task
"resolved"
Emitted when a task is resolved
Kind: event emitted by Task
"rejected"
Emitted when a task is rejected
Kind: event emitted by Task
Type | Description |
---|---|
Error |
the rejection reason |
"running"
Emitted when a task starts
Kind: event emitted by Task
enum
Task.eState : The various Task states.
Kind: static enum property of Task
Properties
Name | Type | Default |
---|---|---|
PENDING | string |
"pending" |
RUNNING | string |
"running" |
FULFILLED | string |
"fulfilled" |
REJECTED | string |
"rejected" |
RESOLVED | string |
"resolved" |
CANCELLED | string |
"cancelled" |
© 2015 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.