summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-29 15:37:35 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-29 15:37:35 +0200
commit156024c771e0f75c6b79aacfb36ce8cc79855c7e (patch)
tree33291f57a04b3d0668293e10a79923b1ef8cebe9
parentcfbbac64e432e37e1a17ab1b01d5a2da08ca38d7 (diff)
Tweak
-rw-r--r--go/react/react.go37
1 files changed, 21 insertions, 16 deletions
diff --git a/go/react/react.go b/go/react/react.go
index 069d7be..ebe8e61 100644
--- a/go/react/react.go
+++ b/go/react/react.go
@@ -29,16 +29,22 @@ func New() Reactor {
return &react{}
}
+func newCom() com {
+ return com{
+ eval: make(chan bool),
+ ok: make(chan bool),
+ }
+}
+
func (r *react) CreateCompute1(c Cell, f func(int) int) ComputeCell {
- ch := make(chan bool)
- ok := make(chan bool)
+ com := newCom()
cc := &compuCell{
cb: make(map[CallbackHandle]func(int)),
}
cc.value = f(c.Value())
- Register(c, ch, ok)
+ Register(c, com)
go func() {
- for range ch {
+ for range com.eval {
log.Println("got", c.Value())
old := cc.value
cc.value = f(c.Value())
@@ -49,27 +55,26 @@ func (r *react) CreateCompute1(c Cell, f func(int) int) ComputeCell {
<-ch.ok
}
for _, cb := range cc.cb {
- log.Println("cb #1")
+ log.Println("cb #1", cb)
cb(cc.value)
}
}
- ok <- true
+ com.ok <- true
}
}()
return cc
}
func (r *react) CreateCompute2(c1, c2 Cell, f func(int, int) int) ComputeCell {
- ch := make(chan bool)
- ok := make(chan bool)
+ com := newCom()
cc := &compuCell{
cb: make(map[CallbackHandle]func(int)),
}
cc.value = f(c1.Value(), c2.Value())
- Register(c1, ch, ok)
- Register(c2, ch, ok)
+ Register(c1, com)
+ Register(c2, com)
go func() {
- for range ch {
+ for range com.eval {
old := cc.value
cc.value = f(c1.Value(), c2.Value())
if old != cc.value {
@@ -79,11 +84,11 @@ func (r *react) CreateCompute2(c1, c2 Cell, f func(int, int) int) ComputeCell {
<-ch.ok
}
for _, cb := range cc.cb {
- log.Println("cb #2")
+ log.Println("cb #2", cb, cc.value)
cb(cc.value)
}
}
- ok <- true
+ com.ok <- true
}
}()
return cc
@@ -122,11 +127,11 @@ func (c *cell) Value() int {
return v
}
-func Register(c Cell, ch, ok chan bool) {
+func Register(c Cell, ch com) {
switch v := c.(type) {
case *compuCell:
- v.observer = append(v.observer, com{ch, ok})
+ v.observer = append(v.observer, ch)
case *inputCell:
- v.observer = append(v.observer, com{ch, ok})
+ v.observer = append(v.observer, ch)
}
}