Skip to content

Commit

Permalink
Merge pull request #98 from BBVAEngineering/initializer-namespace
Browse files Browse the repository at this point in the history
feat: move files to the addon directory
  • Loading branch information
josex2r authored Jul 7, 2020
2 parents aa5ec0d + 5849274 commit 63e31c4
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 190 deletions.
44 changes: 44 additions & 0 deletions addon/instance-initializers/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getWithDefault } from '@ember/object';
import { debug } from '@ember/debug';

export function getConfig(appInstance) {
const config = appInstance.resolveRegistration('config:environment');
const isProdBuild = config.environment === 'production';

return {
isEnabled: getWithDefault(config, 'ember-cli-workbox.enabled', isProdBuild),
debugAddon: getWithDefault(config, 'ember-cli-workbox.debug', !isProdBuild),
swDestFile: getWithDefault(config, 'workbox.swDest', 'sw.js'),
autoRegister: getWithDefault(config, 'ember-cli-workbox.autoRegister', true)
};
}

export function initialize(appInstance) {
const swService = appInstance.lookup('service:service-worker');
const { isEnabled, debugAddon, swDestFile } = getConfig(appInstance);

swService.set('debug', debugAddon);

// first checks whether the browser supports service workers
if (swService.get('isSupported')) {
// Load and register pre-caching Service Worker
if (isEnabled) {
swService.register(swDestFile);
} else {
swService.unregisterAll();
}
} else {
debug('Service workers are not supported in this browser.');
}
}

export default {
name: 'ember-cli-workbox',
initialize(appInstance) {
const { autoRegister } = getConfig(appInstance);

if (autoRegister) {
initialize(appInstance);
}
}
};
146 changes: 146 additions & 0 deletions addon/services/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import Service from '@ember/service';
import Evented from '@ember/object/evented';
import { debug } from '@ember/debug';

/*
*
* Service worker states:
* "installing" - the install event has fired, but is not yet completed
* "installed" - install completed
* "activating" - the activate event has fired, but is not yet completed
* "activated" - fully active
* "redundant" - discarded. Either failed install, or it's been replaced by a newer version
*
* Events triggered:
* "error" - sw not registered
* "waiting" - new sw waiting for controlling page
* "activated" - the new sw is ready to respond
* "registrationComplete" - sw successfully registered
* "unregistrationComplete" - all sw are unregistered
*/
export default class ServiceWorker extends Service.extend(Evented) {
constructor() {
super(...arguments);

const sw = window.navigator.serviceWorker;
let isSupported = false;

if (sw) {
isSupported = [
'getRegistrations',
'register'
].every((func) => func in sw);
}

this.sw = sw;
this.isSupported = isSupported;
}

_log(message) {
if (this.debug) {
debug(`ember-cli-workbox: ${message}`);
}
}

async register(swFile) {
try {
const registration = await this.sw.register(swFile);

return this._onRegistration(registration);
} catch (error) {
this.trigger('error', error);
this._log('Service Worker registration failed: ', error);

throw error;
}
}

/*
* Utility function that unregisters SW, but you still need to reload to see SW removed completely
* This does not delete items in Cache
*/
async unregisterAll() {
const registrations = await this.sw.getRegistrations();

await Promise.all(
registrations.map(async(reg) => {
const boolean = await reg.unregister();

if (boolean) {
this._log(`${reg} unregistered`);
} else {
this._log(`Error unregistering ${reg}`);
}
})
);

this.trigger('unregistrationComplete');
this._log('Unregistrations complete');
}

_onRegistration(registration) {
this._log(`Registration succeeded. Scope is ${registration.scope}`);
this.trigger('registrationComplete');

if (!registration) {
return;
}

if (registration.waiting) {
// SW is waiting to activate. Can occur if multiple clients open and
// one of the clients is refreshed.
this._waiting(registration);
}

// We are currently controlled so a new SW may be found...
// Add a listener in case a new SW is found,
registration.addEventListener('updatefound', () => {
const installingWorker = registration.installing;

if (!installingWorker) {
return;
}

if (installingWorker.state === 'installed') {
this._checkSWInstalled(installingWorker, registration);
} else {
installingWorker.addEventListener('statechange', () => {
this._checkSWInstalled(installingWorker, registration);
});
}
});
}

_checkSWInstalled(installingWorker, registration) {
switch (installingWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
this._log('New content is available and will be used when all tabs for this page are closed.');

// Execute callback
this._waiting(registration);
} else {
// At this point, everything has been precached.
// It's the perfect time to display a "Content is cached for offline use." message.
this._log('New serviceworker is controlling page. Content is now available offline!');
}
break;
default:
break;
}
}

_waiting(registration) {
this._log('New serviceworker is waiting to activate. New or updated content is available.');
this.trigger('waiting', registration);

registration.waiting.addEventListener('statechange', (event) => {
if (event.target.state === 'activated') {
this.trigger('activated', registration);
}
});
}
}
45 changes: 1 addition & 44 deletions app/instance-initializers/sw.js
Original file line number Diff line number Diff line change
@@ -1,44 +1 @@
import { getWithDefault } from '@ember/object';
import { debug } from '@ember/debug';

