|
| 1 | +namespace FSharp.Control |
| 2 | + |
| 3 | +open System.Collections.Generic |
| 4 | +open System.Threading |
| 5 | +open System.Threading.Tasks |
| 6 | + |
| 7 | +#nowarn "57" |
| 8 | +#nowarn "1204" |
| 9 | +#nowarn "3513" |
| 10 | + |
| 11 | + |
| 12 | +[<AutoOpen>] |
| 13 | +module TaskExtensions = |
| 14 | + open Microsoft.FSharp.Core.CompilerServices |
| 15 | + open Microsoft.FSharp.Core.CompilerServices.StateMachineHelpers |
| 16 | + open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators |
| 17 | + |
| 18 | + let rec WhileDynamic |
| 19 | + ( |
| 20 | + sm: byref<TaskStateMachine<'Data>>, |
| 21 | + condition: unit -> ValueTask<bool>, |
| 22 | + body: TaskCode<'Data, unit> |
| 23 | + ) : bool = |
| 24 | + let vt = condition () |
| 25 | + |
| 26 | + TaskBuilderBase.BindDynamic( |
| 27 | + &sm, |
| 28 | + vt, |
| 29 | + fun result -> |
| 30 | + TaskCode<_, _>(fun sm -> |
| 31 | + if result then |
| 32 | + if body.Invoke(&sm) then |
| 33 | + WhileDynamic(&sm, condition, body) |
| 34 | + else |
| 35 | + let rf = sm.ResumptionDynamicInfo.ResumptionFunc |
| 36 | + |
| 37 | + sm.ResumptionDynamicInfo.ResumptionFunc <- |
| 38 | + (TaskResumptionFunc<'Data>(fun sm -> WhileBodyDynamicAux(&sm, condition, body, rf))) |
| 39 | + |
| 40 | + false |
| 41 | + else |
| 42 | + true) |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | + and WhileBodyDynamicAux |
| 47 | + ( |
| 48 | + sm: byref<TaskStateMachine<'Data>>, |
| 49 | + condition: unit -> ValueTask<bool>, |
| 50 | + body: TaskCode<'Data, unit>, |
| 51 | + rf: TaskResumptionFunc<_> |
| 52 | + ) : bool = |
| 53 | + if rf.Invoke(&sm) then |
| 54 | + WhileDynamic(&sm, condition, body) |
| 55 | + else |
| 56 | + let rf = sm.ResumptionDynamicInfo.ResumptionFunc |
| 57 | + |
| 58 | + sm.ResumptionDynamicInfo.ResumptionFunc <- |
| 59 | + (TaskResumptionFunc<'Data>(fun sm -> WhileBodyDynamicAux(&sm, condition, body, rf))) |
| 60 | + |
| 61 | + false |
| 62 | + |
| 63 | + // Add asynchronous for loop to the 'task' computation builder |
| 64 | + type Microsoft.FSharp.Control.TaskBuilder with |
| 65 | + |
| 66 | + /// Used by `For`. F# currently doesn't support `while!`, so this cannot be called directly from the task CE |
| 67 | + /// This code is mostly a copy of TaskSeq.WhileAsync. |
| 68 | + member inline _.WhileAsync |
| 69 | + ( |
| 70 | + [<InlineIfLambda>] condition: unit -> ValueTask<bool>, |
| 71 | + body: TaskCode<_, unit> |
| 72 | + ) : TaskCode<_, _> = |
| 73 | + let mutable condition_res = true |
| 74 | + |
| 75 | + ResumableCode.While( |
| 76 | + (fun () -> condition_res), |
| 77 | + TaskCode<_, _>(fun sm -> |
| 78 | + let mutable __stack_condition_fin = true |
| 79 | + let __stack_vtask = condition () |
| 80 | + |
| 81 | + let mutable awaiter = __stack_vtask.GetAwaiter() |
| 82 | + |
| 83 | + if awaiter.IsCompleted then |
| 84 | + // logInfo "at WhileAsync: returning completed task" |
| 85 | + |
| 86 | + __stack_condition_fin <- true |
| 87 | + condition_res <- awaiter.GetResult() |
| 88 | + else |
| 89 | + // logInfo "at WhileAsync: awaiting non-completed task" |
| 90 | + |
| 91 | + // This will yield with __stack_fin = false |
| 92 | + // This will resume with __stack_fin = true |
| 93 | + let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm) |
| 94 | + __stack_condition_fin <- __stack_yield_fin |
| 95 | + |
| 96 | + if __stack_condition_fin then |
| 97 | + condition_res <- awaiter.GetResult() |
| 98 | + |
| 99 | + |
| 100 | + if __stack_condition_fin then |
| 101 | + if condition_res then body.Invoke(&sm) else true |
| 102 | + else |
| 103 | + sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm) |
| 104 | + false) |
| 105 | + ) |
| 106 | + |
| 107 | + member inline this.For(tasksq: IAsyncEnumerable<'T>, body: 'T -> TaskCode<_, unit>) : TaskCode<_, unit> = |
| 108 | + // tasksq |
| 109 | + // |> TaskSeq.iterAsync (body >> task.Run) |
| 110 | + // |> task.ReturnFrom |
| 111 | + |
| 112 | + // task.ReturnFrom <| |
| 113 | + // task { |
| 114 | + // let mutable continueWhile = true |
| 115 | + // use e = tasksq.GetAsyncEnumerator() |
| 116 | + // while continueWhile do |
| 117 | + // let! next = e.MoveNextAsync() |
| 118 | + // if next then |
| 119 | + // do! task.Run(body e.Current) |
| 120 | + // else |
| 121 | + // continueWhile <- false |
| 122 | + // } |
| 123 | + |
| 124 | + TaskCode<'TOverall, unit>(fun sm -> |
| 125 | + |
| 126 | + this |
| 127 | + .Using( |
| 128 | + tasksq.GetAsyncEnumerator(CancellationToken()), |
| 129 | + (fun e -> this.WhileAsync(e.MoveNextAsync, (fun sm -> (body e.Current).Invoke(&sm)))) |
| 130 | + ) |
| 131 | + .Invoke(&sm)) |
| 132 | + |
| 133 | + // temp example |
| 134 | + let foo () = task { |
| 135 | + let mutable sum = 0 |
| 136 | + |
| 137 | + let xs = taskSeq { |
| 138 | + 1 |
| 139 | + 2 |
| 140 | + 3 |
| 141 | + } |
| 142 | + |
| 143 | + for x in xs do |
| 144 | + sum <- sum + x |
| 145 | + } |
0 commit comments