Skip to content

Commit

Permalink
Merge pull request #3 from NullVoxPopuli/master
Browse files Browse the repository at this point in the history
change strategy to to check for updates without a page reload
  • Loading branch information
topaxi authored Nov 28, 2018
2 parents 3688e35 + 98feeab commit eb119e7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 30 deletions.
50 changes: 43 additions & 7 deletions addon/components/service-worker-update-notify.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,53 @@
import Ember from 'ember'
import Component from '@ember/component';
import { isEmpty } from '@ember/utils';
import { task, timeout } from 'ember-concurrency';

import layout from '../templates/components/service-worker-update-notify'
import serviceWorkerHasUpdate from '../utils/service-worker-has-update'

export default Ember.Component.extend({
export default Component.extend({
layout,
pollingInterval: 1200000, // 20 minutes in ms

tagName: '',

hasUpdate: false,

didInsertElement() {
/**
* Delay attaching the updateHandler to prevent users from
* seeing a new build notification immediately on page load.
*/
setupTask: task(function*() {
const hasServiceWorker = 'serviceWorker' in navigator;
const supportsPromises = 'Promise' in window;

if (hasServiceWorker && supportsPromises) {
yield timeout(this.pollingInterval);
this._attachUpdateHandler();

const polling = this.pollingTask.perform();

this.set('polling', polling);
}
}).on('didInsertElement'),

pollingTask: task(function*() {
while (true) {
const reg = yield navigator.serviceWorker.register('/sw.js', { scope: '/' });

reg.update();

yield timeout(this.pollingInterval);
}
}),

_attachUpdateHandler() {
serviceWorkerHasUpdate().then(hasUpdate => {
this.set('hasUpdate', hasUpdate)
})
},
})
if (!isEmpty(this.polling)) {
this.polling.cancel();
}

this.set('hasUpdate', hasUpdate);
});
}
});
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
"name": "ember-service-worker-update-notify",
"version": "1.0.2",
"description": "Update notification for service workers",
"keywords": ["ember-addon", "ember-service-worker-plugin"],
"keywords": [
"ember-addon",
"ember-service-worker-plugin"
],
"license": "MIT",
"author": "Damian Senn <[email protected]>",
"directories": {
"doc": "doc",
"test": "tests"
},
"repository":
"https://github.com/topaxi/ember-service-worker-update-notify.git",
"repository": "https://github.com/topaxi/ember-service-worker-update-notify.git",
"scripts": {
"build": "ember build",
"start": "ember server",
Expand All @@ -32,13 +34,17 @@
"ember-cli-shims": "^1.1.0",
"ember-cli-sri": "^2.1.0",
"ember-cli-uglify": "^1.2.0",
"ember-concurrency": "^0.8.21",
"ember-disable-prototype-extensions": "^1.1.2",
"ember-export-application-global": "^2.0.0",
"ember-load-initializers": "^1.0.0",
"ember-resolver": "^4.0.0",
"ember-source": "~2.14.1",
"loader.js": "^4.2.3"
},
"peerDependencies": {
"ember-concurrency": "^0.8.21"
},
"engines": {
"node": "^4.5 || 6.* || >= 7.*"
},
Expand Down
46 changes: 26 additions & 20 deletions service-worker-registration/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { addSuccessHandler } from 'ember-service-worker/service-worker-registration'

window.hasServiceWorkerUpdate =
'Promise' in window
? new Promise(function hasServiceWorkerUpdate(resolve) {
addSuccessHandler(function emberServiceWorkerUpdateNotifyRegistration(
reg,
) {
reg.onupdatefound = function serviceWorkerHasFoundUpdate() {
const { installing } = reg
function hasServiceWorkerUpdate(resolve) {
addSuccessHandler(function(reg) {
reg.onupdatefound = function() {
const { installing } = reg;

installing.onstatechange = function installingServiceWorkerStateChange() {
if (installing.state === 'installed') {
resolve(navigator.serviceWorker.controller !== null)
}
}
}
})
})
: {
then: function() {
return false
},
installing.onstatechange = function() {
if (installing.state === 'activated') {
resolve(navigator.serviceWorker.controller !== null)
}
}
}
});
}

// https://caniuse.com/#search=Promise
// IE11 (2.5%) and Opera Mini (2.3%) do not support Promises
const arePromisesSupported = 'Promise' in window;
const disablePolyfill = ({
then: function() {
return false;
}
});

window.hasServiceWorkerUpdate =
arePromisesSupported
? new Promise(hasServiceWorkerUpdate)
: disablePolyfill

0 comments on commit eb119e7

Please sign in to comment.