summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-29 23:51:00 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-29 23:51:00 +0200
commit30c5fc3a334748eafec1cadf15307af542a377af (patch)
treeaf50d53be1d6e99bc908458f57985dd4e2cca8ed
parent5b17177545f794c4be16c150caee302e0b3fa40d (diff)
Alt approach
-rw-r--r--go/react/react.go129
1 files changed, 32 insertions, 97 deletions
diff --git a/go/react/react.go b/go/react/react.go
index 5771b41..0f85811 100644
--- a/go/react/react.go
+++ b/go/react/react.go
@@ -1,136 +1,71 @@
package react
-import "log"
-
const testVersion = 4
-type react struct {
-}
-
-type inputCell struct {
- cell
-}
-
-type compuCell struct {
- cell
- cb map[CallbackHandle]func(int)
-}
-
-type com struct {
- eval, ok chan bool
-}
+/* reactor */
-type cell struct {
- value int
- observer []com
+type react struct {
}
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 {
- com := newCom()
cc := &compuCell{
- cb: make(map[CallbackHandle]func(int)),
+ eval: func() int { return f(c.Value()) },
+ cb: make(map[CallbackHandle]func(int)),
}
- cc.value = f(c.Value())
- Register(c, com)
- go func() {
- for range com.eval {
- log.Println("got", c.Value())
- old := cc.value
- cc.value = f(c.Value())
- if old != cc.value {
- for _, ch := range cc.observer {
- log.Println("notify #1")
- ch.eval <- true
- <-ch.ok
- }
- for _, cb := range cc.cb {
- log.Println("run cb #1")
- cb(cc.value)
- }
- }
- com.ok <- true
- }
- }()
return cc
}
func (r *react) CreateCompute2(c1, c2 Cell, f func(int, int) int) ComputeCell {
- com := newCom()
cc := &compuCell{
- cb: make(map[CallbackHandle]func(int)),
+ eval: func() int { return f(c1.Value(), c2.Value()) },
+ cb: make(map[CallbackHandle]func(int)),
}
- cc.value = f(c1.Value(), c2.Value())
- Register(c1, com)
- Register(c2, com)
- go func() {
- for range com.eval {
- old := cc.value
- cc.value = f(c1.Value(), c2.Value())
- if old != cc.value {
- for _, ch := range cc.observer {
- log.Println("notify #2")
- ch.eval <- true
- <-ch.ok
- }
- for _, cb := range cc.cb {
- log.Println("run cb #2")
- cb(cc.value)
- }
- }
- com.ok <- true
- }
- }()
return cc
}
func (r *react) CreateInput(i int) InputCell {
return &inputCell{
- cell: cell{value: i},
+ value: i,
}
}
-func (c *compuCell) AddCallback(f func(int)) CallbackHandle {
- c.cb[&f] = f
- return &f
-}
+/* input cell */
-func (c *compuCell) RemoveCallback(h CallbackHandle) {
- delete(c.cb, h)
+type inputCell struct {
+ value int
}
func (c *inputCell) SetValue(i int) {
- old := c.value
- c.value = i
- if old != c.value {
- for _, ch := range c.observer {
- log.Println("notify", i)
- ch.eval <- true
- <-ch.ok
- }
+ // TODO call back
+ if c.value != i {
+ c.value = i
}
}
-func (c *cell) Value() int {
- log.Println("report", c.value)
+func (c *inputCell) Value() int {
return c.value
}
-func Register(c Cell, ch com) {
- switch v := c.(type) {
- case *compuCell:
- v.observer = append(v.observer, ch)
- case *inputCell:
- v.observer = append(v.observer, ch)
- }
+/* compute cell */
+
+type compuCell struct {
+ eval func() int
+ cb map[CallbackHandle]func(int)
+}
+
+func (c *compuCell) AddCallback(f func(int)) CallbackHandle {
+ c.cb[&f] = f
+ return &f
+}
+
+func (c *compuCell) RemoveCallback(h CallbackHandle) {
+ delete(c.cb, h)
+}
+
+func (c *compuCell) Value() int {
+ return c.eval()
}