Completed go/react

This commit is contained in:
Brian Buller 2016-08-15 07:32:59 -05:00
parent 2671fb5ad1
commit 0ef5df860d

View File

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