-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
93 lines (76 loc) · 2.42 KB
/
index.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
const DOMContentLoad = new Promise((resolve) => {
document.addEventListener("DOMContentLoaded", resolve);
})
function networkIdleCallback(fn, options = { timeout: 0 }) {
// Call the function immediately if required features are absent
if (
!'MessageChannel' in window ||
!'serviceWorker' in navigator ||
!navigator.serviceWorker.controller
) {
DOMContentLoad.then(() => fn({ didTimeout: false }))
return
}
const messageChannel = new MessageChannel();
navigator.serviceWorker.controller
.postMessage(
'NETWORK_IDLE_ENQUIRY',
[messageChannel.port2],
)
const timeoutId = setTimeout(() => {
const cbToPop = networkIdleCallback.__callbacks__
.find(cb => cb.id === timeoutId)
networkIdleCallback.__popCallback__(cbToPop, true)
}, options.timeout)
networkIdleCallback.__callbacks__.push({
id: timeoutId,
fn,
timeout: options.timeout,
})
messageChannel.port1.addEventListener('message', handleMessage);
messageChannel.port1.start();
return timeoutId;
}
function cancelNetworkIdleCallback(callbackId) {
clearTimeout(callbackId)
networkIdleCallback.__callbacks__ = networkIdleCallback.__callbacks__
.filter(cb => cb.id !== callbackId)
}
networkIdleCallback.__popCallback__ = (callback, didTimeout) => {
DOMContentLoad.then(() => {
const cbToPop = networkIdleCallback.__callbacks__
.find(cb => cb.id === callback.id)
if (cbToPop) {
cbToPop.fn({ didTimeout })
clearTimeout(cbToPop.id)
networkIdleCallback.__callbacks__ = networkIdleCallback.__callbacks__.filter(
cb => cb.id !== callback.id)
}
})
}
networkIdleCallback.__callbacks__ = []
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistration()
.then((registration) => {
if (!registration) {
console.warn('`networkIdleCallback` was called before a service worker was registered. `networkIdleCallback` is ineffective without a working service worker')
}
})
navigator.serviceWorker.addEventListener('message', handleMessage)
}
function handleMessage(event) {
if (!event.data)
return
switch (event.data) {
case 'NETWORK_IDLE_ENQUIRY_RESULT_IDLE':
case 'NETWORK_IDLE_CALLBACK':
networkIdleCallback.__callbacks__.forEach(callback => {
networkIdleCallback.__popCallback__(callback, false)
})
break;
}
}
module.exports = {
networkIdleCallback,
cancelNetworkIdleCallback,
}