History

Service worker refactoring

Jasper van Merle committed 7 years ago

Commit ec8616e1 parent 8ed1f4a2 files · +29 −41Browse files

Showing the changes to assets/javascripts (1 of 2 files). Show all

Unified · Split

assets/javascripts/app/serviceworker.coffee+5−13
@@ -6,7 +6,6 @@ class app.ServiceWorker · 5 unmodified lines
66
77 constructor: ->
88 @registration = null
9− @installingRegistration = null
109 @notifyUpdate = true
1110
1211 navigator.serviceWorker.register(app.config.service_worker_path, {scope: '/'})
@@ -16,37 +15,30 @@ class app.ServiceWorker · 3 unmodified lines
1615 update: ->
1716 return unless @registration
1817 @notifyUpdate = true
19− return @doUpdate()
18+ return @registration.update().catch(->)
2019
2120 updateInBackground: ->
2221 return unless @registration
2322 @notifyUpdate = false
24− return @doUpdate()
23+ return @registration.update().catch(->)
2524
2625 reload: ->
2726 return @updateInBackground().then(() -> app.reboot())
2827
29− doUpdate: ->
30− return @registration.update().catch(->)
31−
3228 updateRegistration: (registration) ->
33− $.off @registration, 'updatefound', @onUpdateFound if @registration
34− $.off @installingRegistration, 'statechange', @onStateChange if @installingRegistration
35−
3629 @registration = registration
37− @installingRegistration = null
38−
3930 $.on @registration, 'updatefound', @onUpdateFound
4031 return
4132
4233 onUpdateFound: () =>
34+ $.off @installingRegistration, 'statechange', @onStateChange() if @installingRegistration
4335 @installingRegistration = @registration.installing
4436 $.on @installingRegistration, 'statechange', @onStateChange
4537 return
4638
4739 onStateChange: () =>
48− if @installingRegistration.state == 'installed' and navigator.serviceWorker.controller
49− @updateRegistration(@installingRegistration)
40+ if @installingRegistration and @installingRegistration.state == 'installed' and navigator.serviceWorker.controller
41+ @installingRegistration = null
5042 @onUpdateReady()
5143 return
5244