@@ -322,8 +322,54 @@ module TaskSeq =
322322
323323 let foldAsync folder state source = Internal.fold ( AsyncFolderAction folder) state source
324324
325+ #nowarn " 1204"
326+ #nowarn " 3513"
327+
328+
325329[<AutoOpen>]
326330module AsyncSeqExtensions =
331+
332+ let rec WhileDynamic
333+ (
334+ sm : byref < TaskStateMachine < 'Data >>,
335+ condition : unit -> ValueTask < bool >,
336+ body : TaskCode < 'Data , unit >
337+ ) : bool =
338+ let vt = condition ()
339+ TaskBuilderBase.BindDynamic(& sm, vt, fun result ->
340+ TaskCode<_,_>( fun sm ->
341+ if result then
342+ if body.Invoke(& sm) then
343+ WhileDynamic(& sm, condition, body)
344+ else
345+ let rf = sm.ResumptionDynamicInfo.ResumptionFunc
346+
347+ sm.ResumptionDynamicInfo.ResumptionFunc <-
348+ ( TaskResumptionFunc< 'Data>( fun sm -> WhileBodyDynamicAux(& sm, condition, body, rf)))
349+
350+ false
351+ else
352+ true
353+ )
354+ )
355+
356+
357+ and WhileBodyDynamicAux
358+ (
359+ sm : byref < TaskStateMachine < 'Data >>,
360+ condition : unit -> ValueTask < bool >,
361+ body : TaskCode < 'Data , unit >,
362+ rf : TaskResumptionFunc < _ >
363+ ) : bool =
364+ if rf.Invoke(& sm) then
365+ WhileDynamic(& sm, condition, body)
366+ else
367+ let rf = sm.ResumptionDynamicInfo.ResumptionFunc
368+
369+ sm.ResumptionDynamicInfo.ResumptionFunc <-
370+ ( TaskResumptionFunc< 'Data>( fun sm -> WhileBodyDynamicAux(& sm, condition, body, rf)))
371+
372+ false
327373 open Microsoft.FSharp .Core .CompilerServices
328374
329375 // Add asynchronous for loop to the 'async' computation builder
@@ -337,17 +383,35 @@ module AsyncSeqExtensions =
337383 // Add asynchronous for loop to the 'task' computation builder
338384 type Microsoft.FSharp.Control.TaskBuilder with
339385
386+
387+ member inline this.While ( condition : unit -> ValueTask < bool >, body : TaskCode < 'TOverall , unit >) =
388+ TaskCode<_,_>( fun sm ->
389+ WhileDynamic(& sm, condition, body)
390+
391+ )
392+
393+
394+
340395 member inline this.For
341396 (
342397 tasksq : IAsyncEnumerable < 'T >,
343398 body : 'T -> TaskCode < 'TOverall , unit >
344399 ) : TaskCode < 'TOverall , unit > =
345400 TaskCode< 'TOverall, unit>( fun sm ->
401+
346402 this
347403 .Using(
348404 tasksq.GetAsyncEnumerator( CancellationToken()),
349405 ( fun e ->
350- // TODO: fix 'true' with e.MoveNextAsync()
351- this.While(( fun () -> true ) , ( fun sm -> ( body e.Current) .Invoke(& sm))))
406+ let next () = e.MoveNextAsync()
407+ this.While( next , ( fun sm -> ( body e.Current) .Invoke(& sm))))
352408 )
353409 .Invoke(& sm))
410+
411+ let foo () =
412+ task {
413+ let mutable sum = 0
414+ let xs = taskSeq { 1 ; 2 ; 3 }
415+ for x in xs do
416+ sum <- sum + x
417+ }
0 commit comments