-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchannel.go
52 lines (41 loc) · 1.07 KB
/
channel.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
package jpipe
import "sync"
// A Channel is a wrapper for a Go channel.
// It provides chainable methods to construct pipelines, but conceptually it must be seen as a nothing but an enhanced Go channel.
type Channel[T any] struct {
pipeline *Pipeline
channel <-chan T
unsubscriber func()
toNode pipelineNode
mutex sync.Mutex
}
func newChannel[T any](pipeline *Pipeline, channel chan T, unsubscriber func()) *Channel[T] {
return &Channel[T]{
pipeline: pipeline,
channel: channel,
unsubscriber: unsubscriber,
}
}
func (c *Channel[T]) unsubscribe() {
c.unsubscriber()
}
func (c *Channel[T]) setToNode(toNode pipelineNode) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.toNode != nil {
panic("Can't subscribe more than one operator to a Channel")
}
c.toNode = toNode
}
func (c *Channel[T]) getToNode() pipelineNode {
return c.toNode
}
func (c *Channel[T]) getPipeline() *Pipeline {
return c.pipeline
}
func (c *Channel[T]) getChannel() <-chan T {
return c.channel
}
func (c *Channel[T]) Pipeline() *Pipeline {
return c.pipeline
}