2015-05-20 07:06:59 +08:00
|
|
|
_ = require 'underscore'
|
2015-08-29 04:24:05 +08:00
|
|
|
{Actions, DatabaseStore} = require 'nylas-exports'
|
2015-05-16 01:53:00 +08:00
|
|
|
NylasLongConnection = require './nylas-long-connection'
|
2015-05-20 06:59:37 +08:00
|
|
|
|
refactor(*): Thread list fixes, flexible workspace store, multiple root sheets
Summary:
Remember to remove all the event listeners added to email frame
New files tab, queryable filename, not attribute
Rename ThreadSelectionBar to RootSelectionBar to go with RootCenterComponent, make it appear for draft selection and file selection as well
Initial file list and file list store, File Location
Remove unnecessary shouldComponentUpdate
Always track whether new requests have happened since ours to prevent out of order triggers
Always scroll to the current [focused/keyboard-cursor] in lists
So goodbye to the trash tag
Only scroll to current item if focus or keyboard has moved
Show message snippet in notification if no subject line
Make the RootSelectionBar pull items from Component Registry
New Archive button (prettier than the other one)
Refactor event additions to iframe so iframe can be used for file display also
Thread List is no longer the uber root package - drafts and files moved to separate packages
WorkspaceStore now allows packages to register sheets, "view" concept replaced with "root sheet" concept, "mode" may not be observed by all sheets, and is now called "preferred mode"
Don't animate transitions between two root sheets
Mode switch is only visible on root sheets that support multiple modes
Account sidebar now shows "Views" that have registered themselves: drafts and files for now
Model Selection Bar is now a component, just like ModelList. Meant to be in the toolbar above a Model List
Misc supporting changes
New files package which registers it's views and components
Rename files package to `file-list`
Move checkmark column down into model list
Don't throw exception if shift-down arrow and nothing selected
Takes a long time on login to fetch first page of threads, make pages smaller
Displaynames, spec fixes
Test Plan: Run tests
Reviewers: evan
Reviewed By: evan
Differential Revision: https://review.inboxapp.com/D1412
2015-04-11 05:33:05 +08:00
|
|
|
PAGE_SIZE = 250
|
2015-04-07 02:46:20 +08:00
|
|
|
|
2015-08-12 03:28:37 +08:00
|
|
|
# BackoffTimer is a small helper class that wraps setTimeout. It fires the function
|
|
|
|
# you provide at a regular interval, but backs off each time you call `backoff`.
|
|
|
|
#
|
|
|
|
class BackoffTimer
|
|
|
|
constructor: (@fn) ->
|
|
|
|
@reset()
|
|
|
|
|
|
|
|
cancel: =>
|
|
|
|
clearTimeout(@_timeout) if @_timeout
|
|
|
|
@_timeout = null
|
|
|
|
|
|
|
|
reset: =>
|
|
|
|
@cancel()
|
|
|
|
@_delay = 20 * 1000
|
|
|
|
|
|
|
|
backoff: =>
|
|
|
|
@_delay = Math.min(@_delay * 1.4, 5 * 1000 * 60) # Cap at 5 minutes
|
2015-08-14 02:20:36 +08:00
|
|
|
if not atom.inSpecMode()
|
|
|
|
console.log("Backing off after sync failure. Will retry in #{Math.floor(@_delay / 1000)} seconds.")
|
2015-08-12 03:28:37 +08:00
|
|
|
|
|
|
|
start: =>
|
|
|
|
clearTimeout(@_timeout) if @_timeout
|
|
|
|
@_timeout = setTimeout =>
|
|
|
|
@_timeout = null
|
|
|
|
@fn()
|
|
|
|
, @_delay
|
|
|
|
|
|
|
|
|
2015-04-07 02:46:20 +08:00
|
|
|
module.exports =
|
2015-05-16 01:53:00 +08:00
|
|
|
class NylasSyncWorker
|
2015-04-07 02:46:20 +08:00
|
|
|
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
constructor: (api, account) ->
|
2015-05-16 01:53:00 +08:00
|
|
|
@_api = api
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
@_account = account
|
2015-04-07 02:46:20 +08:00
|
|
|
|
|
|
|
@_terminated = false
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
@_connection = new NylasLongConnection(api, account.id)
|
2015-08-14 02:20:36 +08:00
|
|
|
@_resumeTimer = new BackoffTimer =>
|
|
|
|
# indirection needed so resumeFetches can be spied on
|
|
|
|
@resumeFetches()
|
2015-08-07 03:21:24 +08:00
|
|
|
|
2015-08-29 04:24:05 +08:00
|
|
|
@_unlisten = Actions.retryInitialSync.listen(@_onRetryInitialSync, @)
|
|
|
|
|
2015-08-07 03:21:24 +08:00
|
|
|
@_state = null
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
DatabaseStore.findJSONObject("NylasSyncWorker:#{@_account.id}").then (json) =>
|
2015-08-07 03:21:24 +08:00
|
|
|
@_state = json ? {}
|
|
|
|
for model, modelState of @_state
|
|
|
|
modelState.busy = false
|
|
|
|
@resumeFetches()
|
2015-05-20 06:59:37 +08:00
|
|
|
|
2015-04-07 02:46:20 +08:00
|
|
|
@
|
2015-05-16 01:53:00 +08:00
|
|
|
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
account: ->
|
|
|
|
@_account
|
2015-04-07 02:46:20 +08:00
|
|
|
|
|
|
|
connection: ->
|
|
|
|
@_connection
|
|
|
|
|
2015-05-20 06:59:37 +08:00
|
|
|
state: ->
|
|
|
|
@_state
|
|
|
|
|
2015-06-18 04:14:45 +08:00
|
|
|
busy: ->
|
2015-08-07 03:21:24 +08:00
|
|
|
return false unless @_state
|
2015-06-18 04:14:45 +08:00
|
|
|
for key, state of @_state
|
|
|
|
if state.busy
|
|
|
|
return true
|
|
|
|
false
|
|
|
|
|
2015-04-07 02:46:20 +08:00
|
|
|
start: ->
|
2015-08-14 02:20:36 +08:00
|
|
|
@_resumeTimer.start()
|
2015-04-07 02:46:20 +08:00
|
|
|
@_connection.start()
|
2015-05-20 06:59:37 +08:00
|
|
|
@resumeFetches()
|
2015-06-18 04:14:45 +08:00
|
|
|
|
2015-04-07 02:46:20 +08:00
|
|
|
cleanup: ->
|
2015-08-29 04:24:05 +08:00
|
|
|
@_unlisten?()
|
2015-08-12 03:28:37 +08:00
|
|
|
@_resumeTimer.cancel()
|
2015-04-07 02:46:20 +08:00
|
|
|
@_connection.end()
|
|
|
|
@_terminated = true
|
|
|
|
@
|
|
|
|
|
2015-05-20 06:59:37 +08:00
|
|
|
resumeFetches: =>
|
2015-08-07 03:21:24 +08:00
|
|
|
return unless @_state
|
2015-08-12 03:28:37 +08:00
|
|
|
|
|
|
|
# Stop the timer. If one or more network requests fails during the fetch process
|
|
|
|
# we'll backoff and restart the timer.
|
|
|
|
@_resumeTimer.cancel()
|
|
|
|
|
2015-05-20 06:59:37 +08:00
|
|
|
@fetchCollection('threads')
|
|
|
|
@fetchCollection('calendars')
|
|
|
|
@fetchCollection('contacts')
|
2015-07-23 02:18:23 +08:00
|
|
|
@fetchCollection('drafts')
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
if @_account.usesLabels()
|
2015-07-17 08:47:02 +08:00
|
|
|
@fetchCollection('labels')
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
if @_account.usesFolders()
|
2015-07-17 08:47:02 +08:00
|
|
|
@fetchCollection('folders')
|
2015-05-20 06:59:37 +08:00
|
|
|
|
|
|
|
fetchCollection: (model, options = {}) ->
|
2015-08-07 03:21:24 +08:00
|
|
|
return unless @_state
|
2015-04-07 02:46:20 +08:00
|
|
|
return if @_state[model]?.complete and not options.force?
|
2015-05-20 06:59:37 +08:00
|
|
|
return if @_state[model]?.busy
|
2015-04-07 02:46:20 +08:00
|
|
|
|
2015-05-20 06:59:37 +08:00
|
|
|
@_state[model] =
|
|
|
|
complete: false
|
|
|
|
error: null
|
|
|
|
busy: true
|
|
|
|
count: 0
|
|
|
|
fetched: 0
|
2015-04-07 02:46:20 +08:00
|
|
|
@writeState()
|
|
|
|
|
2015-05-20 06:59:37 +08:00
|
|
|
@fetchCollectionCount(model)
|
|
|
|
@fetchCollectionPage(model, {offset: 0, limit: PAGE_SIZE})
|
2015-06-18 04:14:45 +08:00
|
|
|
|
2015-05-20 06:59:37 +08:00
|
|
|
fetchCollectionCount: (model) ->
|
|
|
|
@_api.makeRequest
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
accountId: @_account.id
|
|
|
|
path: "/#{model}"
|
2015-05-20 06:59:37 +08:00
|
|
|
returnsModel: false
|
|
|
|
qs:
|
|
|
|
view: 'count'
|
|
|
|
success: (response) =>
|
|
|
|
return if @_terminated
|
|
|
|
@updateTransferState(model, count: response.count)
|
|
|
|
error: (err) =>
|
|
|
|
return if @_terminated
|
2015-08-12 03:28:37 +08:00
|
|
|
@_resumeTimer.backoff()
|
|
|
|
@_resumeTimer.start()
|
2015-04-07 02:46:20 +08:00
|
|
|
|
2015-05-20 06:59:37 +08:00
|
|
|
fetchCollectionPage: (model, params = {}) ->
|
2015-04-07 02:46:20 +08:00
|
|
|
requestOptions =
|
|
|
|
error: (err) =>
|
|
|
|
return if @_terminated
|
2015-08-12 03:28:37 +08:00
|
|
|
@_resumeTimer.backoff()
|
|
|
|
@_resumeTimer.start()
|
2015-05-20 06:59:37 +08:00
|
|
|
@updateTransferState(model, {busy: false, complete: false, error: err.toString()})
|
2015-04-07 02:46:20 +08:00
|
|
|
success: (json) =>
|
|
|
|
return if @_terminated
|
2015-05-20 06:59:37 +08:00
|
|
|
lastReceivedIndex = params.offset + json.length
|
2015-04-07 02:46:20 +08:00
|
|
|
if json.length is params.limit
|
2015-05-20 06:59:37 +08:00
|
|
|
nextParams = _.extend({}, params, {offset: lastReceivedIndex})
|
|
|
|
@fetchCollectionPage(model, nextParams)
|
|
|
|
@updateTransferState(model, {fetched: lastReceivedIndex})
|
2015-04-07 02:46:20 +08:00
|
|
|
else
|
2015-05-20 06:59:37 +08:00
|
|
|
@updateTransferState(model, {fetched: lastReceivedIndex, busy: false, complete: true})
|
2015-04-07 02:46:20 +08:00
|
|
|
|
|
|
|
if model is 'threads'
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
@_api.getThreads(@_account.id, params, requestOptions)
|
2015-04-07 02:46:20 +08:00
|
|
|
else
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
@_api.getCollection(@_account.id, model, params, requestOptions)
|
2015-05-16 01:53:00 +08:00
|
|
|
|
2015-05-20 06:59:37 +08:00
|
|
|
updateTransferState: (model, {busy, error, complete, fetched, count}) ->
|
|
|
|
@_state[model] = _.defaults({busy, error, complete, fetched, count}, @_state[model])
|
|
|
|
@writeState()
|
|
|
|
|
2015-04-07 02:46:20 +08:00
|
|
|
writeState: ->
|
|
|
|
@_writeState ?= _.debounce =>
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
DatabaseStore.persistJSONObject("NylasSyncWorker:#{@_account.id}", @_state)
|
2015-04-07 02:46:20 +08:00
|
|
|
,100
|
|
|
|
@_writeState()
|
2015-08-12 03:28:37 +08:00
|
|
|
|
2015-08-29 04:24:05 +08:00
|
|
|
_onRetryInitialSync: =>
|
|
|
|
@resumeFetches()
|
|
|
|
|
2015-08-12 03:28:37 +08:00
|
|
|
NylasSyncWorker.BackoffTimer = BackoffTimer
|