Skip to content

Instantly share code, notes, and snippets.

@micktwomey
Last active May 17, 2016 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micktwomey/c1eb0f2354a5ab671f3e633c983b3b0e to your computer and use it in GitHub Desktop.
Save micktwomey/c1eb0f2354a5ab671f3e633c983b3b0e to your computer and use it in GitHub Desktop.
Reproduces TypeError seen in Elm 0.17
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "4.0.0 <= v < 5.0.0",
"elm-lang/html": "1.0.0 <= v < 2.0.0",
"elm-lang/websocket": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
}
{- Simple wrapper to WebSocketExample -}
module Main exposing (..)
import Html exposing (..)
import Html.App as App
import WebSocketExample
main : Program Never
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
type alias Model =
{ ws : WebSocketExample.Model }
init : ( Model, Cmd Msg )
init =
let
( wsModel, msg ) =
WebSocketExample.init
model =
Model wsModel
in
( model, Cmd.map WebSocketExample msg )
type Msg
= WebSocketExample WebSocketExample.Msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
WebSocketExample wsmsg ->
let
( wsmodel, wscmd ) =
WebSocketExample.update wsmsg model.ws
in
( { model | ws = wsmodel }, Cmd.map WebSocketExample wscmd )
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.map WebSocketExample (WebSocketExample.subscriptions model.ws)
view : Model -> Html Msg
view model =
div []
[ App.map WebSocketExample (WebSocketExample.view model.ws)
]
{- Variation of the WebSocket Example which sends the message multiple times and calls a decode function -}
module WebSocketExample exposing (..)
import Html exposing (..)
import Html.App as Html
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import WebSocket
main : Program Never
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
echoServer : String
echoServer =
"ws://echo.websocket.org"
-- MODEL
type alias Model =
{ input : String
, messages : List String
}
init : ( Model, Cmd Msg )
init =
( Model "" [], Cmd.none )
-- UPDATE
type Msg
= Input String
| Send
| NewMessage String
-- Call a second method to trigger a type error in JS
decode : String -> String
decode msg =
msg ++ " decoded"
update : Msg -> Model -> ( Model, Cmd Msg )
update msg { input, messages } =
case msg of
Input newInput ->
( Model newInput messages, Cmd.none )
Send ->
( Model "" messages
-- Send the message multiple times to trigger the issue
, Cmd.batch
[ WebSocket.send echoServer input
, WebSocket.send echoServer (input ++ "1")
, WebSocket.send echoServer (input ++ "2")
, WebSocket.send echoServer (input ++ "3")
]
)
NewMessage str ->
( Model input ((decode str) :: messages), Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
WebSocket.listen echoServer NewMessage
-- VIEW
view : Model -> Html Msg
view model =
div []
[ input [ onInput Input, value model.input ] []
, button [ onClick Send ] [ text "Send" ]
, div [] (List.map viewMessage (List.reverse model.messages))
]
viewMessage : String -> Html msg
viewMessage msg =
div [] [ text msg ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment