This repository was archived by the owner on Apr 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact.go
More file actions
173 lines (145 loc) · 2.68 KB
/
Copy pathreact.go
File metadata and controls
173 lines (145 loc) · 2.68 KB
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 react
// https://github.com/alirezaudev
// https://linkedin.com/in/alireza-msv
type cellType int
const (
Input cellType = iota
Compute
Normal
)
type cell struct {
kind cellType
value int
compute1 func(int) int
compute2 func(int, int) int
dep1 Cell
dep2 Cell
dependents []*cell
callbacks map[int]func(int)
nextCallbackID int
r *reactor
}
type canceler struct {
c *cell
id int
}
type reactor struct {
cells []*cell
}
func (c *canceler) Cancel() {
if c.c == nil {
return
}
delete(c.c.callbacks, c.id)
c.c = nil
}
func (c *cell) Value() int {
return c.value
}
func (c *cell) SetValue(v int) {
if c.kind != Input {
return
}
if c.value == v {
return
}
c.value = v
c.r.propagateFrom(c)
}
func (c *cell) AddCallback(cb func(int)) Canceler {
if c.callbacks == nil {
c.callbacks = make(map[int]func(int))
}
id := c.nextCallbackID
c.nextCallbackID++
c.callbacks[id] = cb
return &canceler{
c: c,
id: id,
}
}
func New() Reactor {
return &reactor{}
}
func (r *reactor) CreateInput(initial int) InputCell {
c := &cell{
kind: Input,
value: initial,
r: r,
}
r.cells = append(r.cells, c)
return c
}
func (r *reactor) CreateCompute1(dep Cell, compute func(int) int) ComputeCell {
c := &cell{
kind: Compute,
compute1: compute,
dep1: dep,
r: r,
}
if d, ok := dep.(*cell); ok {
d.dependents = append(d.dependents, c)
}
c.recompute()
r.cells = append(r.cells, c)
return c
}
func (r *reactor) CreateCompute2(dep1, dep2 Cell, compute func(int, int) int) ComputeCell {
c := &cell{
kind: Compute,
compute2: compute,
dep1: dep1,
dep2: dep2,
r: r,
}
if d1, ok := dep1.(*cell); ok {
d1.dependents = append(d1.dependents, c)
}
if d2, ok := dep2.(*cell); ok {
d2.dependents = append(d2.dependents, c)
}
c.recompute()
r.cells = append(r.cells, c)
return c
}
func (c *cell) recompute() bool {
if c.kind != Compute {
return false
}
old := c.value
var newVal int
if c.compute1 != nil {
newVal = c.compute1(c.dep1.Value())
} else {
newVal = c.compute2(c.dep1.Value(), c.dep2.Value())
}
if newVal == old {
return false
}
c.value = newVal
return true
}
func (r *reactor) propagateFrom(start *cell) {
queue := []*cell{start}
visited := make(map[*cell]bool)
for len(queue) > 0 {
cur := queue[0]
queue = queue[1:]
for _, dep := range cur.dependents {
if visited[dep] {
continue
}
visited[dep] = true
if dep.kind != Compute {
continue
}
changed := dep.recompute()
if changed {
queue = append(queue, dep)
for _, cb := range dep.callbacks {
cb(dep.value)
}
}
}
}
}