-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapped.go
63 lines (55 loc) · 1.55 KB
/
wrapped.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 priority_channels
import (
"context"
"github.com/dimag-jfrog/priority-channels/internal/selectable"
)
func WrapAsPriorityChannel[T any](ctx context.Context, channelName string, msgsC <-chan T, options ...func(*PriorityChannelOptions)) (*PriorityChannel[T], error) {
if channelName == "" {
return nil, ErrEmptyChannelName
}
pcOptions := &PriorityChannelOptions{}
for _, option := range options {
option(pcOptions)
}
compositeChannel := &wrappedChannel[T]{
ctx: ctx,
channelName: channelName,
msgsC: msgsC,
}
return &PriorityChannel[T]{
ctx: ctx,
compositeChannel: compositeChannel,
channelReceiveWaitInterval: pcOptions.channelReceiveWaitInterval,
}, nil
}
type wrappedChannel[T any] struct {
ctx context.Context
channelName string
msgsC <-chan T
}
func (w *wrappedChannel[T]) ChannelName() string {
return w.channelName
}
func (w *wrappedChannel[T]) NextSelectCases(upto int) (selectCases []selectable.SelectCase[T], isLastIteration bool, closedChannel *selectable.ClosedChannelDetails) {
select {
case <-w.ctx.Done():
return nil, true, &selectable.ClosedChannelDetails{
ChannelName: w.channelName,
PathInTree: nil,
}
default:
return []selectable.SelectCase[T]{
{
ChannelName: w.channelName,
MsgsC: w.msgsC,
},
}, true, nil
}
}
func (c *wrappedChannel[T]) UpdateOnCaseSelected(pathInTree []selectable.ChannelNode) {}
func (c *wrappedChannel[T]) Validate() error {
if c.channelName == "" {
return ErrEmptyChannelName
}
return nil
}