-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from NullVoxPopuli/master
change strategy to to check for updates without a page reload
- Loading branch information
Showing
3 changed files
with
78 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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.*" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|