Skip to content

Commit

Permalink
Add support for CancellationToken to TaskSeq
Browse files Browse the repository at this point in the history
  • Loading branch information
abelbraaksma committed Oct 29, 2023
1 parent e2d79cf commit e1f5161
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,12 @@ module HighPriority =
sm.Data.current <- ValueNone
false)

// Binding to a cancellation token. This allows `do! someCancellationToken`
member inline _.Bind(myToken: CancellationToken, continuation: (unit -> ResumableTSC<'T>)) : ResumableTSC<'T> =
ResumableTSC<'T>(fun sm ->
sm.Data.cancellationToken <- myToken
(continuation ()).Invoke(&sm))

[<AutoOpen>]
module TaskSeqBuilder =
/// Builds an asynchronous task sequence based on IAsyncEnumerable<'T> using computation expression syntax.
Expand Down
5 changes: 5 additions & 0 deletions src/FSharp.Control.TaskSeq/TaskSeqInternal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ module internal TaskSeqInternal =
KeyNotFoundException("The predicate function or index did not satisfy any item in the async sequence.")
|> raise

let inline withCancellationToken (cancellationToken: CancellationToken) (source: taskSeq<'T>) = taskSeq {
do! cancellationToken
yield! source
}

let isEmpty (source: taskSeq<_>) =
checkNonNull (nameof source) source

Expand Down

0 comments on commit e1f5161

Please sign in to comment.