-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathsink_stdout.go
46 lines (36 loc) · 927 Bytes
/
sink_stdout.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
package extension
import (
"fmt"
"github.com/reugn/go-streams"
)
// StdoutSink represents a simple outbound connector that writes
// streaming data to standard output.
type StdoutSink struct {
in chan any
done chan struct{}
}
var _ streams.Sink = (*StdoutSink)(nil)
// NewStdoutSink returns a new StdoutSink connector.
func NewStdoutSink() *StdoutSink {
stdoutSink := &StdoutSink{
in: make(chan any),
done: make(chan struct{}),
}
// asynchronously process stream data
go stdoutSink.process()
return stdoutSink
}
func (stdout *StdoutSink) process() {
defer close(stdout.done)
for elem := range stdout.in {
fmt.Println(elem)
}
}
// In returns the input channel of the StdoutSink connector.
func (stdout *StdoutSink) In() chan<- any {
return stdout.in
}
// AwaitCompletion blocks until the StdoutSink has processed all received data.
func (stdout *StdoutSink) AwaitCompletion() {
<-stdout.done
}