summaryrefslogtreecommitdiff
path: root/go/react/react.go
blob: 1f93906d2fcf76384fed791b61ffea6bbc54b597 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
}