summaryrefslogtreecommitdiff
path: root/go/react/react.go
blob: 42a0737d6dafe8b4a50780ce2c625a388fe9658c (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package react

const testVersion = 4

/* reactor */

type reactor struct {
	cells []*compuCell
}

func New() Reactor {
	return &reactor{}
}

func (r *reactor) CreateCompute1(c Cell, f func(int) int) ComputeCell {
	old := f(c.Value())
	cc := new(compuCell)
	cc.cb = make(map[CallbackHandle]func(int))
	cc.eval = func() int {
		v := f(c.Value())
		if v != old {
			for _, cb := range cc.cb {
				cb(v)
			}
			old = v
		}
		return v
	}
	r.cells = append(r.cells, cc)
	return cc
}

func (r *reactor) CreateCompute2(c1, c2 Cell, f func(int, int) int) ComputeCell {
	old := f(c1.Value(), c2.Value())
	cc := new(compuCell)
	cc.cb = make(map[CallbackHandle]func(int))
	cc.eval = func() int {
		v := f(c1.Value(), c2.Value())
		if v != old {
			for _, cb := range cc.cb {
				cb(v)
			}
			old = v
		}
		return v
	}
	r.cells = append(r.cells, cc)
	return cc
}

func (r *reactor) CreateInput(i int) InputCell {
	return &inputCell{
		value:   i,
		reactor: r,
	}
}

/* input cell */

type inputCell struct {
	value int
	*reactor
}

func (c *inputCell) SetValue(i int) {
	if c.value != i {
		c.value = i
		for _, cc := range c.cells {
			cc.eval()
		}
	}
}

func (c *inputCell) Value() int {
	return c.value
}

/* compute cell */

type compuCell struct {
	eval func() int
	cb   map[CallbackHandle]func(int)
}

func (c *compuCell) AddCallback(f func(int)) CallbackHandle {
	c.cb[&f] = f
	return &f // guaranteed to be uniq
}

func (c *compuCell) RemoveCallback(h CallbackHandle) {
	delete(c.cb, h)
}

func (c *compuCell) Value() int {
	return c.eval()
}