Skip to content

Commit 23fb290

Browse files
committed
Introduce attemptEach to run a list of concurrent tasks
1 parent 99323c1 commit 23fb290

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/ConcurrentTask.elm

Lines changed: 24 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,28 @@ 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+
925+
Use this when you don't need to wait explicitly for all the tasks to finish.
926+
927+
-}
928+
attemptEach :
929+
{ pool : Pool msg
930+
, send : Decode.Value -> Cmd msg
931+
, onComplete : Response x a -> msg
932+
}
933+
-> List (ConcurrentTask x a)
934+
-> ( Pool msg, Cmd msg )
935+
attemptEach config taskList =
936+
let
937+
attemptAccum task ( pool_, cmds_ ) =
938+
attempt { config | pool = pool_ } task
939+
|> Tuple.mapSecond (\cmd -> cmd :: cmds_)
940+
in
941+
List.foldl attemptAccum ( config.pool, [] ) taskList
942+
|> Tuple.mapSecond Cmd.batch
943+
944+
923945
{-| The value returned from a task when it completes (returned in the `OnComplete` msg).
924946
925947
Can be either:

0 commit comments

Comments
 (0)