2015-10-03 01:57:40 +08:00
---
layout: docs
title: TaskQueue
2015-10-04 03:39:12 +08:00
edit_url: "https://github.com/nylas/N1/blob/master/src/flux/stores/task-queue.coffee"
2015-10-03 01:57:40 +08:00
---
2015-10-02 03:34:16 +08:00
2015-10-03 01:57:40 +08:00
< h2 > Summary< / h2 >
2015-10-02 03:34:16 +08:00
2015-10-03 01:57:40 +08:00
< div class = "markdown-from-sourecode" >
< p > < p > The TaskQueue is a Flux-compatible Store that manages a queue of < a href = 'task.html' > Task< / a >
objects. Each < a href = 'task.html' > Task< / a > represents an individual API action, like sending a draft
2015-10-02 03:34:16 +08:00
or marking a thread as " read" . Tasks optimistically make changes to the app' s
local cache and encapsulate logic for performing changes on the server, rolling
back in case of failure, and waiting on dependent tasks.< / p >
< p > The TaskQueue is essential to offline mode in N1. It automatically pauses
when the user' s internet connection is unavailable and resumes when online.< / p >
< p > The task queue is persisted to disk, ensuring that tasks are executed later,
even if the user quits N1.< / p >
< p > The TaskQueue is only available in the app' s main window. Rather than directly
2015-10-03 01:57:40 +08:00
queuing tasks, you should use the < a href = 'actions.html' > Actions< / a > to interact with the < a href = 'taskqueue.html' > TaskQueue< / a > .
2015-10-02 03:34:16 +08:00
Tasks queued from secondary windows are serialized and sent to the application' s
main window via IPC.< / p >
< h2 id = "queueing-a-task" > Queueing a Task< / h2 >
< pre > < code class = "lang-coffee" > < span class = "hljs-keyword" > if< / span > < span class = "hljs-property" > @_thread< / span > & & < span class = "hljs-property" > @_thread< / span > .unread
Actions.queueTask(< span class = "hljs-keyword" > new< / span > ChangeStarredTask(< span class = "hljs-attribute" > thread< / span > : < span class = "hljs-property" > @_thread< / span > , < span class = "hljs-attribute" > starred< / span > : < span class = "hljs-literal" > true< / span > ))
< / code > < / pre >
< h2 id = "dequeueing-a-task" > Dequeueing a Task< / h2 >
2015-10-03 07:10:51 +08:00
< pre > < code class = "lang-coffee" > < span class = "hljs-tag" > Actions< / span > < span class = "hljs-class" > .dequeueMatchingTask< / span > (< span class = "hljs-rules" > {
< span class = "hljs-rule" > < span class = "hljs-attribute" > type< / span > :< span class = "hljs-value" > < span class = "hljs-string" > 'FileUploadTask'< / span > ,
2015-10-02 03:34:16 +08:00
matching: {
filePath: uploadData.filePath
}
2015-10-03 07:10:51 +08:00
})< / span > < / span > < / span >
2015-10-02 03:34:16 +08:00
< / code > < / pre >
< h2 id = "creating-tasks" > Creating Tasks< / h2 >
2015-10-03 01:57:40 +08:00
< p > Support for creating custom < a href = 'task.html' > Task< / a > subclasses in third-party packages is coming soon.
2015-10-02 03:34:16 +08:00
This is currently blocked by the ActionBridge, which is responsible for sending actions
between windows, since it' s JSON serializer is not extensible.< / p >
< / p >
2015-10-03 01:57:40 +08:00
< / div >
2015-10-02 03:34:16 +08:00
2015-10-03 01:57:40 +08:00
< ul >
< / ul >
2015-10-02 03:34:16 +08:00
2015-10-03 01:57:40 +08:00
< h3 > Instance Methods< / h3 >
2015-10-02 03:34:16 +08:00
2015-10-03 01:57:40 +08:00
< h4 id = findTask class = "function-name" >
findTask(< span class = "args" > < span class = "arg" > type< / span > < span class = "arg" > matching< / span > < / span > ) < a href = "#findTask" class = "link" > < / a >
< / h4 >
2015-10-04 03:39:12 +08:00
2015-10-03 01:57:40 +08:00
< div class = "function-description markdown-from-sourecode" >
< p > < / p >
< / div >
2015-10-04 03:39:12 +08:00
2015-10-03 01:57:40 +08:00
< strong > Parameters< / strong >
< table class = "arguments" >
2015-10-02 03:34:16 +08:00
< tr >
2015-10-03 01:57:40 +08:00
< th > Argument< / th >
< th > Description< / th >
2015-10-02 03:34:16 +08:00
< / tr >
2015-10-03 01:57:40 +08:00
< tr >
< td style = "width:15%;" >
< em > type< / em >
< / td >
< td class = "markdown-from-sourecode" >
2015-10-04 03:39:12 +08:00
2015-10-03 01:57:40 +08:00
< p > The string name of the task class, or the Task class itself. (ie: {SaveDraftTask} or ' SaveDraftTask' )< / p >
2015-10-04 03:39:12 +08:00
2015-10-03 01:57:40 +08:00
< / td >
< / tr >
< tr >
< td style = "width:15%;" >
< em > matching< / em >
< / td >
< td class = "markdown-from-sourecode" >
2015-10-04 03:39:12 +08:00
2015-10-03 01:57:40 +08:00
< p > Optional An < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/object' > Object< / a > with criteria to pass to _.isMatch. For a SaveDraftTask, this could be {draftClientId: " 123123" }< / p >
2015-10-04 03:39:12 +08:00
2015-10-03 01:57:40 +08:00
< / td >
< / tr >
< / table >
2015-10-04 03:39:12 +08:00
2015-10-03 01:57:40 +08:00
< strong > Returns< / strong >
< table class = "arguments" >
< tr >
< th > Return Values< / th >
< / tr >
< tr > < td class = "markdown-from-sourecode" > < p > Returns an existing task in the queue that matches the type you provide,
and any other match properties. Useful for checking to see if something, like
a " SendDraft" task is in-flight.< / p >
< / td > < / tr >
< tr > < td class = "markdown-from-sourecode" > < p > Returns a matching < a href = 'task.html' > Task< / a > , or null.< / p >
< / td > < / tr >
< / table >