-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathclient.js
More file actions
108 lines (102 loc) · 3.04 KB
/
client.js
File metadata and controls
108 lines (102 loc) · 3.04 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import generateTree from '../shared/generateTree'
import { loadPlugins } from '../shared/plugins'
import context, { generateContext } from './context'
import rerender from './rerender'
import router from './router'
const client = {}
client.initialized = false
client.initializer = null
client.instances = {}
context.instances = client.instances
client.initiationQueue = []
client.renewalQueue = []
client.hydrationQueue = []
client.realHydrationQueue = []
client.virtualDom = {}
client.selector = null
client.events = {}
client.generateContext = generateContext
client.renderQueue = null
client.currentMeta = { body: {}, html: {}, window: {} }
client.nextMeta = { body: {}, html: {}, window: {} }
client.currentHead = []
client.nextHead = []
client.head = document.head
client.body = document.body
client.update = function update() {
if (client.initialized) {
clearInterval(client.renderQueue)
client.renderQueue = setTimeout(async () => {
const scope = client
scope.context = context
scope.plugins = loadPlugins(scope)
client.initialized = false
client.renewalQueue = []
try {
client.nextVirtualDom = await generateTree(client.initializer(), scope)
rerender()
client.processLifecycleQueues()
} catch (error) {
client.skipHotReplacement = true
if (context.catch) {
context.catch(error)
} else {
throw error
}
}
}, 16)
}
}
client.processLifecycleQueues = async function processLifecycleQueues() {
if (!client.initialized) {
client.initialized = true
}
let shouldUpdate = false
let shouldScroll = router._hash
while (client.initiationQueue.length) {
const instance = client.initiationQueue.shift()
instance.initiate && (await instance.initiate())
instance.initiated = true
instance.launch && instance.launch()
shouldUpdate = true
if (instance._attributes.route && shouldScroll) {
const element = document.getElementById(router._hash)
if (element) {
element.scrollIntoView({ behavior: 'smooth' })
}
shouldScroll = false
}
}
shouldUpdate && client.update()
shouldUpdate = false
while (client.realHydrationQueue.length) {
shouldUpdate = true
const instance = client.realHydrationQueue.shift()
instance.hydrate && (await instance.hydrate())
instance.hydrated = true
}
shouldUpdate && client.update()
shouldUpdate = false
while (client.hydrationQueue.length) {
shouldUpdate = true
const instance = client.hydrationQueue.shift()
client.realHydrationQueue.push(instance)
}
shouldUpdate && client.update()
for (const key in client.instances) {
const instance = client.instances[key]
if (!client.renewalQueue.includes(instance) && !instance.terminated) {
instance.terminate && (await instance.terminate())
if (instance.persistent) {
instance.terminated = true
} else {
delete client.instances[key]
}
}
}
router._changed = false
}
if (module.hot) {
client.klasses = {}
}
export default client