Mailspring/internal_packages/deltas/lib/refreshing-json-cache.coffee
Evan Morikawa f2517ea7d7 refactor(name): rename worker-sync package to deltas
Summary:
This diff is a rename. No logical changes.

We used to have a set of files called `nylas-sync-worker`. In Old N1 these
used to have a lot of logic to slowly sync mail from our API. Since we
exclusively use local-sync via a soon-to-be-obsolesced delta stream, these
files really just manage the delta streaming connection.

It's incredibly confusing to have a set of files called worker-sync when
there's a sync-worker already in the codebase. This renames everything to
refer to them as account sync workers.

The reason I wanted this rename is because the IdentityStore on initial
launch triggers and fires a fairly scary-sounding
`Actions.refreshAllSyncWorkers()`. In reality all it does is
`Actions.refreshAllDeltaConnections()`. I'm also tired of staring at files
for a full minute before realizing that this is not the sync worker I was
  looking for.

Test Plan:
After rename, booted the app and ensured that deltas were coming through
for new mail and no errors were being thrown

Reviewers: halla, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3767
2017-01-25 10:08:23 -05:00

55 lines
1.4 KiB
CoffeeScript

_ = require 'underscore'
{NylasStore, DatabaseStore} = require 'nylas-exports'
class RefreshingJSONCache
constructor: ({@key, @version, @refreshInterval}) ->
@_timeoutId = null
start: ->
# Clear any scheduled actions
@end()
# Look up existing data from db
DatabaseStore.findJSONBlob(@key).then (json) =>
# Refresh immediately if json is missing or version is outdated. Otherwise,
# compute next refresh time and schedule
timeUntilRefresh = 0
if json? and json.version is @version
timeUntilRefresh = Math.max(0, @refreshInterval - (Date.now() - json.time))
@_timeoutId = setTimeout(@refresh, timeUntilRefresh)
reset: ->
# Clear db value, turn off any scheduled actions
DatabaseStore.inTransaction (t) => t.persistJSONBlob(@key, {})
@end()
end: ->
# Turn off any scheduled actions
clearInterval(@_timeoutId) if @_timeoutId
@_timeoutId = null
refresh: =>
# Set up next refresh call
clearTimeout(@_timeoutId) if @_timeoutId
@_timeoutId = setTimeout(@refresh, @refreshInterval)
# Call fetch data function, save it to the database
@fetchData (newValue) =>
DatabaseStore.inTransaction (t) =>
t.persistJSONBlob(@key, {
version: @version
time: Date.now()
value: newValue
})
fetchData: (callback) =>
throw new Error("Subclasses should override this method.")
module.exports = RefreshingJSONCache