-
Notifications
You must be signed in to change notification settings - Fork 6
/
patch.go
executable file
·51 lines (39 loc) · 1.24 KB
/
patch.go
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
package vn
import (
vnd "github.com/mfrachet/go-vdom-wasm/dom"
vnh "github.com/mfrachet/go-vdom-wasm/helpers"
)
func updateElement(parent vnd.DomNode, newNode Node, oldNode Node) {
if vnh.IsNil(newNode) {
Remove(*oldNode.GetElement())
} else if vnh.IsNil(oldNode) {
newElement := CreateIfNotExist(parent, newNode)
Append(parent, newElement)
} else if !newNode.IsSame(oldNode) {
newElement := CreateIfNotExist(parent, newNode)
oldElement := *oldNode.GetElement()
oldElement.ReplaceWith(newElement)
} else {
newNode.SetElement(*oldNode.GetElement())
newChildrenCount := newNode.ChildrenCount()
oldChildrenCount := oldNode.ChildrenCount()
max := vnh.Max(newChildrenCount, oldChildrenCount)
for i := 0; i < max; i++ {
oldChild := oldNode.ChildAt(i)
newChild := newNode.ChildAt(i)
updateElement(*oldNode.GetElement(), newChild, oldChild)
}
}
}
func Patch(oldNodeRef interface{}, newVnode Node) {
switch oldNodeRef.(type) {
case string:
rootNodeID := oldNodeRef.(string)
rootNode := vnd.GetDocument().QuerySelector(rootNodeID)
newElement := CreateIfNotExist(rootNode, newVnode)
Append(rootNode, newElement)
default:
oldVnode := oldNodeRef.(Node)
updateElement(*oldVnode.GetElement(), newVnode, oldVnode)
}
}