diff --git a/find.go b/find.go index b5c3f06..9a2fa6f 100644 --- a/find.go +++ b/find.go @@ -1,5 +1,7 @@ package channel +import "context" + func FindFirst[T any](source <-chan T) *T { for v := range source { return &v @@ -7,6 +9,14 @@ func FindFirst[T any](source <-chan T) *T { 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) @@ -15,6 +25,15 @@ func FindFirstAndFlush[T any](source <-chan T) *T { 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