Skip to content

Introduce attemptEach to run concurrently a List ConcurrentTask#36

Merged
andrewMacmurray merged 1 commit intoandrewMacmurray:mainfrom
mpizenberg:attempt-each
Jul 17, 2025
Merged

Introduce attemptEach to run concurrently a List ConcurrentTask#36
andrewMacmurray merged 1 commit intoandrewMacmurray:mainfrom
mpizenberg:attempt-each

Conversation

@mpizenberg
Copy link
Contributor

The rationale behind this new attemptEach function is to make it easier to run multiple tasks concurrently without having to synchronize them with a function like batch. This is mostly useful to run a bunch of independent tasks concurrently.

Let me know what you think, and don’t hesitate to change anything in the PR.

@andrewMacmurray
Copy link
Owner

Ok, finally aiming to get a bunch of these helpers merged now. Thanks for your input on this. Would you mind rebasing your branch on latest main and updating a couple of things?

I don't think it's useful for attemptEach to return a List (Cmd msg)- Cmds are opaque and there's not a lot you can do with them other than run them, so we might as well batch them in the helper. Could you modify the function to something like:

attemptEach :
    { pool : Pool msg
    , send : Decode.Value -> Cmd msg
    , onComplete : Response x a -> msg
    }
    -> List (ConcurrentTask x a)
    -> ( Pool msg, Cmd msg )
attemptEach options =
    List.foldl
        (\task ( pool_, cmds ) ->
            task
                |> attempt { options | pool = pool_ }
                |> Tuple.mapSecond (\nextCmd -> Cmd.batch [ nextCmd, cmds ])
        )
        ( options.pool, Cmd.none )

We can probably simplify the doc comment too, what about:

{-| Start a list of `ConcurrentTask`s. This is identical to [attempt](ConcurrentTask#attempt) except with a `List` of tasks.

    Use this when you don't need to wait explicitly for all the tasks to finish.

-}

Let me know if you don't have time for this and I can get it merged separately.

@mpizenberg
Copy link
Contributor Author

I’ve made the rebase and most suggested changes. Tiny diff is I still accumulates the list of commands and batch at the end. It feels a bit too wrong for my ocd to wrap batch a list of 2 elements at each step. But don’t hesitate to modify if you prefer the other way.

@andrewMacmurray
Copy link
Owner

Looks good, pushed a small fixup to make the docs format nicely.

@andrewMacmurray andrewMacmurray merged commit cb63d6e into andrewMacmurray:main Jul 17, 2025
4 checks passed
@mpizenberg mpizenberg deleted the attempt-each branch July 17, 2025 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants