forked from WICG/web-app-launch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill.js
92 lines (78 loc) · 3.07 KB
/
polyfill.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
/* This work is licensed under the W3C Software and Document License
* (http://www.w3.org/Consortium/Legal/2015/copyright-software-and-document).
*/
// To be included in the Service Worker.
class LaunchEvent extends ExtendableEvent {
constructor(type, init) {
super(type, init);
this.url = init ? init.url || null : null;
this.clientId = init ? init.clientId || null : null;
this._handlePending = null;
}
handleLaunch(promise) {
if (this._handlePending !== null)
throw new InvalidStateError('handleLaunch has already been called');
// Extend the lifetime of the event until the promise completes.
// This is not actually allowed on a script-constructed ExtendableEvent.
//this.waitUntil(promise);
// Wait until the promise resolves before applying the default.
this._handlePending = promise;
}
}
// Listen for a message from simulatenavigate.html.
self.addEventListener('message', async event => {
if (event.data.tag !== 'polyfill_simulatenavigate')
return;
// |url| is relative to Service Worker scope.
const url = new URL(event.data.url, self.registration.scope).href;
const target = event.data.target;
console.log('[Polyfill]: simulatenavigate message: source =', event.source.id,
', url =', url, ', target =', target);
if (target === 'nolaunch') {
// Like target === 'blank', but do not dispatch the launch event (simulate a
// user explicitly asking for a new context).
clients.openWindow(url);
return;
}
// Create and fire a LaunchEvent.
// If target is self, pass the client ID into the launch event, so it can
// identify the sending window. If target is blank, this navigation is not
// tied to any particular context.
const clientId = target === 'self' ? event.source.id : null;
const launchEvent =
new LaunchEvent('launch', {url, clientId, cancelable: true});
self.dispatchEvent(launchEvent);
if (launchEvent.defaultPrevented) {
console.log('[Polyfill]: Cancelling default behavior (preventDefault)');
// Do not apply the default behaviour, regardless of any pending promise.
return;
}
defaultBehavior = () => {
if (target === 'self')
event.source.navigate(url);
else
clients.openWindow(url);
}
if (launchEvent._handlePending === null) {
console.log('[Polyfill]: Proceeding with default behavior '
+ '(no preventDefault / handleLaunch)');
// Launch handler did not queue a custom handler. Immediately apply the
// default behavior.
defaultBehavior();
return;
}
try {
await launchEvent._handlePending;
} catch (e) {
// Promise failed. Apply the default behavior.
// NOTE: If we do not want to do this (i.e., we don't care whether it
// succeeds or fails), we can just use preventDefault instead of
// handleLaunch taking a promise.
console.log('[Polyfill]: Proceeding with default behavior '
+ '(handleLaunch rejected)');
defaultBehavior();
return;
}
console.log('[Polyfill]: Cancelling default behavior '
+ '(handleLaunch fulfilled)');
});