Commit c9a42af1 authored by André Espaze's avatar André Espaze
Browse files

Task.sequence rewrite for failing with error message

parent 7235e0b2fc26
......@@ -7,13 +7,16 @@ module Common exposing
, expectStringResponse
, maybe
, resultEither
, taskSequenceEither
)
import Dict
import Either exposing (Either)
import Html.Styled exposing (Attribute)
import Html.Styled.Attributes exposing (class)
import Http exposing (Response)
import Json.Decode as D
import Task exposing (Task)
classes : List String -> Attribute msg
......@@ -110,3 +113,33 @@ checkUrlPrefix x =
else
x
taskSequenceEither : List (Task x a) -> Task (List x) (List (Either x a))
taskSequenceEither tasks =
Task.andThen
(\xs ->
let
lefts =
Either.lefts xs
in
if not (List.isEmpty xs) && (List.length lefts == List.length xs) then
Task.fail lefts
else
Task.succeed xs
)
(List.foldr
(\a b ->
let
rightTask =
Task.map Either.Right a
in
Task.map2
(::)
(Task.onError (Either.Left >> Task.succeed) rightTask)
b
)
(Task.succeed [])
tasks
)
......@@ -43,6 +43,10 @@ type alias NamedSerie =
( String, Serie )
type alias NamedError =
( String, String )
serieDecoder : Decoder Serie
serieDecoder =
Decode.dict Decode.float
......@@ -58,7 +62,7 @@ type Msg
| ToggleItem String
| SearchSeries String
| MakeSearch
| RenderPlot (Result String ( SeriesCache, List NamedSerie ))
| RenderPlot (Result (List String) ( SeriesCache, List NamedSerie, List NamedError ))
type alias Trace =
......@@ -109,7 +113,10 @@ plotFigure =
node "plot-figure"
fetchSeries : List String -> Model -> Task String ( SeriesCache, List NamedSerie )
fetchSeries :
List String
-> Model
-> Task (List String) ( SeriesCache, List NamedSerie, List NamedError )
fetchSeries selectedNames model =
let
( usedCache, cachedSeries ) =
......@@ -150,9 +157,9 @@ fetchSeries selectedNames model =
Common.decodeJsonMessage serieDecoder
}
getMissingSeries : Task String (List Serie)
getMissingSeries : Task (List String) (List (Either String Serie))
getMissingSeries =
Task.sequence <| List.map getSerie missingNames
Common.taskSequenceEither <| List.map getSerie missingNames
getSeries : List NamedSerie -> List NamedSerie
getSeries missing =
......@@ -174,13 +181,31 @@ fetchSeries selectedNames model =
missing
in
getMissingSeries
|> Task.onError (List.map Either.Left >> Task.succeed)
|> Task.andThen
(\missingSeries ->
(\missings ->
let
xs : List (Either NamedError NamedSerie)
xs =
List.map2 Tuple.pair missingNames missingSeries
List.map2
(\name x ->
let
addName =
Tuple.pair name
in
Either.mapBoth addName addName x
)
missingNames
missings
missingSeries =
Either.rights xs
in
Task.succeed ( updateCache xs, getSeries xs )
Task.succeed
( updateCache missingSeries
, getSeries missingSeries
, Either.lefts xs
)
)
......@@ -242,7 +267,11 @@ update msg model =
MakeSearch ->
newModel { model | searchedSeries = keywordMatch model.searchString model.series }
RenderPlot (Ok ( cache, namedSeries )) ->
RenderPlot (Ok ( cache, namedSeries, namedErrors )) ->
let
_ =
Debug.log "Named errors" namedErrors
in
( { model | cache = cache, selectedNamedSeries = namedSeries }, Cmd.none )
RenderPlot (Err x) ->
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment