Skip to content

Commit 3a9a47c

Browse files
committed
Add support for CancellationToken to TaskSeq
1 parent fdcc270 commit 3a9a47c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,12 @@ module HighPriority =
657657
sm.Data.current <- ValueNone
658658
false)
659659

660+
// Binding to a cancellation token. This allows `do! someCancellationToken`
661+
member inline _.Bind(myToken: CancellationToken, continuation: (unit -> ResumableTSC<'T>)) : ResumableTSC<'T> =
662+
ResumableTSC<'T>(fun sm ->
663+
sm.Data.cancellationToken <- myToken
664+
(continuation ()).Invoke(&sm))
665+
660666
[<AutoOpen>]
661667
module TaskSeqBuilder =
662668
/// Builds an asynchronous task sequence based on IAsyncEnumerable<'T> using computation expression syntax.

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ module internal TaskSeqInternal =
6060
KeyNotFoundException("The predicate function or index did not satisfy any item in the async sequence.")
6161
|> raise
6262

63+
let inline withCancellationToken (cancellationToken: CancellationToken) (source: taskSeq<'T>) = taskSeq {
64+
do! cancellationToken
65+
yield! source
66+
}
67+
6368
let isEmpty (source: taskSeq<_>) = task {
6469
use e = source.GetAsyncEnumerator(CancellationToken())
6570
let! step = e.MoveNextAsync()

0 commit comments

Comments
 (0)