-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
124 lines (105 loc) · 2.63 KB
/
controller.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package pipe
type sink[T any] struct{ ch chan T }
// A Controller bundles a sink channel and a broadcaster.
type Controller[T any] struct {
sink[T]
broadcaster[T]
}
func NewController[T any]() *Controller[T] {
c := new(Controller[T])
c.sink.ch = make(chan T)
c.broadcaster.init(c.sink.ch, nil)
return c
}
// Sink returns the sink channel of the controller.
func (c *Controller[T]) Sink() chan<- T {
c.ensureInit()
return c.ch
}
// Send sends value to the sink channel.
func (c *Controller[T]) Send(value T) (ok bool) {
if c.initialized() {
c.ch <- value
return true
}
return false
}
// A Controller with a comparable element type.
type ControllerC[T comparable] struct {
sink[T]
broadcasterc[T]
}
func NewControllerC[T comparable]() *ControllerC[T] {
c := new(ControllerC[T])
c.sink.ch = make(chan T)
c.broadcaster.init(c.sink.ch, nil)
return c
}
// Sink returns the sink channel of the controller.
func (c *ControllerC[T]) Sink() chan<- T {
c.ensureInit()
return c.ch
}
// Send sends value to the sink channel.
func (c *ControllerC[T]) Send(value T) (ok bool) {
if c.initialized() {
c.ch <- value
return true
}
return false
}
// A Controller with a memorizable broadcaster.
type ControllerM[T any] struct {
sink[T]
broadcaster[T]
}
func NewControllerM[T any](initial T) *ControllerM[T] {
c := new(ControllerM[T])
c.sink.ch = make(chan T)
c.broadcaster.init(c.sink.ch, &initial)
return c
}
// Sink returns the sink channel of the controller.
func (c *ControllerM[T]) Sink() chan<- T {
c.ensureInit()
return c.ch
}
// Send sends value to the sink channel.
func (c *ControllerM[T]) Send(value T) (ok bool) {
if c.initialized() {
c.ch <- value
return true
}
c.replaceBuf(value)
return false
}
// Current returns the latest value that the broadcaster memorizes.
func (c *ControllerM[T]) Current() T { return c.broadcaster.current() }
type ControllerCM[T comparable] struct {
sink[T]
broadcasterc[T]
}
// A Controller with a comparable element type and memorizable broadcaster.
func NewControllerCM[T comparable](initial T, dedup bool) *ControllerCM[T] {
c := new(ControllerCM[T])
c.sink.ch = make(chan T)
c.broadcaster.init(c.sink.ch, &initial)
c.dedup = dedup
return c
}
// Sink returns the sink channel of the controller.
func (c *ControllerCM[T]) Sink() chan<- T {
c.ensureInit()
return c.ch
}
// Send sends value to the sink channel.
func (c *ControllerCM[T]) Send(value T) (ok bool) {
if c.initialized() {
c.ch <- value
return true
}
c.replaceBuf(value)
return false
}
// Current returns the latest value that the broadcaster memorizes.
func (c *ControllerCM[T]) Current() T { return c.broadcaster.current() }