-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (46 loc) · 986 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
48
49
50
51
52
53
54
55
56
57
58
import { h, create, diff, patch } from 'virtual-dom'
class Widget {
constructor() {
this.type = 'Widget' // must be the string 'Widget'
}
}
class ACCWidget extends Widget {
constructor(num) {
super()
this.num = num
}
init() {
return create(
h('div', String(this.num))
)
}
update(previous) {
this.num += previous.num
return this.init()
}
}
let $rootNode
let currentVTree
let newVTree
const newOnClick = (num) => () => {
const nextVTree = newVTree(num)
const patches = diff(currentVTree, nextVTree)
patch($rootNode, patches)
currentVTree = nextVTree
}
const onClickAdd = newOnClick(1)
const onClickSub = newOnClick(-1)
newVTree = (num) => {
return h('div', [
h('div', new ACCWidget(num)),
h('button', {
onclick: onClickAdd,
}, '+'),
h('button', {
onclick: onClickSub,
}, '-'),
])
}
currentVTree = newVTree(0)
$rootNode = create(currentVTree)
document.body.appendChild($rootNode)