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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
package tui
import (
"reflect"
"github.com/grindlemire/go-tui/internal/debug"
)
type mountKey struct {
parent Component
key any
}
type mountState struct {
cache map[mountKey]Component
cleanups map[mountKey]func()
activeKeys map[mountKey]bool
persistKeys map[mountKey]bool
}
func newMountState() *mountState {
return &mountState{
cache: make(map[mountKey]Component),
cleanups: make(map[mountKey]func()),
activeKeys: make(map[mountKey]bool),
persistKeys: make(map[mountKey]bool),
}
}
type PropsUpdater interface {
UpdateProps(fresh Component)
}
func (a *App) mount(parent Component, key any, factory func() Component) *Element {
app := a
ms := app.mounts
k := mountKey{parent: parent, key: key}
if ms.activeKeys[k] {
debug.Log("Mount: duplicate mount key %v (parent %T) within one render pass", key, parent)
}
ms.activeKeys[k] = true
instance, cached := ms.cache[k]
var fresh Component
if cached {
if _, ok := instance.(PropsUpdater); ok {
fresh = factory()
if reflect.TypeOf(fresh) != reflect.TypeOf(instance) {
debug.Log("Mount: key collision at %v: cached %T, factory produced %T; remounting", key, instance, fresh)
ms.evict(k)
cached = false
}
}
}
if !cached {
if fresh != nil {
instance = fresh
} else {
instance = factory()
}
ms.cache[k] = instance
debug.Log("Mount: NEW component at key %v, type %T", key, instance)
if binder, ok := instance.(AppBinder); ok {
binder.BindApp(app)
}
if init, ok := instance.(Initializer); ok {
cleanup := init.Init()
if cleanup != nil {
ms.cleanups[k] = cleanup
}
}
} else {
if updater, ok := instance.(PropsUpdater); ok {
debug.Log("Mount: CACHED component at key %v, calling UpdateProps, type %T", key, instance)
updater.UpdateProps(fresh)
} else {
debug.Log("Mount: CACHED component at key %v, NO UpdateProps, type %T", key, instance)
}
if binder, ok := instance.(AppBinder); ok {
binder.BindApp(app)
}
}
el := instance.Render(a)
el.component = instance
return el
}
func (a *App) Mount(parent Component, key any, factory func() Component) *Element {
return a.mount(parent, key, factory)
}
func (a *App) MountPersistent(parent Component, key any, factory func() Component) *Element {
k := mountKey{parent: parent, key: key}
a.mounts.persistKeys[k] = true
return a.mount(parent, key, factory)
}
func (ms *mountState) sweep() {
for key := range ms.cache {
if !ms.activeKeys[key] && !ms.persistKeys[key] {
ms.evict(key)
}
}
ms.activeKeys = make(map[mountKey]bool)
}
func (ms *mountState) evict(key mountKey) {
if instance, ok := ms.cache[key]; ok {
if unbinder, ok := instance.(AppUnbinder); ok {
unbinder.UnbindApp()
}
}
if cleanup, ok := ms.cleanups[key]; ok {
cleanup()
delete(ms.cleanups, key)
}
delete(ms.cache, key)
}
|