summaryrefslogtreecommitdiff
path: root/go/react/react.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/react/react.go')
-rw-r--r--go/react/react.go45
1 files changed, 36 insertions, 9 deletions
diff --git a/go/react/react.go b/go/react/react.go
index 99b40d7..da14099 100644
--- a/go/react/react.go
+++ b/go/react/react.go
@@ -5,31 +5,55 @@ const testVersion = 4
/* reactor */
type reactor struct {
- cells []compuCell // XXX
+ cells map[*compuCell]struct{}
}
func New() Reactor {
- return &reactor{}
+ return &reactor{
+ cells: make(map[*compuCell]struct{}),
+ }
}
func (r *reactor) CreateCompute1(c Cell, f func(int) int) ComputeCell {
- return &compuCell{
- eval: func() int { return f(c.Value()) },
- cb: make(map[CallbackHandle]func(int)),
+ old := f(c.Value())
+ cc := new(compuCell)
+ cc.cb = make(map[CallbackHandle]func(int))
+ cc.eval = func() int {
+ v := f(c.Value())
+ if v != old {
+ for _, cb := range cc.cb {
+ cb(v)
+ }
+ old = v
+ }
+ return v
}
+ r.cells[cc] = struct{}{}
+ return cc
}
func (r *reactor) CreateCompute2(c1, c2 Cell, f func(int, int) int) ComputeCell {
- return &compuCell{
- eval: func() int { return f(c1.Value(), c2.Value()) },
- cb: make(map[CallbackHandle]func(int)),
+ old := f(c1.Value(), c2.Value())
+ cc := new(compuCell)
+ cc.cb = make(map[CallbackHandle]func(int))
+ cc.eval = func() int {
+ v := f(c1.Value(), c2.Value())
+ if v != old {
+ for _, cb := range cc.cb {
+ cb(v)
+ }
+ old = v
+ }
+ return v
}
+ r.cells[cc] = struct{}{}
+ return cc
}
func (r *reactor) CreateInput(i int) InputCell {
return &inputCell{
value: i,
- reactor: r, // XXX
+ reactor: r,
}
}
@@ -44,6 +68,9 @@ func (c *inputCell) SetValue(i int) {
// TODO trigger call back
if c.value != i {
c.value = i
+ for cc := range c.cells {
+ cc.eval()
+ }
}
}