Completed go/react

This commit is contained in:
Brian Buller 2016-08-15 07:32:59 -05:00
parent 2671fb5ad1
commit 0ef5df860d
1 changed files with 10 additions and 0 deletions

View File

@ -1,5 +1,7 @@
package react
import "strconv"
const testVersion = 4
// MyReactor implements Reactor
@ -18,6 +20,7 @@ func New() *MyReactor {
func (r *MyReactor) CreateInput(i int) InputCell {
r.lastId++
ic := MyCell{val: i, id: r.lastId}
ic.previousVal = ic.Value()
return &ic
}
@ -27,6 +30,7 @@ func (r *MyReactor) CreateCompute1(c Cell, f func(int) int) ComputeCell {
r.lastId++
cc := &MyCell{id: r.lastId, isComputed: true}
cc.compVal = func() int { return f(c.Value()) }
cc.previousVal = cc.Value()
c.(*MyCell).addDependent(cc)
return cc
}
@ -37,6 +41,7 @@ func (r *MyReactor) CreateCompute2(c1, c2 Cell, f func(int, int) int) ComputeCel
r.lastId++
cc := &MyCell{id: r.lastId, isComputed: true}
cc.compVal = func() int { return f(c1.Value(), c2.Value()) }
cc.previousVal = cc.Value()
c1.(*MyCell).addDependent(cc)
c2.(*MyCell).addDependent(cc)
return cc
@ -73,8 +78,13 @@ func (c *MyCell) SetValue(i int) {
c.updated()
}
func i(v int) string {
return strconv.Itoa(v)
}
func (c *MyCell) updated() {
if c.Value() == c.previousVal {
// Value didn't change, done.
return
}
c.previousVal = c.Value()