forked from nullstack/nullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
82 lines (74 loc) · 2.29 KB
/
client.js
File metadata and controls
82 lines (74 loc) · 2.29 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
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.hydrated = false;
client.initializer = null;
client.instances = {};
context.instances = client.instances;
client.initiationQueue = [];
client.renewalQueue = [];
client.hydrationQueue = [];
client.virtualDom = {};
client.selector = null;
client.events = {};
client.generateContext = generateContext;
client.renderQueue = null;
client.update = async function () {
if (client.initialized) {
clearInterval(client.renderQueue);
client.renderQueue = setTimeout(async () => {
const scope = client;
scope.context = context;
scope.plugins = loadPlugins(scope);
client.initialized = false;
client.initiationQueue = [];
client.renewalQueue = [];
client.hydrationQueue = [];
client.nextVirtualDom = await generateTree(client.initializer(), scope);
rerender(client.selector);
client.virtualDom = client.nextVirtualDom;
client.nextVirtualDom = null;
client.processLifecycleQueues();
}, 16);
}
}
client.processLifecycleQueues = async function () {
if (!client.initialized) {
client.initialized = true;
client.hydrated = true;
}
const initiationQueue = client.initiationQueue;
const hydrationQueue = client.hydrationQueue;
for (const instance of initiationQueue) {
instance.initiate && await instance.initiate();
instance._self.initiated = true;
instance.launch && instance.launch()
}
if (initiationQueue.length) {
client.update();
}
for (const instance of hydrationQueue) {
instance.hydrate && await instance.hydrate();
instance._self.hydrated = true;
}
if (hydrationQueue.length) {
client.update();
}
for (const key in client.instances) {
const instance = client.instances[key];
if (!client.renewalQueue.includes(instance) && !instance._self.terminated) {
instance.terminate && await instance.terminate();
if (instance._self.persistent) {
instance._self.terminated = true
} else {
delete client.instances[key];
}
}
}
router._changed = false;
}
export default client;