Tasks and queues are primitives contained in
@orbit/core that are useful for
processing actions asynchronously and serially.
Although you’ll typically work with tasks indirectly, understanding these concepts can help you better troubleshoot and harden your Orbit applications.
Every action performed by sources, from updates to queries, is considered a “task” to be performed asynchronously.
Task interface is simply:
type, such as
"update", signals how that task should
be performed. An
id is assigned to uniquely identify the task. And
should contain the type-specific data needed to perform the task, such as an
object that conforms with the
Tasks are performed asynchronously by a
Source implements the
Tasks can be added to queues, which act as FIFO stacks that perform each task serially and asynchronously.
Task queues are associated with a single
performer, such as a
will perform each task. A
performer must be assigned when instantiating a
By default, task queues automatically process any tasks that are added to them and will continue until either all tasks have been performed or a problem has been encountered. For finer control over processing, it’s possible to instantiate a queue that will only process tasks explicitly:
Tasks are normally added to the end of a queue via the
Depending upon whether a queue is set to
autoProcess, task processing will
start either immediately or after the
process method has been called.
Queues emit the following events when processing tasks:
change- whenever a task has been added or removed to a queue
beforeTask- before processing of a task begins
task- after a task has been processed successfully
fail- when a task has failed to process
complete- when all tasks have been processed
As each task is processed successfully, it will be removed from the queue.
If processing fails, the queue will emit the
fail event and processing will
stop. At that point, you have several options:
retry()will retry the task that failed.
skip()will cancel and discard the current task and proceed to process the next task.
clear()will cancel the current task and completely clear the queue.
shift()will cancel the current task and remove it, but will not continue processing.
unshift(newTask)will cancel the current task and insert a new task in front of it at the beginning of the queue.
These options provide fairly complete control over task processing, which can prove useful when handling exceptions, debugging, and testing.
@orbit/data maintains two task queues:
requestQueuefor processing requests, such as updates and queries.
syncQueuefor synchronizing changes between sources.
These queues are discussed in more detail as part of the guide on “data flows”.