Skip to content

Commit 6496832

Browse files
committed
Add support for CancellationToken to TaskSeq
1 parent ff2fd7d commit 6496832

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
@@ -649,6 +649,12 @@ module HighPriority =
649649
sm.Data.current <- ValueNone
650650
false)
651651

652+
// Binding to a cancellation token. This allows `do! someCancellationToken`
653+
member inline _.Bind(myToken: CancellationToken, continuation: (unit -> ResumableTSC<'T>)) : ResumableTSC<'T> =
654+
ResumableTSC<'T>(fun sm ->
655+
sm.Data.cancellationToken <- myToken
656+
(continuation ()).Invoke(&sm))
657+
652658
member inline _.Bind(computation: Async<'T>, continuation: ('T -> ResumableTSC<'U>)) =
653659
ResumableTSC<'U>(fun sm ->
654660
let mutable awaiter =

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

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

79+
let inline withCancellationToken (cancellationToken: CancellationToken) (source: taskSeq<'T>) = taskSeq {
80+
do! cancellationToken
81+
yield! source
82+
}
83+
7984
let isEmpty (source: TaskSeq<_>) =
8085
checkNonNull (nameof source) source
8186

0 commit comments

Comments
 (0)