Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. IndexController.prototype._registerServiceWorker = function() {
  2.   if (!navigator.serviceWorker) return;
  3.  
  4.   var indexController = this;
  5.  
  6.   navigator.serviceWorker.register('/sw.js').then(function(reg) {
  7.     // TODO: if there's no controller, this page wasn't loaded
  8.     // via a service worker, so they're looking at the latest version.
  9.     // In that case, exit early
  10.     if (!navigator.serviceWorker.controller) return;
  11.  
  12.     // TODO: if there's an updated worker already waiting, call
  13.     // indexController._updateReady()
  14.     if (reg.waiting) {
  15.       indexController._updateReady();
  16.       return;
  17.     }
  18.  
  19.     // TODO: if there's an updated worker installing, track its
  20.     // progress. If it becomes "installed", call
  21.     // indexController._updateReady()
  22.     if (reg.installing) {
  23.       indexController._trackInstalling(reg.installing);
  24.       return;
  25.     }
  26.  
  27.     // TODO: otherwise, listen for new installing workers arriving.
  28.     // If one arrives, track its progress.
  29.     // If it becomes "installed", call
  30.     // indexController._updateReady()
  31.     reg.addEventListener('updatefound', function() {
  32.       indexController._trackInstalling(reg.installing);
  33.     });
  34.   });
  35. };
  36.  
  37. IndexController.prototype._trackInstalling = function(worker) {
  38.   var indexController = this;
  39.  
  40.   worker.addEventListener('statechange', function() {
  41.     if (worker.state == 'installed') {
  42.       indexController._updateReady();
  43.     }
  44.   });
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement