Reproduces the issues I've been seeing in Elm 0.17.
- elm reactor
- http://localhost:8000/WebSocketExample.elm works (four messages sent and received)
- http://localhost:8000/Main.elm triggers a JS TypeError (looks like the ctor is incorrect)
Reproduces the issues I've been seeing 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 ] |