-
Notifications
You must be signed in to change notification settings - Fork 2
/
service-worker.js
164 lines (148 loc) · 4.23 KB
/
service-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
self.importScripts("/elm-worker.js");
self.importScripts("/elm-ports-indexeddb.js");
var app = Elm.Worker.init();
ElmPortsIndexedDB.bind(app);
// set up broadcast channel
const channel = new BroadcastChannel("sw-messages");
app.ports.postMessageInternal.subscribe(msg => {
channel.postMessage(msg);
});
app.ports.getVapidKey.subscribe(() => {
fetch("vapid-public-key")
.then(response => {
return response.text();
})
.then(function(text) {
app.ports.onVapidkeyResult.send(text);
});
});
app.ports.sendLogin.subscribe(opts => {
fetch("/api/login", {
method: "POST",
body: JSON.stringify(opts)
})
.then(response => {
return response.json();
})
.then(function(json) {
app.ports.onLoginResult.send(json);
});
});
app.ports.subscribeInternal.subscribe(key => {
var options = {
userVisibleOnly: true,
applicationServerKey: key
};
registration.pushManager
.subscribe(options)
.then(subscription => {
app.ports.onNewSubscriptionInternal.send(subscription.toJSON());
})
.catch(x => {
console.log(x);
});
});
app.ports.uploadSubscription.subscribe(opts => {
fetch("/api/subscription", {
method: "POST",
headers: new Headers({
Authorization: opts.auth,
"Content-Type": "application/json"
}),
body: JSON.stringify({
endpoint: opts.payload.endpoint,
auth: opts.payload.auth,
p256dh: opts.payload.p256dh
})
}).then(response => {
var result = response.status == 201;
app.ports.getSubscriptionReply.send(result);
});
});
app.ports.uploadPost.subscribe(opts => {
fetch("/api/posts", {
method: "POST",
headers: new Headers({
Authorization: opts.auth,
"Content-Type": "application/json"
}),
body: JSON.stringify(opts.payload)
}).then(response => {
var result = {
status: response.status
};
if (response.status == 201) {
response.text().then(id => {
result.id = id;
app.ports.uploadPostReply.send(result);
});
} else {
app.ports.uploadPostReply.send(result);
}
});
});
app.ports.getSubscription.subscribe(opts => {
fetch("/api/subscription", {
headers: new Headers({
Authorization: opts.auth
})
}).then(response => {
var result = response.status == 204;
app.ports.getSubscriptionReply.send(result);
});
});
self.navigator.permissions.query({ name: "notifications" }).then(ps => {
app.ports.onPermissionChangeInternal.send(ps.state);
ps.onchange = ev => {
console.log(ev.target);
app.ports.onPermissionChangeInternal.send(ev.target.state);
};
});
var CACHE_NAME = "elm-pwa-example-cache-v1";
var urlsToCache = [
"/",
"/index.js",
"/elm.js",
"/elm-worker.js"
];
self.addEventListener("install", function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener("activate", function(event) {
// TODO try to create a Promise context in the SW-Elm
event.waitUntil(self.clients.claim());
});
self.addEventListener("fetch", function(event) {
event.respondWith(
caches.match(event.request).then(function(response) {
// Cache hit - return response
if (response) {
return response;
}
return fetch(event.request);
})
);
});
self.addEventListener("sync", event => {
app.ports.onSync.send();
});
self.addEventListener("push", function(event) {
console.log("[Service Worker] Push Received.");
var syncAndNotify = sync().then(notify());
event.waitUntil(syncAndNotify);
});
function notify() {
const title = "Push Codelab";
const options = {
body: "Yay it works."
};
self.registration.showNotification(title, options);
}
self.addEventListener("message", event => {
app.ports.onMessageInternal.send(event.data);
});