Skip to content

Commit 7e3fe11

Browse files
mpizenbergandrewMacmurray
authored andcommitted
Introduce attemptEach to run a list of concurrent tasks
1 parent 99323c1 commit 7e3fe11

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/ConcurrentTask.elm

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module ConcurrentTask exposing
88
, fromResult, andThenDo, return, debug
99
, batch, sequence
1010
, map, andMap, map2, map3, map4, map5
11-
, attempt, Response(..), UnexpectedError(..), onProgress, Pool, pool
11+
, attempt, attemptEach, Response(..), UnexpectedError(..), onProgress, Pool, pool
1212
)
1313

1414
{-| A Task similar to `elm/core`'s `Task` but:
@@ -186,7 +186,7 @@ Here's a minimal complete example:
186186
, subscriptions = subscriptions
187187
}
188188
189-
@docs attempt, Response, UnexpectedError, onProgress, Pool, pool
189+
@docs attempt, attemptEach, Response, UnexpectedError, onProgress, Pool, pool
190190
191191
-}
192192

@@ -920,6 +920,27 @@ attempt config task =
920920
mappedTask
921921

922922

923+
{-| Start a list of `ConcurrentTask`s. This is identical to [attempt](ConcurrentTask#attempt) except with a `List` of tasks.
924+
Use this when you don't need to wait explicitly for all the tasks to finish.
925+
-}
926+
attemptEach :
927+
{ pool : Pool msg
928+
, send : Decode.Value -> Cmd msg
929+
, onComplete : Response x a -> msg
930+
}
931+
-> List (ConcurrentTask x a)
932+
-> ( Pool msg, Cmd msg )
933+
attemptEach config taskList =
934+
let
935+
attemptAccum : ConcurrentTask x a -> ( Pool msg, List (Cmd msg) ) -> ( Pool msg, List (Cmd msg) )
936+
attemptAccum task ( pool_, cmds_ ) =
937+
attempt { config | pool = pool_ } task
938+
|> Tuple.mapSecond (\cmd -> cmd :: cmds_)
939+
in
940+
List.foldl attemptAccum ( config.pool, [] ) taskList
941+
|> Tuple.mapSecond Cmd.batch
942+
943+
923944
{-| The value returned from a task when it completes (returned in the `OnComplete` msg).
924945
925946
Can be either:

0 commit comments

Comments
 (0)