60 lines
933 B
Go
60 lines
933 B
Go
package channel
|
|
|
|
import "context"
|
|
|
|
func FindFirst[T any](source <-chan T) *T {
|
|
for v := range source {
|
|
return &v
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FindFirstAndCancel[T any](cancel context.CancelFunc, source <-chan T) *T {
|
|
for v := range source {
|
|
cancel()
|
|
return &v
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FindFirstAndFlush[T any](source <-chan T) *T {
|
|
for v := range source {
|
|
go Flush(source)
|
|
return &v
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FindFirstAndCancelFlush[T any](cancel context.CancelFunc, source <-chan T) *T {
|
|
for v := range source {
|
|
cancel()
|
|
go Flush(source)
|
|
return &v
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func HasAny[T any](source <-chan T) bool {
|
|
return FindFirst(source) != nil
|
|
}
|
|
|
|
func HasAnyAndFlush[T any](source <-chan T) bool {
|
|
return FindFirstAndFlush(source) != nil
|
|
}
|