Skip to content

Commit 2bf9594

Browse files
committed
Add let! support for async and allow setting of CancellationToken
1 parent 327f1fa commit 2bf9594

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs

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

660+
member inline _.Bind
661+
(
662+
myAsync: Async<'TResult1>,
663+
continuation: ('TResult1 -> ResumableTSC<'T>)
664+
) : ResumableTSC<'T> =
665+
ResumableTSC<'T>(fun sm ->
666+
let mutable awaiter =
667+
Async
668+
.StartAsTask(myAsync, cancellationToken = sm.Data.cancellationToken)
669+
.GetAwaiter()
670+
671+
let mutable __stack_fin = true
672+
673+
Debug.logInfo "at Bind"
674+
675+
if not awaiter.IsCompleted then
676+
// This will yield with __stack_fin2 = false
677+
// This will resume with __stack_fin2 = true
678+
let __stack_fin2 = ResumableCode.Yield().Invoke(&sm)
679+
__stack_fin <- __stack_fin2
680+
681+
Debug.logInfo ("at Bind: with __stack_fin = ", __stack_fin)
682+
Debug.logInfo ("at Bind: this.completed = ", sm.Data.completed)
683+
684+
if __stack_fin then
685+
Debug.logInfo "at Bind: finished awaiting, calling continuation"
686+
let result = awaiter.GetResult()
687+
(continuation result).Invoke(&sm)
688+
689+
else
690+
Debug.logInfo "at Bind: await further"
691+
692+
sm.Data.awaiter <- awaiter
693+
sm.Data.current <- ValueNone
694+
false)
695+
696+
member inline _.Bind(myToken: CancellationToken, continuation: (unit -> ResumableTSC<'T>)) : ResumableTSC<'T> =
697+
ResumableTSC<'T>(fun sm ->
698+
sm.Data.cancellationToken <- myToken
699+
(continuation ()).Invoke(&sm))
700+
660701
[<AutoOpen>]
661702
module TaskSeqBuilder =
662703
/// Builds an asynchronous task sequence based on IAsyncEnumerable<'T> using computation expression syntax.

0 commit comments

Comments
 (0)