Skip to content

Commit b47cfe2

Browse files
author
edencoder
committed
amend issues
1 parent 9cd96bf commit b47cfe2

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
lines changed

bundles/serviceworker/public/js/bootstrap.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ class EdenServiceworkerBuilder extends Events {
3535
this.registration = await navigator.serviceWorker.register('/sw.js');
3636
this.ready = await navigator.serviceWorker.ready;
3737

38-
// Await subscription
39-
this.subscription = await this.ready.pushManager.getSubscription();
40-
4138
// Create message channel
4239
this.channel = new MessageChannel();
4340

bundles/serviceworker/public/js/serviceworker.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,66 @@ class EdenServiceworker extends Events {
4444
// require offline
4545
this.offline = new EdenOffline(this);
4646
}
47+
48+
// send event
49+
self.addEventListener('push', (event) => {
50+
// body
51+
let body = {};
52+
53+
// try/catch
54+
try {
55+
// get body
56+
body = JSON.parse(event.data.text());
57+
} catch (e) {}
58+
59+
// emit to this
60+
this.emit(`push.${body.type}`, body.data, event);
61+
});
62+
63+
// on push notification
64+
this.on('push.notification', (data, event) => {
65+
// wait for push to send
66+
event.waitUntil(self.registration.showNotification(data.title, data.opts));
67+
});
68+
69+
// on notification click
70+
self.onnotificationclick = (event) => {
71+
// close notification
72+
event.notification.close();
73+
74+
// This looks to see if the current is already open and
75+
// focuses if it is
76+
event.waitUntil(clients.matchAll({
77+
type : 'window',
78+
}).then((clientList) => {
79+
// data
80+
const data = event.notification.data || {};
81+
82+
// find client
83+
const client = clientList.find((c) => {
84+
// client
85+
return 'focus' in c && (!data.url || c.url === data.url);
86+
});
87+
88+
// focus
89+
if (client) {
90+
// focus
91+
return client.focus();
92+
}
93+
94+
// open window
95+
if (clients.openWindow) {
96+
// open url
97+
clients.openWindow(data.url || '/');
98+
}
99+
}));
100+
};
47101
}
48102

49103
/**
50104
* return config
51105
*
52-
* @param {*} noCache
106+
* @param {*} noCache
53107
*/
54108
async config(noCache = false) {
55109
// return config

bundles/serviceworker/tasks/serviceworker.js renamed to bundles/serviceworker/tasks/serviceworker.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
2-
// Require dependencies
3-
const config = require('config');
4-
51
/**
62
* Build serviceworker task class
73
*
84
* @task serviceworker
95
*/
10-
class ServiceworkerTask {
6+
export default class ServiceworkerTask {
117
/**
128
* Construct serviceworker task class
139
*
14-
* @param {edenGulp} runner
10+
* @param {edenGulp} cli
1511
*/
16-
constructor(runner) {
12+
constructor(cli) {
1713
// Set private variables
18-
this._runner = runner;
14+
this.cli = cli;
1915
this._b = null;
2016

2117
// Bind methods
@@ -35,20 +31,20 @@ class ServiceworkerTask {
3531

3632
dest : `${global.appRoot}/www`,
3733
cache : `${global.appRoot}/.edenjs/.cache/serviceworker.json`,
38-
config : config.get('serviceworker.config') || {},
39-
domain : config.get('domain'),
34+
config : this.cli.get('config.serviceworker.config') || {},
35+
domain : this.cli.get('config.domain'),
4036
imports : global.importLocations,
41-
version : config.get('version'),
42-
browsers : config.get('browserlist'),
37+
version : this.cli.get('config.version'),
38+
browsers : this.cli.get('config.browserlist'),
4339
polyfill : require.resolve('@babel/polyfill'),
44-
sourceMaps : config.get('environment') === 'dev' && !config.get('noSourcemaps'),
40+
sourceMaps : this.cli.get('config.environment') === 'dev',
4541

4642
appRoot : global.appRoot,
4743
edenRoot : global.edenRoot,
4844
};
4945

5046
// run in thread
51-
return this._runner.thread(this.thread, opts);
47+
return this.cli.thread(this.thread, opts);
5248
}
5349

5450
/**
@@ -149,16 +145,6 @@ class ServiceworkerTask {
149145
*/
150146
watch() {
151147
// Return files
152-
return [
153-
'public/js/serviceworker.js',
154-
'public/js/serviceworker/**/*',
155-
];
148+
return 'public/js/serviceworker/**/*';
156149
}
157-
}
158-
159-
/**
160-
* Export serviceworker task
161-
*
162-
* @type {ServiceworkerTask}
163-
*/
164-
module.exports = ServiceworkerTask;
150+
}

0 commit comments

Comments
 (0)