fix(sync-worker): Never paginate faster than 1 page per 1.5sec

This should prevent us from flooding the app with contacts (/contacts responds very quickly). Fixes T3475
This commit is contained in:
Ben Gotow 2015-09-14 13:30:45 -07:00
parent 345dd941a3
commit 041950e014
2 changed files with 4 additions and 1 deletions

View file

@ -132,6 +132,7 @@ class NylasSyncWorker
@_resumeTimer.start()
fetchCollectionPage: (model, params = {}) ->
requestStartTime = Date.now()
requestOptions =
error: (err) =>
return if @_terminated
@ -143,7 +144,8 @@ class NylasSyncWorker
lastReceivedIndex = params.offset + json.length
if json.length is params.limit
nextParams = _.extend({}, params, {offset: lastReceivedIndex})
@fetchCollectionPage(model, nextParams)
nextDelay = Math.max(0, 1500 - (Date.now() - requestStartTime))
setTimeout(( => @fetchCollectionPage(model, nextParams)), nextDelay)
@updateTransferState(model, {fetched: lastReceivedIndex})
else
@updateTransferState(model, {fetched: lastReceivedIndex, busy: false, complete: true})

View file

@ -170,6 +170,7 @@ describe "NylasSyncWorker", ->
models = []
models.push(new Thread) for i in [0..(pageSize-1)]
@request.requestOptions.success(models)
advanceClock(2000)
expect(@apiRequests.length).toBe(1)
expect(@apiRequests[0].params).toEqual
limit: pageSize,