feat: add func in channel.go

This commit is contained in:
dudaodong committed 2022-04-19 16:21:23 +08:00
1 parent fc6dee9e77
commit 046e90486d
2 files changed
+42

No files matched your search

+24
View File
@@ -157,3 +157,27 @@ func (c *Channel) Or(channels ...<-chan any) <-chan any {
return orDone
}
// OrDone
func (c *Channel) OrDone(ctx context.Context, channel <-chan any) <-chan any {
resStream := make(chan any)
go func() {
defer close(resStream)
select {
case <-ctx.Done():
return
case v, ok := <-channel:
if !ok {
return
}
select {
case resStream <- v:
case <-ctx.Done():
}
}
}()
return resStream
}