forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
41 lines (35 loc) · 947 Bytes
/
loader.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
// For things that must only happen once
export function loader(promiseCreator) {
let promise
return (...args) => {
if (!promise) {
promise = promiseCreator(...args).then(res => {
promise.loaded = true
return res
})
}
return promise
}
}
export const bodyLoader = loader(() => {
return new Promise(resolve => {
if (document.readyState === 'complete') {
return resolve()
}
window.addEventListener('load', () => {
resolve()
})
})
})
export const interactiveLoader = loader(() => {
return new Promise(resolve => {
if (document.readyState !== 'loading') {
return resolve()
}
document.addEventListener('readystatechange', () => {
if (document.readyState !== 'loading') {
return resolve()
}
})
})
})