From af278d9867f5af05ca8be4e5455d40bee267b6e4 Mon Sep 17 00:00:00 2001 From: Casey Allred Date: Mon, 24 Feb 2020 15:49:31 -0700 Subject: [PATCH] large multi selects --- src/MultiSelect.elm | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/MultiSelect.elm b/src/MultiSelect.elm index 82125ac..b2921c7 100644 --- a/src/MultiSelect.elm +++ b/src/MultiSelect.elm @@ -104,18 +104,36 @@ selectedOptionsDecoder = |> Decode.map filterSelected + +{- | + To allow for large multi selects the length is first retrieved in a IE safe way. + Then a method that compiles to a JS loop is used to retrieve the options. +-} + + optionsDecoder : Decode.Decoder (List Option) optionsDecoder = - let - loop idx xs = - Decode.maybe (Decode.field (String.fromInt idx) optionDecoder) - |> Decode.andThen - (Maybe.map (\x -> loop (idx + 1) (x :: xs)) - >> Maybe.withDefault (Decode.succeed xs) - ) - in - (Decode.field "options" <| loop 0 []) - |> Decode.map List.reverse + Decode.field "options" + (Decode.keyValuePairs (Decode.maybe optionDecoder) + |> Decode.andThen + (\list -> + let + onlySuccess = + List.foldr + (\( _, next ) sofar -> + case next of + Nothing -> + sofar + + Just x -> + x :: sofar + ) + [] + list + in + Decode.succeed onlySuccess + ) + ) type alias Option =