Created
August 2, 2019 15:32
-
-
Save PanagiotisGeorgiadis/291c23712b63dae416ea1e2143a63462 to your computer and use it in GitHub Desktop.
Testing Elm concurrency with a kind of extreme example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (main) | |
import Html exposing (Html, br, div, input, text) | |
import Html.Attributes exposing (defaultValue, type_, value) | |
import Html.Events exposing (onInput) | |
-- Elm 0.18 implementation | |
-- Example can be found on https://ellie-app.com/sbw95cJgz2a1 | |
-- MODEL | |
type alias Model = | |
{ inputValues : List String | |
} | |
init : ( Model, Cmd Msg ) | |
init = | |
( { inputValues = List.repeat 5000 "" | |
} | |
, Cmd.none | |
) | |
-- UPDATE | |
type Msg | |
= NoOp | |
| CaptureInput Int String | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
CaptureInput index val -> | |
let | |
_ = | |
Debug.log "val" <| String.length val | |
updatedModel = | |
{ model | |
| inputValues = | |
List.take index model.inputValues | |
++ [ val ] | |
++ List.drop (index + 1) model.inputValues | |
} | |
in | |
( updatedModel | |
, Cmd.none | |
) | |
NoOp -> | |
( model, Cmd.none ) | |
-- VIEW | |
textHtml : String -> List (Html Msg) | |
textHtml val = | |
[ br [] [] | |
, text val | |
] | |
inputHtml : Int -> String -> List (Html Msg) | |
inputHtml index val = | |
[ br [] [] | |
, text val | |
, br [] [] | |
{- Transform this to an "uncontrolled" component in order | |
to solve the concurrency problem here. ie remove the | |
`value` attribute. | |
-} | |
, input [ type_ "text", onInput (CaptureInput index), value val ] [] | |
] | |
view : Model -> Html Msg | |
view model = | |
div [] | |
([ text "Race Condition checking" ] | |
++ (List.concat <| List.indexedMap inputHtml model.inputValues) | |
++ (List.concat <| List.map textHtml model.inputValues) | |
) | |
-- PROGRAM | |
main : Program Never Model Msg | |
main = | |
Html.program | |
{ view = view | |
, init = init | |
, update = update | |
, subscriptions = always Sub.none | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (main) | |
import Browser | |
import Html exposing (Html, br, div, input, text) | |
import Html.Attributes exposing (type_, value) | |
import Html.Events exposing (onInput) | |
-- Elm 0.19 implementation ( throws a "Maximum CallStack size exceeded" runtime error. Need to take a look ) | |
-- MODEL | |
type alias Model = | |
{ inputValues : List String | |
} | |
initialModel : ( Model, Cmd Msg ) | |
initialModel = | |
( { inputValues = List.repeat 5000 "" | |
} | |
, Cmd.none | |
) | |
-- UPDATE | |
type Msg | |
= NoOp | |
| CaptureInput Int String | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
CaptureInput index val -> | |
let | |
_ = | |
Debug.log "val" <| String.length val | |
updatedModel = | |
{ model | |
| inputValues = | |
List.take index model.inputValues | |
++ [ val ] | |
++ List.drop (index + 1) model.inputValues | |
} | |
in | |
( updatedModel | |
, Cmd.none | |
) | |
NoOp -> | |
( model | |
, Cmd.none | |
) | |
-- VIEW | |
textHtml : String -> List (Html Msg) | |
textHtml val = | |
[ br [] [] | |
, text val | |
] | |
inputHtml : Int -> String -> List (Html Msg) | |
inputHtml index val = | |
[ br [] [] | |
, text val | |
, br [] [] | |
{- Transform this to an "uncontrolled" component in order | |
to solve the concurrency problem here. | |
-} | |
, input [ type_ "text", onInput (CaptureInput index), value val ] [] | |
] | |
view : Model -> Html Msg | |
view model = | |
div [] | |
([ text "Race Condition checking" ] | |
++ (List.concat <| List.indexedMap inputHtml model.inputValues) | |
++ (List.concat <| List.map textHtml model.inputValues) | |
) | |
-- PROGRAM | |
main : Program () Model Msg | |
main = | |
Browser.element | |
{ init = \_ -> initialModel | |
, view = view | |
, update = update | |
, subscriptions = \_ -> Sub.none | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment