@@ -9,39 +9,43 @@ import (
9
9
"github.com/vladimirvivien/automi/log"
10
10
)
11
11
12
- // ChanSink sends streamed items to an output channel.
13
- type ChanSink [T any ] struct {
14
- output chan T
12
+ // ChanSink represents a sink backed by a Go channel.
13
+ type ChanSink [IN any , CHAN <- chan IN ] struct {
14
+ chansink chan IN
15
15
input <- chan any
16
16
logf api.StreamLogFunc
17
17
}
18
18
19
- // Chan is the constructor function which returns a new ChanSink .
20
- func Chan [ T any ]( outputChan chan T ) * ChanSink [T ] {
21
- return & ChanSink [T ]{
22
- output : outputChan ,
19
+ // BufferedChan returns a new ChanSink backed by a buffered channel .
20
+ func BufferedChan [ IN any , CHAN <- chan IN ]( bufferSize int ) * ChanSink [IN , CHAN ] {
21
+ return & ChanSink [IN , CHAN ]{
22
+ chansink : make ( chan IN , bufferSize ) ,
23
23
logf : log .NoLogFunc ,
24
24
}
25
25
}
26
26
27
- // SetInput sets the source for the sink.
28
- func (s * ChanSink [T ]) SetInput (in <- chan any ) {
27
+ // Chan returns a new ChanSink backed by an unbuffered channel.
28
+ func Chan [IN any , CHAN <- chan IN ]() * ChanSink [IN ,CHAN ] {
29
+ return BufferedChan [IN ,CHAN ](0 )
30
+ }
31
+
32
+ // SetInput sets the input for the sink.
33
+ func (s * ChanSink [IN ,CHAN ]) SetInput (in <- chan any ) {
29
34
s .input = in
30
35
}
31
36
32
- // Get returns the output channel used by the sink.
33
- func (s * ChanSink [T ]) Get () <- chan T {
34
- return s .output
37
+ // Get returns a receive-only channel used by the sink.
38
+ func (s * ChanSink [IN , CHAN ]) Get () CHAN {
39
+ return s .chansink
35
40
}
36
41
37
42
// SetLogFunc sets a logging func for the component.
38
- func (s * ChanSink [T ]) SetLogFunc (f api.StreamLogFunc ) {
43
+ func (s * ChanSink [IN , CHAN ]) SetLogFunc (f api.StreamLogFunc ) {
39
44
s .logf = f
40
45
}
41
46
42
47
// Open starts the sink and returns and waits on the returned
43
- // channel for the sink to be done or an error to be received.
44
- func (s * ChanSink [T ]) Open (ctx context.Context ) <- chan error {
48
+ func (s * ChanSink [IN ,CHAN ]) Open (ctx context.Context ) <- chan error {
45
49
result := make (chan error )
46
50
47
51
s .logf (ctx , log .LogInfo (
@@ -56,7 +60,7 @@ func (s *ChanSink[T]) Open(ctx context.Context) <-chan error {
56
60
"Component closing" ,
57
61
slog .String ("sink" , "Chan" ),
58
62
))
59
- close (s .output ) // Ensure output channel is closed
63
+ close (s .chansink ) // Ensure output channel is closed
60
64
}()
61
65
62
66
for {
@@ -65,7 +69,7 @@ func (s *ChanSink[T]) Open(ctx context.Context) <-chan error {
65
69
if ! opened {
66
70
return
67
71
}
68
- data , ok := item .(T )
72
+ data , ok := item .(IN )
69
73
if ! ok {
70
74
s .logf (ctx , log .LogDebug (
71
75
"Error: unexpected data type" ,
@@ -74,7 +78,7 @@ func (s *ChanSink[T]) Open(ctx context.Context) <-chan error {
74
78
))
75
79
continue
76
80
}
77
- s .output <- data
81
+ s .chansink <- data
78
82
case <- ctx .Done ():
79
83
return
80
84
}
0 commit comments