Mailspring/internal_packages/worker-sync/lib/refreshing-json-cache.coffee
Drew Regitsky 0332d7264e fix(contacts): move contact rank fetching to sync workers, refactor
Summary:
Fixes bug where contact ranking was not being fetched, and refactors the refreshing
of contact ranks. Moves periodic refreshing of the database-stored ranks to the sync
workers so it occurs in the background, once per account. Refactors JSON cache code
accordingly.

Test Plan: manual

Reviewers: evan, bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D2137
2015-10-09 12:40:36 -07:00

53 lines
No EOL
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.findJSONObject(@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.persistJSONObject(@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.persistJSONObject(@key, {
version: @version
time: Date.now()
value: newValue
})
fetchData: (callback) =>
throw new Error("Subclasses should override this method.")
module.exports = RefreshingJSONCache