channel/find.go

60 lines
933 B
Go
Raw Normal View History

2022-12-19 18:01:38 +01:00
package channel
2023-01-16 15:13:07 +01:00
import "context"
2022-12-19 18:01:38 +01:00
func FindFirst[T any](source <-chan T) *T {
for v := range source {
return &v
}
return nil
}
2023-01-16 15:13:07 +01:00
func FindFirstAndCancel[T any](cancel context.CancelFunc, source <-chan T) *T {
for v := range source {
cancel()
return &v
}
return nil
}
2022-12-19 18:01:38 +01:00
func FindFirstAndFlush[T any](source <-chan T) *T {
for v := range source {
go Flush(source)
2022-12-19 18:01:38 +01:00
return &v
}
return nil
}
2023-01-16 15:13:07 +01:00
func FindFirstAndCancelFlush[T any](cancel context.CancelFunc, source <-chan T) *T {
for v := range source {
cancel()
go Flush(source)
return &v
}
return nil
}
2022-12-19 18:01:38 +01:00
func FindLast[T any](source <-chan T) *T {
var last *T = new(T)
found := false
for v := range source {
*last = v
found = true
}
if !found {
return nil
}
return last
}
2022-12-19 18:07:43 +01:00
func HasAny[T any](source <-chan T) bool {
return FindFirst(source) != nil
}
func HasAnyAndFlush[T any](source <-chan T) bool {
return FindFirstAndFlush(source) != nil
}