From 156024c771e0f75c6b79aacfb36ce8cc79855c7e Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 29 Aug 2016 15:37:35 +0200 Subject: Tweak --- go/react/react.go | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'go/react/react.go') 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) } } -- cgit v1.2.3