-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
173 lines (156 loc) · 3.74 KB
/
hooks.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
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
165
166
167
168
169
170
171
172
173
package goui
type EffectTeardown func()
type Deps []any
type Callback[Func any] struct {
invoke Func
}
type effectRecord struct {
deps Deps
teardown EffectTeardown
}
func useHooks() (int, *Node) {
node := currentNode
cursor := node.hooksCursor
node.hooksCursor++
return cursor, node
}
func UseState[T comparable](initialValue T) (T, func(func(T) T)) {
cursor, node := useHooks()
if len(node.hooks) <= cursor {
node.hooks = append(node.hooks, initialValue)
}
setState := func(update func(T) T) {
if node.unmounted {
panic("bad set state")
}
oldVal := node.hooks[cursor].(T)
newVal := update(oldVal)
if newVal != oldVal {
node.hooks[cursor] = newVal
node.queue = append(node.queue, callComponentFunc(node))
go func() {
if len(node.queue) > 0 {
tip := node.queue[len(node.queue)-1]
node.queue = node.queue[:0]
reconcile(node.virtNode, tip)
node.virtNode = tip
}
}()
}
}
return node.hooks[cursor].(T), setState
}
func UseEffect(effect func() EffectTeardown, deps Deps) {
cursor, node := useHooks()
if len(node.hooks) <= cursor {
record := &effectRecord{deps: deps}
node.hooks = append(node.hooks, record)
go func() {
if !node.unmounted {
record.teardown = effect()
}
}()
return
}
record := node.hooks[cursor].(*effectRecord)
if !areDepsEqual(deps, record.deps) {
record.deps = deps
go func() {
if record.teardown != nil {
record.teardown()
}
if !node.unmounted {
record.teardown = effect()
}
}()
}
}
func UseImmediateEffect(effect func() EffectTeardown, deps Deps) {
cursor, node := useHooks()
if len(node.hooks) <= cursor {
node.hooks = append(node.hooks, &effectRecord{
deps: deps,
teardown: effect(),
})
return
}
record := node.hooks[cursor].(*effectRecord)
if !areDepsEqual(deps, record.deps) {
if record.teardown != nil {
record.teardown()
}
record.deps = deps
record.teardown = effect()
}
}
type memoRecord[T any] struct {
deps Deps
val T
}
func UseMemo[T any](create func() T, deps Deps) T {
cursor, node := useHooks()
if len(node.hooks) <= cursor {
m := &memoRecord[T]{
val: create(),
deps: deps,
}
node.hooks = append(node.hooks, m)
return m.val
}
memo := node.hooks[cursor].(*memoRecord[T])
if !areDepsEqual(deps, memo.deps) {
memo.deps = deps
memo.val = create()
}
return memo.val
}
func UseCallback[Func any](handlerFunc Func, deps Deps) *Callback[Func] {
return UseMemo(func() *Callback[Func] {
return &Callback[Func]{invoke: handlerFunc}
}, deps)
}
type Ref[T any] struct {
Value T
}
func UseRef[T any](initialValue T) *Ref[T] {
return UseMemo[*Ref[T]](func() *Ref[T] { return &Ref[T]{Value: initialValue} }, Deps{})
}
func useAtomSubscription[T comparable](atom *Atom[T]) {
node := currentNode
UseImmediateEffect(func() EffectTeardown {
atom.subscribe(node)
return func() {
atom.unsubscribe(node)
}
}, Deps{node})
}
func UseAtom[T comparable](atom *Atom[T]) (T, func(func(T) T)) {
useAtomSubscription(atom)
return atom.value, atom.update
}
func UseAtomValue[T comparable](atom *Atom[T]) T {
useAtomSubscription(atom)
return atom.value
}
func UseAtomSetter[T comparable](atom *Atom[T]) func(func(T) T) {
return atom.update
}
func UseAtomSelector[T comparable, R any](atom *Atom[T], selector func(T) R) R {
node := currentNode
selected := selector(atom.value)
UseImmediateEffect(func() EffectTeardown {
selects, ok := atom.selectors.Get(node)
record := &selectorRecord[T]{
selected: selected,
selector: func(t T) any { return selector(t) },
}
if ok {
selects = append(selects, record)
atom.selectors.Set(node, selects)
} else {
atom.selectors.Set(node, []*selectorRecord[T]{record})
}
return func() { atom.selectors.Delete(node) }
}, nil)
return selected
}