summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-30 00:36:48 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-30 00:36:48 +0200
commit5725aa77b7aaba46f798d3e18c37d425c93a892f (patch)
treeeee98d19657cfa9fded945a93d943d1e0fb01ca3
parent9c1b13fdfc573650d7e5f96de308483252025a12 (diff)
Solved react
-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()
+ }
}
}