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 Nov 6, 2023
1 parent 4a01e87 commit cdf7938
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 @@ -645,6 +645,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))

member inline _.Bind(computation: Async<'T>, continuation: ('T -> ResumableTSC<'U>)) =
ResumableTSC<'U>(fun sm ->
let mutable awaiter =
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 cdf7938

Please sign in to comment.