package react import ( "fmt" "strconv" ) const testVersion = 4 // MyReactor implements Reactor type MyReactor struct { lastId int cells []Cell } // New creates a new Reactor func New() *MyReactor { r := &MyReactor{} //r.callbacks = make(map[Cell][]func(int)) 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} 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()) } 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()) } 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) } // 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 } c.val = i // Hit all callbacks for _, v := range c.callbacks { fmt.Println("Hitting a callback: " + strconv.Itoa(i)) v(i) } } func (c *MyCell) AddCallback(cb func(int)) CallbackHandle { if c.lastCallbackId == 0 { fmt.Println("Initializing Callback Map (Cell " + strconv.Itoa(c.id) + ")") c.callbacks = make(map[int]func(int)) } fmt.Println("Adding a Callback to " + strconv.Itoa(c.id)) c.lastCallbackId++ c.callbacks[c.lastCallbackId] = cb fmt.Println("Number of Callbacks: " + strconv.Itoa(c.lastCallbackId)) return c.lastCallbackId } func (c *MyCell) RemoveCallback(cbh CallbackHandle) { delete(c.callbacks, cbh.(int)) }