|
| 1 | +namespace FsToolkit.ErrorHandling |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Threading.Tasks |
| 5 | + |
| 6 | +[<AutoOpen>] |
| 7 | +module ParallelAsyncResultCE = |
| 8 | + |
| 9 | + type ParallelAsyncResultBuilder() = |
| 10 | + |
| 11 | + member inline _.Return(value: 'ok) : Async<Result<'ok, 'error>> = |
| 12 | + result.Return value |
| 13 | + |> async.Return |
| 14 | + |
| 15 | + member inline _.ReturnFrom(input: Async<Result<'ok, 'error>>) : Async<Result<'ok, 'error>> = |
| 16 | + input |
| 17 | + |
| 18 | + member inline _.Zero() : Async<Result<unit, 'error>> = |
| 19 | + result.Zero() |
| 20 | + |> async.Return |
| 21 | + |
| 22 | + member inline _.Bind |
| 23 | + ( |
| 24 | + asyncResult: Async<Result<'okInput, 'error>>, |
| 25 | + [<InlineIfLambda>] binder: 'okInput -> Async<Result<'okOutput, 'error>> |
| 26 | + ) : Async<Result<'okOutput, 'error>> = |
| 27 | + AsyncResult.bind binder asyncResult |
| 28 | + |
| 29 | + member inline _.Delay |
| 30 | + ([<InlineIfLambda>] generator: unit -> Async<Result<'ok, 'error>>) |
| 31 | + : Async<Result<'ok, 'error>> = |
| 32 | + async.Delay generator |
| 33 | + |
| 34 | + member inline this.Combine |
| 35 | + (computation1: Async<Result<unit, 'error>>, computation2: Async<Result<'ok, 'error>>) |
| 36 | + : Async<Result<'ok, 'error>> = |
| 37 | + this.Bind(computation1, (fun () -> computation2)) |
| 38 | + |
| 39 | + member inline _.TryWith |
| 40 | + ( |
| 41 | + computation: Async<Result<'ok, 'error>>, |
| 42 | + [<InlineIfLambda>] handler: System.Exception -> Async<Result<'ok, 'error>> |
| 43 | + ) : Async<Result<'ok, 'error>> = |
| 44 | + async.TryWith(computation, handler) |
| 45 | + |
| 46 | + member inline _.TryFinally |
| 47 | + (computation: Async<Result<'ok, 'error>>, [<InlineIfLambda>] compensation: unit -> unit) |
| 48 | + : Async<Result<'ok, 'error>> = |
| 49 | + async.TryFinally(computation, compensation) |
| 50 | +#if !FABLE_COMPILER |
| 51 | + member inline _.TryFinallyAsync |
| 52 | + ( |
| 53 | + computation: Async<Result<'ok, 'error>>, |
| 54 | + [<InlineIfLambda>] compensation: unit -> ValueTask |
| 55 | + ) : Async<Result<'ok, 'error>> = |
| 56 | + let compensation = |
| 57 | + async { |
| 58 | + let vTask = compensation () |
| 59 | + |
| 60 | + if vTask.IsCompletedSuccessfully then |
| 61 | + return () |
| 62 | + else |
| 63 | + return! |
| 64 | + vTask.AsTask() |
| 65 | + |> Async.AwaitTask |
| 66 | + } |
| 67 | + |
| 68 | + Async.TryFinallyAsync(computation, compensation) |
| 69 | + |
| 70 | + |
| 71 | + member inline this.Using |
| 72 | + ( |
| 73 | + resource: 'ok :> IAsyncDisposable, |
| 74 | + [<InlineIfLambda>] binder: 'ok -> Async<Result<'U, 'error>> |
| 75 | + ) : Async<Result<'U, 'error>> = |
| 76 | + this.TryFinallyAsync( |
| 77 | + binder resource, |
| 78 | + (fun () -> |
| 79 | + if not (isNull (box resource)) then |
| 80 | + resource.DisposeAsync() |
| 81 | + else |
| 82 | + ValueTask() |
| 83 | + ) |
| 84 | + ) |
| 85 | +#endif |
| 86 | + member inline this.While |
| 87 | + ([<InlineIfLambda>] guard: unit -> bool, computation: Async<Result<unit, 'error>>) |
| 88 | + : Async<Result<unit, 'error>> = |
| 89 | + if guard () then |
| 90 | + let mutable whileAsync = Unchecked.defaultof<_> |
| 91 | + |
| 92 | + whileAsync <- |
| 93 | + this.Bind(computation, (fun () -> if guard () then whileAsync else this.Zero())) |
| 94 | + |
| 95 | + whileAsync |
| 96 | + else |
| 97 | + this.Zero() |
| 98 | + |
| 99 | + |
| 100 | + member inline _.BindReturn |
| 101 | + (x: Async<Result<'okInput, 'error>>, [<InlineIfLambda>] f: 'okInput -> 'okOutput) |
| 102 | + : Async<Result<'okOutput, 'error>> = |
| 103 | + AsyncResult.map f x |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type. |
| 107 | + /// |
| 108 | + /// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder |
| 109 | + /// </summary> |
| 110 | + member inline _.Source(result: Async<Result<'ok, 'error>>) : Async<Result<'ok, 'error>> = |
| 111 | + result |
| 112 | + |
| 113 | + member inline _.MergeSources(input1, input2) : Async<Result<'a * 'b, 'error>> = |
| 114 | + ParallelAsyncResult.zip input1 input2 |
| 115 | + |
| 116 | + let parallelAsyncResult = ParallelAsyncResultBuilder() |
| 117 | + |
| 118 | + [<AutoOpen>] |
| 119 | + module MediumPriority = |
| 120 | + |
| 121 | + type ParallelAsyncResultBuilder with |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Needed to allow `for..in` and `for..do` functionality |
| 125 | + /// </summary> |
| 126 | + member inline _.Source(s: #seq<'value>) : #seq<'value> = s |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Method lets us transform data types into our internal representation. |
| 130 | + /// </summary> |
| 131 | + member inline _.Source(result: Result<'ok, 'error>) : Async<Result<'ok, 'error>> = |
| 132 | + Async.singleton result |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Method lets us transform data types into our internal representation. |
| 136 | + /// </summary> |
| 137 | + member inline _.Source(choice: Choice<'ok, 'error>) : Async<Result<'ok, 'error>> = |
| 138 | + choice |
| 139 | + |> Result.ofChoice |
| 140 | + |> Async.singleton |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Method lets us transform data types into our internal representation. |
| 144 | + /// </summary> |
| 145 | + member inline _.Source(asyncComputation: Async<'ok>) : Async<Result<'ok, 'error>> = |
| 146 | + asyncComputation |
| 147 | + |> Async.map Ok |
| 148 | + |
| 149 | + |
| 150 | + member inline _.Using |
| 151 | + ( |
| 152 | + resource: 'ok :> IDisposable, |
| 153 | + [<InlineIfLambda>] binder: 'ok -> Async<Result<'U, 'error>> |
| 154 | + ) : Async<Result<'U, 'error>> = |
| 155 | + async.Using(resource, binder) |
| 156 | + |
| 157 | + |
| 158 | + member inline this.For |
| 159 | + (sequence: #seq<'ok>, [<InlineIfLambda>] binder: 'ok -> Async<Result<unit, 'error>>) |
| 160 | + : Async<Result<unit, 'error>> = |
| 161 | + this.Using( |
| 162 | + sequence.GetEnumerator(), |
| 163 | + fun enum -> |
| 164 | + this.While( |
| 165 | + (fun () -> enum.MoveNext()), |
| 166 | + this.Delay(fun () -> binder enum.Current) |
| 167 | + ) |
| 168 | + ) |
| 169 | + |
| 170 | +#if !FABLE_COMPILER |
| 171 | + /// <summary> |
| 172 | + /// Method lets us transform data types into our internal representation. |
| 173 | + /// </summary> |
| 174 | + member inline _.Source(task: Task<'ok>) : Async<Result<'ok, 'error>> = |
| 175 | + task |
| 176 | + |> Async.AwaitTask |
| 177 | + |> Async.map Ok |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// Method lets us transform data types into our internal representation. |
| 181 | + /// </summary> |
| 182 | + member inline _.Source(task: Task) : Async<Result<unit, 'error>> = |
| 183 | + task |
| 184 | + |> Async.AwaitTask |
| 185 | + |> Async.map Ok |
| 186 | +#endif |
| 187 | + |
| 188 | +#if !FABLE_COMPILER |
| 189 | + [<AutoOpen>] |
| 190 | + module HighPriority = |
| 191 | + |
| 192 | + type ParallelAsyncResultBuilder with |
| 193 | + |
| 194 | + /// <summary> |
| 195 | + /// Method lets us transform data types into our internal representation. |
| 196 | + /// </summary> |
| 197 | + member inline _.Source(task: Task<Result<'ok, 'error>>) : Async<Result<'ok, 'error>> = |
| 198 | + task |
| 199 | + |> Async.AwaitTask |
| 200 | +#endif |
0 commit comments