Skip to content

Commit 4c735e2

Browse files
committed
Add let! support for async in the taskSeq CE
1 parent fdcc270 commit 4c735e2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs

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

660+
member inline _.Bind
661+
(
662+
asyncSource: Async<'TResult1>,
663+
continuation: ('TResult1 -> ResumableTSC<'T>)
664+
) : ResumableTSC<'T> =
665+
ResumableTSC<'T>(fun sm ->
666+
let mutable awaiter =
667+
Async
668+
.StartAsTask(asyncSource, 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+
660696
[<AutoOpen>]
661697
module TaskSeqBuilder =
662698
/// Builds an asynchronous task sequence based on IAsyncEnumerable<'T> using computation expression syntax.

src/FSharp.Control.TaskSeq/TaskSeqBuilder.fsi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,5 @@ module HighPriority =
191191
type TaskSeqBuilder with
192192

193193
member inline Bind: task: Task<'TResult1> * continuation: ('TResult1 -> ResumableTSC<'T>) -> ResumableTSC<'T>
194+
member inline Bind:
195+
asyncSource: Async<'TResult1> * continuation: ('TResult1 -> ResumableTSC<'T>) -> ResumableTSC<'T>

0 commit comments

Comments
 (0)