Skip to content

Commit e2e6938

Browse files
committed
Simplify the Pool type
1 parent e7bbca3 commit e2e6938

File tree

2 files changed

+70
-59
lines changed

2 files changed

+70
-59
lines changed

src/ConcurrentTask.elm

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -882,18 +882,42 @@ Make sure to update your `Model` and pass in the `Cmd` returned from `attempt`.
882882
883883
-}
884884
attempt :
885-
{ pool : Pool msg x a
885+
{ pool : Pool msg
886886
, send : Decode.Value -> Cmd msg
887887
, onComplete : Response x a -> msg
888888
}
889889
-> ConcurrentTask x a
890-
-> ( Pool msg x a, Cmd msg )
891-
attempt options =
890+
-> ( Pool msg, Cmd msg )
891+
attempt config task =
892+
let
893+
mappedTask : ConcurrentTask msg msg
894+
mappedTask =
895+
task
896+
|> map (\res -> config.onComplete (Success res))
897+
|> onError
898+
(\err ->
899+
config.onComplete (Error err)
900+
|> succeed
901+
)
902+
903+
onComplete : Internal.Response msg msg -> msg
904+
onComplete res =
905+
case res of
906+
Internal.Success s ->
907+
s
908+
909+
Internal.Error e ->
910+
e
911+
912+
Internal.UnexpectedError e ->
913+
config.onComplete (UnexpectedError (toUnexpectedError e))
914+
in
892915
Internal.attempt
893-
{ pool = options.pool
894-
, send = options.send
895-
, onComplete = toResponse >> options.onComplete
916+
{ pool = config.pool
917+
, send = config.send
918+
, onComplete = onComplete
896919
}
920+
mappedTask
897921

898922

899923
{-| The value returned from a task when it completes (returned in the `OnComplete` msg).
@@ -939,19 +963,6 @@ type UnexpectedError
939963
| InternalError String
940964

941965

942-
toResponse : Internal.Response x a -> Response x a
943-
toResponse res =
944-
case res of
945-
Internal.Success a ->
946-
Success a
947-
948-
Internal.Error x ->
949-
Error x
950-
951-
Internal.UnexpectedError e ->
952-
UnexpectedError (toUnexpectedError e)
953-
954-
955966
toUnexpectedError : Internal.UnexpectedError -> UnexpectedError
956967
toUnexpectedError err =
957968
case err of
@@ -1000,9 +1011,9 @@ Make sure to update your `Model` and pass in the `Cmd` in your `OnProgress` bran
10001011
onProgress :
10011012
{ send : Decode.Value -> Cmd msg
10021013
, receive : (Decode.Value -> msg) -> Sub msg
1003-
, onProgress : ( Pool msg x a, Cmd msg ) -> msg
1014+
, onProgress : ( Pool msg, Cmd msg ) -> msg
10041015
}
1005-
-> Pool msg x a
1016+
-> Pool msg
10061017
-> Sub msg
10071018
onProgress =
10081019
Internal.onProgress
@@ -1011,8 +1022,8 @@ onProgress =
10111022
{-| A Pool keeps track of each task's progress,
10121023
and allows multiple Task attempts to be in-flight at the same time.
10131024
-}
1014-
type alias Pool msg x a =
1015-
Internal.Pool msg x a
1025+
type alias Pool msg =
1026+
Internal.Pool msg
10161027

10171028

10181029
{-| Create an empty ConcurrentTask Pool.
@@ -1024,6 +1035,6 @@ Right now it doesn't expose any functionality, but it could be used in the futur
10241035
- Expose metrics on previous or running tasks.
10251036
10261037
-}
1027-
pool : Pool msg x a
1038+
pool : Pool msg
10281039
pool =
10291040
Internal.pool

src/ConcurrentTask/Internal/ConcurrentTask.elm

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,14 @@ mapResponseError f res =
562562
-- Execute a Task
563563

564564

565-
type Pool msg x a
566-
= Pool (Pool_ msg x a)
565+
type Pool msg
566+
= Pool (Pool_ msg)
567567

568568

569-
type alias Pool_ msg x a =
569+
type alias Pool_ msg =
570570
{ poolId : PoolId
571-
, queued : List ( Array Todo, Progress msg x a )
572-
, attempts : Dict AttemptId (Progress msg x a)
571+
, queued : List ( Array Todo, Progress msg )
572+
, attempts : Dict AttemptId (Progress msg)
573573
, attemptIds : Ids
574574
}
575575

@@ -595,10 +595,10 @@ type PoolId
595595
| Identified String
596596

597597

598-
type alias Progress msg x a =
598+
type alias Progress msg =
599599
{ inFlight : Set TaskId
600-
, task : ( Ids, ConcurrentTask x a )
601-
, onComplete : Response x a -> msg
600+
, task : ( Ids, ConcurrentTask msg msg )
601+
, onComplete : Response msg msg -> msg
602602
}
603603

604604

@@ -617,21 +617,21 @@ type alias AttemptId =
617617
Ids.Id
618618

619619

620-
type alias Attempt msg x a =
621-
{ pool : Pool msg x a
620+
type alias Attempt msg =
621+
{ pool : Pool msg
622622
, send : Decode.Value -> Cmd msg
623-
, onComplete : Response x a -> msg
623+
, onComplete : Response msg msg -> msg
624624
}
625625

626626

627-
type alias OnProgress msg x a =
627+
type alias OnProgress msg =
628628
{ send : Decode.Value -> Cmd msg
629629
, receive : (Decode.Value -> msg) -> Sub msg
630-
, onProgress : ( Pool msg x a, Cmd msg ) -> msg
630+
, onProgress : ( Pool msg, Cmd msg ) -> msg
631631
}
632632

633633

634-
attempt : Attempt msg x a -> ConcurrentTask x a -> ( Pool msg x a, Cmd msg )
634+
attempt : Attempt msg -> ConcurrentTask msg msg -> ( Pool msg, Cmd msg )
635635
attempt attempt_ task =
636636
case stepTask Dict.empty ( Ids.init, task ) of
637637
( _, Done res ) ->
@@ -641,7 +641,7 @@ attempt attempt_ task =
641641

642642
( _, Pending defs _ ) ->
643643
let
644-
progress : Progress msg x a
644+
progress : Progress msg
645645
progress =
646646
{ task = ( Ids.init, task )
647647
, inFlight = recordSent defs Set.empty
@@ -669,12 +669,12 @@ attempt attempt_ task =
669669

670670

671671
startTask :
672-
{ progress : Progress msg x a
673-
, pool : Pool msg x a
672+
{ progress : Progress msg
673+
, pool : Pool msg
674674
, send : Encode.Value -> Cmd msg
675675
, defs : Array Todo
676676
}
677-
-> ( Pool msg x a, Cmd msg )
677+
-> ( Pool msg, Cmd msg )
678678
startTask options =
679679
( startAttempt options.progress options.pool
680680
, options.send (encodeDefinitions (currentAttemptId options.pool) options.defs)
@@ -694,7 +694,7 @@ decodeIdentifyResponse =
694694
)
695695

696696

697-
onProgress : OnProgress msg x a -> Pool msg x a -> Sub msg
697+
onProgress : OnProgress msg -> Pool msg -> Sub msg
698698
onProgress options pool_ =
699699
options.receive
700700
(\rawResults ->
@@ -726,7 +726,7 @@ onProgress options pool_ =
726726
)
727727

728728

729-
startQueuedTasks : { send : Encode.Value -> Cmd msg, pool : Pool msg x a } -> ( Pool msg x a, Cmd msg )
729+
startQueuedTasks : { send : Encode.Value -> Cmd msg, pool : Pool msg } -> ( Pool msg, Cmd msg )
730730
startQueuedTasks options =
731731
queuedTasks options.pool
732732
|> List.foldl
@@ -743,12 +743,12 @@ startQueuedTasks options =
743743
|> Tuple.mapFirst clearQueue
744744

745745

746-
updateAttempt : OnProgress msg x a -> Pool msg x a -> ( AttemptId, Results ) -> Progress msg x a -> ( Pool msg x a, Cmd msg )
746+
updateAttempt : OnProgress msg -> Pool msg -> ( AttemptId, Results ) -> Progress msg -> ( Pool msg, Cmd msg )
747747
updateAttempt options pool_ ( attemptId, results ) progress =
748748
case stepTask results progress.task of
749749
( ids_, Pending _ next_ ) ->
750750
let
751-
nextProgress : ( Ids, ConcurrentTask x a )
751+
nextProgress : ( Ids, ConcurrentTask msg msg )
752752
nextProgress =
753753
( ids_, next_ )
754754
in
@@ -800,7 +800,7 @@ sendResult onComplete res =
800800
CoreTask.succeed res |> CoreTask.perform onComplete
801801

802802

803-
notStarted : Progress msg x a -> Todo -> Bool
803+
notStarted : Progress msg -> Todo -> Bool
804804
notStarted model def =
805805
not (Set.member def.taskId model.inFlight)
806806

@@ -994,7 +994,7 @@ encodeDefinition attemptId def =
994994
-- Pool
995995

996996

997-
pool : Pool msg x a
997+
pool : Pool msg
998998
pool =
999999
Pool
10001000
{ poolId = Unidentified
@@ -1004,7 +1004,7 @@ pool =
10041004
}
10051005

10061006

1007-
startAttempt : Progress msg x a -> Pool msg x a -> Pool msg x a
1007+
startAttempt : Progress msg -> Pool msg -> Pool msg
10081008
startAttempt progress p =
10091009
mapPool
10101010
(\pool_ ->
@@ -1016,7 +1016,7 @@ startAttempt progress p =
10161016
p
10171017

10181018

1019-
currentAttemptId : Pool msg x a -> AttemptId
1019+
currentAttemptId : Pool msg -> AttemptId
10201020
currentAttemptId (Pool pool_) =
10211021
case pool_.poolId of
10221022
Identified id ->
@@ -1029,47 +1029,47 @@ currentAttemptId (Pool pool_) =
10291029
Ids.get pool_.attemptIds
10301030

10311031

1032-
poolId : Pool msg x a -> PoolId
1032+
poolId : Pool msg -> PoolId
10331033
poolId (Pool pool_) =
10341034
pool_.poolId
10351035

10361036

1037-
withPoolId : PoolId -> Pool msg x a -> Pool msg x a
1037+
withPoolId : PoolId -> Pool msg -> Pool msg
10381038
withPoolId id =
10391039
mapPool (\pool_ -> { pool_ | poolId = id })
10401040

10411041

1042-
queueTask : ( Array Todo, Progress msg x a ) -> Pool msg x a -> Pool msg x a
1042+
queueTask : ( Array Todo, Progress msg ) -> Pool msg -> Pool msg
10431043
queueTask progress =
10441044
mapPool (\pool_ -> { pool_ | queued = progress :: pool_.queued })
10451045

10461046

1047-
updateProgressFor : AttemptId -> Progress msg x a -> Pool msg x a -> Pool msg x a
1047+
updateProgressFor : AttemptId -> Progress msg -> Pool msg -> Pool msg
10481048
updateProgressFor attemptId progress_ =
10491049
mapPool (\pool_ -> { pool_ | attempts = Dict.update attemptId (Maybe.map (always progress_)) pool_.attempts })
10501050

10511051

1052-
removeFromPool : AttemptId -> Pool msg x a -> Pool msg x a
1052+
removeFromPool : AttemptId -> Pool msg -> Pool msg
10531053
removeFromPool attemptId =
10541054
mapPool (\pool_ -> { pool_ | attempts = Dict.remove attemptId pool_.attempts })
10551055

10561056

1057-
queuedTasks : Pool msg x a -> List ( Array Todo, Progress msg x a )
1057+
queuedTasks : Pool msg -> List ( Array Todo, Progress msg )
10581058
queuedTasks (Pool p) =
10591059
p.queued
10601060

10611061

1062-
clearQueue : Pool msg x a -> Pool msg x a
1062+
clearQueue : Pool msg -> Pool msg
10631063
clearQueue =
10641064
mapPool (\pool_ -> { pool_ | queued = [] })
10651065

10661066

1067-
findAttempt : AttemptId -> Pool msg x a -> Maybe (Progress msg x a)
1067+
findAttempt : AttemptId -> Pool msg -> Maybe (Progress msg)
10681068
findAttempt attemptId (Pool p) =
10691069
Dict.get attemptId p.attempts
10701070

10711071

1072-
mapPool : (Pool_ msg x a -> Pool_ msg x a) -> Pool msg x a -> Pool msg x a
1072+
mapPool : (Pool_ msg -> Pool_ msg) -> Pool msg -> Pool msg
10731073
mapPool f (Pool p) =
10741074
Pool (f p)
10751075

0 commit comments

Comments
 (0)