Skip to content

Commit cdf7938

Browse files
committed
Add support for CancellationToken to TaskSeq
1 parent 4a01e87 commit cdf7938

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
@@ -645,6 +645,12 @@ module HighPriority =
645645
sm.Data.current <- ValueNone
646646
false)
647647

648+
// Binding to a cancellation token. This allows `do! someCancellationToken`
649+
member inline _.Bind(myToken: CancellationToken, continuation: (unit -> ResumableTSC<'T>)) : ResumableTSC<'T> =
650+
ResumableTSC<'T>(fun sm ->
651+
sm.Data.cancellationToken <- myToken
652+
(continuation ()).Invoke(&sm))
653+
648654
member inline _.Bind(computation: Async<'T>, continuation: ('T -> ResumableTSC<'U>)) =
649655
ResumableTSC<'U>(fun sm ->
650656
let mutable awaiter =

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

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

70+
let inline withCancellationToken (cancellationToken: CancellationToken) (source: taskSeq<'T>) = taskSeq {
71+
do! cancellationToken
72+
yield! source
73+
}
74+
7075
let isEmpty (source: TaskSeq<_>) =
7176
checkNonNull (nameof source) source
7277

0 commit comments

Comments
 (0)