-
Notifications
You must be signed in to change notification settings - Fork 2
/
pass_through.go
63 lines (53 loc) · 1.29 KB
/
pass_through.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
package gstream
// passThrough retransmits incoming elements as is.
//
// in -- 1 -- 2 ---- 3 -- 4 ------ 5 --
//
// out -- 1 -- 2 ---- 3 -- 4 ------ 5 --
type passThrough struct {
FlowState
in chan interface{}
out chan interface{}
}
// Verify passThrough satisfies the Transfer interface.
var _ Transfer = (*passThrough)(nil)
// newPassThrough returns a new passThrough instance.
func newPassThrough() *passThrough {
passThrough := &passThrough{
in: make(chan interface{}),
out: make(chan interface{}),
}
go passThrough.doStream()
return passThrough
}
// Via streams data through the given flow
func (pt *passThrough) Via(flow Transfer) Transfer {
flow.setState(pt.State())
go pt.transmit(flow)
return flow
}
// To streams data to the given sink
func (pt *passThrough) To(sink Sink) {
sink.setSinkState(pt.State())
go pt.transmit(sink)
}
// Out returns an output channel for sending data
func (pt *passThrough) Out() <-chan interface{} {
return pt.out
}
// In returns an input channel for receiving data
func (pt *passThrough) In() chan<- interface{} {
return pt.in
}
func (pt *passThrough) transmit(inlet Inlet) {
for elem := range pt.Out() {
inlet.In() <- elem
}
close(inlet.In())
}
func (pt *passThrough) doStream() {
for elem := range pt.in {
pt.out <- elem
}
close(pt.out)
}