-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwa-install.js
76 lines (69 loc) · 2.09 KB
/
pwa-install.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
/**
* @copyright 2023 Chris Zuber <admin@kernvalley.us>
*/
export const supported = 'serviceWorker' in navigator && navigator.serviceWorker.register instanceof Function;
export const controller = new AbortController();
export const signal = controller.signal;
export const aborted = new Promise(resolve => {
if (signal.aborted) {
resolve();
} else {
signal.addEventListener('abort', () => resolve(), { once: true });
}
});
export const promise = new Promise((resolve, reject) => {
if (! supported) {
controller.abort();
reject(new DOMException('Service Worker not supported'));
} else {
globalThis.addEventListener('beforeinstallprompt', event => {
event.preventDefault();
resolve(event);
}, { signal, once: true });
controller.signal.addEventListener('abort', () => {
reject(new DOMException('Installation aborted'));
}, { once: true });
}
});
export async function registerButton(el) {
if (! (el instanceof HTMLButtonElement)) {
throw new DOMException('Not a <button>');
} else if (signal.aborted) {
el.disabled = true;
throw new DOMException('PWA installation aborted');
} else {
el.disabled = true;
const event = await promise;
const platforms = event.platforms;
el.disabled = false;
return await new Promise((resolve, reject) => {
let resolved = false;
aborted.then(() => {
el.disabled = true;
if (! resolved) {
resolved = true;
reject(new DOMException('Installation aborted'));
}
});
el.addEventListener('click', async () => {
const afterInstall = new CustomEvent('afterinstallprompt');
afterInstall.platforms = platforms;
afterInstall.userResponse = await Promise.all([
event.userResponse,
event.prompt(),
]).then(([resp]) => resp);
el.dispatchEvent(afterInstall);
el.disabled = true;
if (afterInstall.userResponse === 'accepted') {
resolved = true;
resolve({ userResponse: afterInstall.userResponse, platforms });
controller.abort();
} else {
resolved = true;
reject(new DOMException('User aborted installation'));
controller.abort();
}
}, { signal });
});
}
}