-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (37 loc) · 895 Bytes
/
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
'use strict'
const document = require('global/document')
const createElement = require('virtual-dom/create-element')
const diff = require('virtual-dom/diff')
const patch = require('virtual-dom/patch')
require('./ui/insert-styles')
const createWorker = require('./lib/worker')
const render = require('./ui')
const state = {
worker: null,
ready: false,
frame: null
}
const onError = (err) => {
console.error(err)
// todo: show in UI
}
const onReady = () => {
state.ready = true
rerender()
}
const onFrame = (frame) => {
state.frame = frame
rerender()
}
const actions = {onError, onReady, onFrame}
setTimeout(() => {
state.worker = createWorker(actions)
})
let tree = render(state, actions)
let root = createElement(tree)
document.body.appendChild(root)
const rerender = () => {
const newTree = render(state, actions)
root = patch(root, diff(tree, newTree))
tree = newTree
}