-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathlazy.js
More file actions
37 lines (32 loc) · 724 Bytes
/
lazy.js
File metadata and controls
37 lines (32 loc) · 724 Bytes
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
import client from './client'
const queue = {}
let next = null
async function preload() {
cancelIdleCallback(next)
let entry = Object.entries(queue)[0]
if (!entry) return
let loader = entry[1]
if (!loader) return
await loader.load()
next = requestIdleCallback(preload)
}
window.addEventListener('blur', () => {
preload()
})
window.addEventListener('focus', () => {
cancelIdleCallback(next)
})
export default function lazy(hash, importer) {
const loader = {
load: async () => {
const mod = await importer()
loader.component = mod.default
delete queue[hash]
client.update()
},
component: null,
__nullstack_lazy: true
}
queue[hash] = loader
return loader
}