export function getConfig(appInstance) {
const config = appInstance.resolveRegistration('config:environment');
const isProdBuild = config.environment === 'production';

return {
isEnabled: getWithDefault(config, 'ember-cli-workbox.enabled', isProdBuild),
debugAddon: getWithDefault(config, 'ember-cli-workbox.debug', !isProdBuild),
swDestFile: getWithDefault(config, 'workbox.swDest', 'sw.js'),
autoRegister: getWithDefault(config, 'ember-cli-workbox.autoRegister', true)
};
}

export function initialize(appInstance) {
const swService = appInstance.lookup('service:service-worker');
const { isEnabled, debugAddon, swDestFile } = getConfig(appInstance);

swService.set('debug', debugAddon);

// first checks whether the browser supports service workers
if (swService.get('isSupported')) {
// Load and register pre-caching Service Worker
if (isEnabled) {
swService.register(swDestFile);
} else {
swService.unregisterAll();
}
} else {
debug('Service workers are not supported in this browser.');
}
}

export default {
name: 'ember-cli-workbox',
initialize(appInstance) {
const { autoRegister } = getConfig(appInstance);

if (autoRegister) {
initialize(appInstance);
}
}
};
export { default, initialize } from 'ember-cli-workbox/instance-initializers/sw';
147 changes: 1 addition & 146 deletions app/services/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,146 +1 @@
import Service from '@ember/service';
import Evented from '@ember/object/evented';
import { debug } from '@ember/debug';

/*
*
* Service worker states:
* "installing" - the install event has fired, but is not yet completed
* "installed" - install completed
* "activating" - the activate event has fired, but is not yet completed
* "activated" - fully active
* "redundant" - discarded. Either failed install, or it's been replaced by a newer version
*
* Events triggered:
* "error" - sw not registered
* "waiting" - new sw waiting for controlling page
* "activated" - the new sw is ready to respond
* "registrationComplete" - sw successfully registered
* "unregistrationComplete" - all sw are unregistered
*/
export default class ServiceWorker extends Service.extend(Evented) {
constructor() {
super(...arguments);

const sw = window.navigator.serviceWorker;
let isSupported = false;

if (sw) {
isSupported = [
'getRegistrations',
'register'
].every((func) => func in sw);
}

this.sw = sw;
this.isSupported = isSupported;
}

_log(message) {
if (this.debug) {
debug(`ember-cli-workbox: ${message}`);
}
}

async register(swFile) {
try {
const registration = await this.sw.register(swFile);

return this._onRegistration(registration);
} catch (error) {
this.trigger('error', error);
this._log('Service Worker registration failed: ', error);

throw error;
}
}

/*
* Utility function that unregisters SW, but you still need to reload to see SW removed completely
* This does not delete items in Cache
*/
async unregisterAll() {
const registrations = await this.sw.getRegistrations();

await Promise.all(
registrations.map(async(reg) => {
const boolean = await reg.unregister();

if (boolean) {
this._log(`${reg} unregistered`);
} else {
this._log(`Error unregistering ${reg}`);
}
})
);

this.trigger('unregistrationComplete');
this._log('Unregistrations complete');
}

_onRegistration(registration) {
this._log(`Registration succeeded. Scope is ${registration.scope}`);
this.trigger('registrationComplete');

if (!registration) {
return;
}

if (registration.waiting) {
// SW is waiting to activate. Can occur if multiple clients open and
// one of the clients is refreshed.
this._waiting(registration);
}

// We are currently controlled so a new SW may be found...
// Add a listener in case a new SW is found,
registration.addEventListener('updatefound', () => {
const installingWorker = registration.installing;

if (!installingWorker) {
return;
}

if (installingWorker.state === 'installed') {
this._checkSWInstalled(installingWorker, registration);
} else {
installingWorker.addEventListener('statechange', () => {
this._checkSWInstalled(installingWorker, registration);
});
}
});
}

_checkSWInstalled(installingWorker, registration) {
switch (installingWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
this._log('New content is available and will be used when all tabs for this page are closed.');

// Execute callback
this._waiting(registration);
} else {
// At this point, everything has been precached.
// It's the perfect time to display a "Content is cached for offline use." message.
this._log('New serviceworker is controlling page. Content is now available offline!');
}
break;
default:
break;
}
}

_waiting(registration) {
this._log('New serviceworker is waiting to activate. New or updated content is available.');
this.trigger('waiting', registration);

registration.waiting.addEventListener('statechange', (event) => {
if (event.target.state === 'activated') {
this.trigger('activated', registration);
}
});
}
}
export { default } from 'ember-cli-workbox/services/service-worker';

0 comments on commit 63e31c4

Please sign in to comment.