Check for updates when AppCache is disabled

This commit is contained in:
Thibaut 2015-02-01 17:51:45 -05:00
parent 977e991ff1
commit a7d212d4de
3 changed files with 41 additions and 19 deletions

View file

@ -96,14 +96,7 @@
new app.views.Notif 'Share', autoHide: null if visitCount is 5
new app.views.Notif 'Thanks', autoHide: null if visitCount is 10
new app.views.News()
@checkForDocUpdates()
checkForDocUpdates: ->
if @settings.get('autoUpdate')
@docs.updateInBackground()
else
@docs.checkForUpdates (i) ->
new app.views.Notif 'UpdateDocs', autoHide: null if i > 0
@updateChecker = new app.UpdateChecker()
reload: ->
@docs.clearCache()

View file

@ -13,9 +13,6 @@ class app.AppCache
$.on @cache, 'progress', @onProgress
$.on @cache, 'updateready', @onUpdateReady
@lastCheck = Date.now()
$.on window, 'focus', @checkForUpdate
update: ->
try @cache.update() catch
return
@ -26,17 +23,10 @@ class app.AppCache
@update()
return
checkForUpdate: =>
if Date.now() - @lastCheck > 86400e3
@lastCheck = Date.now()
@update()
return
onProgress: (event) =>
@trigger 'progress', event
return
onUpdateReady: =>
new app.views.Notif 'UpdateReady', autoHide: null unless @reloading
@trigger 'updateready'
@trigger 'updateready' unless @reloading
return

View file

@ -0,0 +1,39 @@
class app.UpdateChecker
constructor: ->
@lastCheck = Date.now()
$.on window, 'focus', @checkForUpdate
app.appCache.on 'updateready', @onUpdateReady if app.appCache
@checkDocs()
check: ->
if app.appCache
app.appCache.update()
else
ajax
url: $('script[src*="application"]').getAttribute('src')
dataType: 'application/javascript'
error: (_, xhr) => @onUpdateReady() if xhr.status is 404
return
onUpdateReady: ->
new app.views.Notif 'UpdateReady', autoHide: null
return
checkDocs: ->
if app.settings.get('autoUpdate')
app.docs.updateInBackground()
else
app.docs.checkForUpdates (i) => @onDocsUpdateReady() if i > 0
return
onDocsUpdateReady: ->
new app.views.Notif 'UpdateDocs', autoHide: null
return
onFocus: =>
if Date.now() - @lastCheck > 21600e3
@lastCheck = Date.now()
@check()
return