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
|
|
|
|
2015-10-01 01:47:33 +08:00
|
|
|
INITIAL_PAGE_SIZE = 30
|
|
|
|
MAX_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')
|
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-10-04 14:53:59 +08:00
|
|
|
@fetchCollection('labels', {initialPageSize: 1000})
|
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-10-04 14:53:59 +08:00
|
|
|
@fetchCollection('folders', {initialPageSize: 1000})
|
2015-10-01 01:47:33 +08:00
|
|
|
@fetchCollection('drafts')
|
|
|
|
@fetchCollection('contacts')
|
|
|
|
@fetchCollection('calendars')
|
|
|
|
@fetchCollection('events')
|
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)
|
2015-10-04 14:53:59 +08:00
|
|
|
@fetchCollectionPage(model, {
|
|
|
|
limit: options.initialPageSize ? INITIAL_PAGE_SIZE,
|
|
|
|
offset: 0
|
|
|
|
})
|
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-09-15 04:30:45 +08:00
|
|
|
requestStartTime = Date.now()
|
2015-04-07 02:46:20 +08:00
|
|
|
requestOptions =
|
|
|
|
error: (err) =>
|
|
|
|
return if @_terminated
|
2015-09-24 01:46:07 +08:00
|
|
|
@_fetchCollectionPageError(model, err)
|
2015-04-07 02:46:20 +08:00
|
|
|
success: (json) =>
|
|
|
|
return if @_terminated
|
2015-09-24 01:46:07 +08:00
|
|
|
|
|
|
|
if model in ["labels", "folders"] and @_hasNoInbox(json)
|
|
|
|
@_fetchCollectionPageError(model, "No inbox in #{model}")
|
|
|
|
return
|
|
|
|
|
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})
|
2015-10-01 01:47:33 +08:00
|
|
|
nextParams.limit = Math.min(Math.round(params.limit * 1.5), MAX_PAGE_SIZE)
|
|
|
|
nextDelay = Math.max(0, 1000 - (Date.now() - requestStartTime))
|
2015-09-15 04:30:45 +08:00
|
|
|
setTimeout(( => @fetchCollectionPage(model, nextParams)), nextDelay)
|
2015-05-20 06:59:37 +08:00
|
|
|
@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-09-24 01:46:07 +08:00
|
|
|
# It's occasionally possible for the NylasAPI's labels or folders
|
|
|
|
# endpoint to not return an "inbox" label. Since that's a core part of
|
|
|
|
# the app and it doesn't function without it, keep retrying until we see
|
|
|
|
# it.
|
|
|
|
_hasNoInbox: (json) ->
|
|
|
|
return not _.any(json, (obj) -> obj.name is "inbox")
|
|
|
|
|
|
|
|
_fetchCollectionPageError: (model, err) ->
|
|
|
|
@_resumeTimer.backoff()
|
|
|
|
@_resumeTimer.start()
|
|
|
|
@updateTransferState(model, {busy: false, complete: false, error: err.toString()})
|
|
|
|
|
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
|
2015-10-01 01:47:33 +08:00
|
|
|
NylasSyncWorker.INITIAL_PAGE_SIZE = INITIAL_PAGE_SIZE
|
|
|
|
NylasSyncWorker.MAX_PAGE_SIZE = MAX_PAGE_SIZE
|