|
| 1 | +namespace FsToolkit.ErrorHandling |
| 2 | + |
| 3 | +open System |
| 4 | + |
| 5 | +[<RequireQualifiedAccess>] |
| 6 | +module ParallelAsyncResult = |
| 7 | + |
| 8 | + [<AutoOpen>] |
| 9 | + module InternalHelpers = |
| 10 | + |
| 11 | + type AsyncResultErrorException<'a>(value: 'a) = |
| 12 | + inherit Exception() |
| 13 | + member this.Value = value |
| 14 | + |
| 15 | + let toBoxedAsync (input: Async<Result<'ok, 'error>>) : Async<obj> = |
| 16 | + async { |
| 17 | + match! input with |
| 18 | + | Ok x -> return box x |
| 19 | + | Error e -> return raise (AsyncResultErrorException<'error>(e)) |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Transforms two AsyncResults in one that executes them concurrently and combines the results using the specified function. |
| 24 | + /// If either AsyncResult resolves to an error, then the other is cancelled and only the first error is returned. |
| 25 | + /// </summary> |
| 26 | + /// <param name="mapper">The function to apply to the values of the AsyncResult values.</param> |
| 27 | + /// <param name="input1">The first AsyncResult value to transform.</param> |
| 28 | + /// <param name="input2">The second AsyncResult value to transform.</param> |
| 29 | + /// <returns>The transformed AsyncResult value.</returns> |
| 30 | + let inline map2 |
| 31 | + ([<InlineIfLambda>] mapper: 'a -> 'b -> 'c) |
| 32 | + (input1: Async<Result<'a, 'error>>) |
| 33 | + (input2: Async<Result<'b, 'error>>) |
| 34 | + : Async<Result<'c, 'error>> = |
| 35 | + async { |
| 36 | + try |
| 37 | + return! |
| 38 | + Async.parallelMap2 |
| 39 | + (fun a b -> |
| 40 | + let a = unbox<'a> a |
| 41 | + let b = unbox<'b> b |
| 42 | + Ok(mapper a b) |
| 43 | + ) |
| 44 | + (toBoxedAsync input1) |
| 45 | + (toBoxedAsync input2) |
| 46 | + |
| 47 | + with :? AsyncResultErrorException<'error> as exn -> |
| 48 | + return Error exn.Value |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Transforms three AsyncResults in one that executes them concurrently and combines the results using the specified function. |
| 53 | + /// If any AsyncResult resolves to an error, then the others are cancelled and only the first error is returned. |
| 54 | + /// </summary> |
| 55 | + /// <param name="mapper">The function to apply to the values of the AsyncResult values.</param> |
| 56 | + /// <param name="input1">The first AsyncResult value to transform.</param> |
| 57 | + /// <param name="input2">The second AsyncResult value to transform.</param> |
| 58 | + /// <param name="input3">The third AsyncResult value to transform.</param> |
| 59 | + /// <returns>The transformed AsyncResult value.</returns> |
| 60 | + let inline map3 |
| 61 | + ([<InlineIfLambda>] mapper: 'a -> 'b -> 'c -> 'd) |
| 62 | + (input1: Async<Result<'a, 'error>>) |
| 63 | + (input2: Async<Result<'b, 'error>>) |
| 64 | + (input3: Async<Result<'c, 'error>>) |
| 65 | + : Async<Result<'d, 'error>> = |
| 66 | + async { |
| 67 | + try |
| 68 | + return! |
| 69 | + Async.parallelMap3 |
| 70 | + (fun a b c -> |
| 71 | + let a = unbox<'a> a |
| 72 | + let b = unbox<'b> b |
| 73 | + let c = unbox<'c> c |
| 74 | + Ok(mapper a b c) |
| 75 | + ) |
| 76 | + (toBoxedAsync input1) |
| 77 | + (toBoxedAsync input2) |
| 78 | + (toBoxedAsync input3) |
| 79 | + |
| 80 | + with :? AsyncResultErrorException<'error> as exn -> |
| 81 | + return Error exn.Value |
| 82 | + } |
| 83 | + |
| 84 | + let inline zip |
| 85 | + (a: Async<Result<'a, 'error>>) |
| 86 | + (b: Async<Result<'b, 'error>>) |
| 87 | + : Async<Result<'a * 'b, 'error>> = |
| 88 | + map2 (fun a b -> a, b) a b |
0 commit comments