-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathpass_through.go
66 lines (55 loc) · 1.4 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
64
65
66
package flow
import (
"github.com/reugn/go-streams"
)
// PassThrough retransmits incoming elements downstream as they are.
//
// in -- 1 -- 2 ---- 3 -- 4 ------ 5 --
//
// out -- 1 -- 2 ---- 3 -- 4 ------ 5 --
type PassThrough struct {
in chan any
out chan any
}
// Verify PassThrough satisfies the Flow interface.
var _ streams.Flow = (*PassThrough)(nil)
// NewPassThrough returns a new PassThrough operator.
func NewPassThrough() *PassThrough {
passThrough := &PassThrough{
in: make(chan any),
out: make(chan any),
}
go passThrough.doStream()
return passThrough
}
// Via asynchronously streams data to the given Flow and returns it.
func (pt *PassThrough) Via(flow streams.Flow) streams.Flow {
go pt.transmit(flow)
return flow
}
// To streams data to the given Sink and blocks until the Sink has completed
// processing all data.
func (pt *PassThrough) To(sink streams.Sink) {
pt.transmit(sink)
sink.AwaitCompletion()
}
// Out returns the output channel of the PassThrough operator.
func (pt *PassThrough) Out() <-chan any {
return pt.out
}
// In returns the input channel of the PassThrough operator.
func (pt *PassThrough) In() chan<- any {
return pt.in
}
func (pt *PassThrough) transmit(inlet streams.Inlet) {
for element := range pt.Out() {
inlet.In() <- element
}
close(inlet.In())
}
func (pt *PassThrough) doStream() {
for element := range pt.in {
pt.out <- element
}
close(pt.out)
}