소스 검색

Check for updates when AppCache is disabled

Thibaut 11 년 전
부모
커밋
a7d212d4de
3개의 변경된 파일41개의 추가작업 그리고 19개의 파일을 삭제
  1. 1 8
      assets/javascripts/app/app.coffee
  2. 1 11
      assets/javascripts/app/appcache.coffee
  3. 39 0
      assets/javascripts/app/update_checker.coffee

+ 1 - 8
assets/javascripts/app/app.coffee

@@ -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()

+ 1 - 11
assets/javascripts/app/appcache.coffee

@@ -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

+ 39 - 0
assets/javascripts/app/update_checker.coffee

@@ -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