2016-08-13 23:20:14 +00:00
|
|
|
package react
|
|
|
|
|
2016-08-15 12:32:59 +00:00
|
|
|
import "strconv"
|
|
|
|
|
2016-08-13 23:20:14 +00:00
|
|
|
const testVersion = 4
|
|
|
|
|
|
|
|
// MyReactor implements Reactor
|
|
|
|
type MyReactor struct {
|
|
|
|
lastId int
|
|
|
|
cells []Cell
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Reactor
|
|
|
|
func New() *MyReactor {
|
|
|
|
r := &MyReactor{}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateInput builds an input cell and adds it to the reactor
|
|
|
|
func (r *MyReactor) CreateInput(i int) InputCell {
|
|
|
|
r.lastId++
|
|
|
|
ic := MyCell{val: i, id: r.lastId}
|
2016-08-15 12:32:59 +00:00
|
|
|
ic.previousVal = ic.Value()
|
2016-08-13 23:20:14 +00:00
|
|
|
return &ic
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCompute1 Takes a cell and a function and returns a compute cell
|
|
|
|
// which has a value based on running the function on the cells value.
|
|
|
|
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()) }
|
2016-08-15 12:32:59 +00:00
|
|
|
cc.previousVal = cc.Value()
|
2016-08-15 12:26:26 +00:00
|
|
|
c.(*MyCell).addDependent(cc)
|
2016-08-13 23:20:14 +00:00
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCompute2 Takes two cells and a function and returns a compute cell
|
|
|
|
// which has a value based on running the function on the cells values.
|
|
|
|
func (r *MyReactor) CreateCompute2(c1, c2 Cell, f func(int, int) int) ComputeCell {
|
|
|
|
r.lastId++
|
|
|
|
cc := &MyCell{id: r.lastId, isComputed: true}
|
|
|
|
cc.compVal = func() int { return f(c1.Value(), c2.Value()) }
|
2016-08-15 12:32:59 +00:00
|
|
|
cc.previousVal = cc.Value()
|
2016-08-15 12:26:26 +00:00
|
|
|
c1.(*MyCell).addDependent(cc)
|
|
|
|
c2.(*MyCell).addDependent(cc)
|
2016-08-13 23:20:14 +00:00
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// MyCell implements the all Cell interfaces
|
|
|
|
type MyCell struct {
|
|
|
|
id int
|
|
|
|
isComputed bool
|
|
|
|
val int
|
|
|
|
compVal func() int
|
|
|
|
lastCallbackId int
|
|
|
|
callbacks map[int]func(int)
|
2016-08-15 12:26:26 +00:00
|
|
|
dependents []Cell
|
|
|
|
previousVal int
|
2016-08-13 23:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value of the cell
|
|
|
|
func (c MyCell) Value() int {
|
|
|
|
if c.isComputed {
|
|
|
|
return c.compVal()
|
|
|
|
}
|
|
|
|
return c.val
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue sets the value on the cell
|
|
|
|
func (c *MyCell) SetValue(i int) {
|
|
|
|
if i == c.val || c.isComputed {
|
|
|
|
// No change or this is a computed cell, just return
|
|
|
|
return
|
|
|
|
}
|
2016-08-15 12:26:26 +00:00
|
|
|
c.previousVal = c.val
|
2016-08-13 23:20:14 +00:00
|
|
|
c.val = i
|
2016-08-15 12:26:26 +00:00
|
|
|
c.updated()
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:32:59 +00:00
|
|
|
func i(v int) string {
|
|
|
|
return strconv.Itoa(v)
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:26:26 +00:00
|
|
|
func (c *MyCell) updated() {
|
|
|
|
if c.Value() == c.previousVal {
|
2016-08-15 12:32:59 +00:00
|
|
|
// Value didn't change, done.
|
2016-08-15 12:26:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.previousVal = c.Value()
|
|
|
|
// Triggers callbacks
|
2016-08-13 23:20:14 +00:00
|
|
|
for _, v := range c.callbacks {
|
2016-08-15 12:26:26 +00:00
|
|
|
v(c.Value())
|
|
|
|
}
|
|
|
|
// Hit 'updated' on all dependents
|
|
|
|
for _, v := range c.dependents {
|
|
|
|
v.(*MyCell).updated()
|
2016-08-13 23:20:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:26:26 +00:00
|
|
|
func (c *MyCell) addDependent(nc Cell) {
|
|
|
|
c.dependents = append(c.dependents, nc)
|
|
|
|
}
|
|
|
|
|
2016-08-13 23:20:14 +00:00
|
|
|
func (c *MyCell) AddCallback(cb func(int)) CallbackHandle {
|
|
|
|
if c.lastCallbackId == 0 {
|
|
|
|
c.callbacks = make(map[int]func(int))
|
|
|
|
}
|
|
|
|
c.lastCallbackId++
|
|
|
|
c.callbacks[c.lastCallbackId] = cb
|
|
|
|
return c.lastCallbackId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MyCell) RemoveCallback(cbh CallbackHandle) {
|
|
|
|
delete(c.callbacks, cbh.(int))
|
|
|
|
}
|