mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-12 04:25:31 +08:00
0332d7264e
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
53 lines
No EOL
1.4 KiB
CoffeeScript
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 |