From 13444d068d2428fd2592420591a7a5f2432350e0 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 28 Aug 2016 19:07:03 +0200 Subject: React WIP --- go/react/react.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 go/react/react.go diff --git a/go/react/react.go b/go/react/react.go new file mode 100644 index 0000000..1f93906 --- /dev/null +++ b/go/react/react.go @@ -0,0 +1,64 @@ +package react + +const testVersion = 4 + +type react struct { +} + +type inputCell struct { + cell +} + +type computeCell struct { + cell + cb map[CallbackHandle]func(int) +} + +func newComputeCell() *computeCell { + return &computeCell{cb: make(map[CallbackHandle]func(int))} +} + +type cell struct { + value int +} + +func New() Reactor { + return &react{} +} + +func (r *react) CreateCompute1(c Cell, f func(int) int) ComputeCell { + return &computeCell{ + cell: cell{value: f(c.Value())}, + cb: make(map[CallbackHandle]func(int)), + } +} + +func (r *react) CreateCompute2(c1, c2 Cell, f func(int, int) int) ComputeCell { + return &computeCell{ + cell: cell{value: f(c1.Value(), c2.Value())}, + cb: make(map[CallbackHandle]func(int)), + } +} + +func (r *react) CreateInput(i int) InputCell { + return &inputCell{ + cell: cell{value: i}, + } +} + +func (c *computeCell) AddCallback(f func(int)) CallbackHandle { + c.cb[&f] = f + return &f +} + +func (c *computeCell) RemoveCallback(h CallbackHandle) { + delete(c.cb, h) +} + +func (c *inputCell) SetValue(i int) { + c.value = i +} + +func (c cell) Value() int { + return c.value +} -- cgit v1.2.3