Skip to content

Instantly share code, notes, and snippets.

@prozacchiwawa
Created February 2, 2016 08:47
Show Gist options
  • Save prozacchiwawa/0625666c2a4a231e602f to your computer and use it in GitHub Desktop.
Save prozacchiwawa/0625666c2a4a231e602f to your computer and use it in GitHub Desktop.
Port test where outputPort depends on app.model.
module PortTest where
{--
<html>
<head>
<script src='port-test.js'></script>
</head>
<body>
<div id='port-test'></div>
<script>
var portTest = Elm.embed(Elm.PortTest, document.getElementById('port-test'));
portTest.ports.outputPort.subscribe(function (t) {
console.log("js-output",t);
});
</script>
</body>
</html>
--}
{--
Uncaught TypeError: Cannot read property 'ctor' of undefinedgetValue @ VM153 port-test.js:10475(anonymous function) @ VM153 port-test.js:10478A2 @ VM153 port-test.js:1842(anonymous function) @ VM153 port-test.js:10487(anonymous function) @ VM153 port-test.js:10489A2 @ VM153 port-test.js:1842(anonymous function) @ VM153 port-test.js:10436A2 @ VM153 port-test.js:1842foldl @ VM153 port-test.js:2655A3 @ VM153 port-test.js:1848(anonymous function) @ VM153 port-test.js:10441A2 @ VM153 port-test.js:1842node.notify @ VM153 port-test.js:3496broadcastToKids @ VM153 port-test.js:3294node.notify @ VM153 port-test.js:3317notify @ VM153 port-test.js:1316(anonymous function) @ VM153 port-test.js:3341
2VM153 port-test.js:1307 Uncaught Error: The notify function has been called synchronously!
This can lead to frames being dropped.
Definitely report this to <https://github.com/elm-lang/Elm/issues>
--}
import Debug exposing (log)
import StartApp
import Effects exposing (Effects(..), Never)
import Html exposing (div, text)
import Html.Events exposing (onClick)
import Signal
type Action = Act Int
type Model = ModelX Int | ModelWithOutput { v : Int, o : String }
update : Action -> Model -> Model
update action model =
ModelX ((getValue model) + (case (log "action" action) of Act x -> x))
view : Signal.Address Action -> Model -> Html.Html
view address model =
div [onClick address (Act 1)] [text (toString model)]
getValue : Model -> Int
getValue m =
case m of
ModelX x -> x
ModelWithOutput d -> d.v
app : StartApp.App Model
app = StartApp.start {
init = (ModelX 0, Effects.none)
, update = \a -> \m -> (
let m = update a m in
ModelWithOutput { v = getValue m, o = (toString m) },
Effects.none
)
, view = view
, inputs = []
}
port outputPort : Signal String
port outputPort =
Signal.map getOutput (Signal.filter hasOutput (ModelWithOutput { v = 0, o = "hi there" }) app.model)
main : Signal Html.Html
main =
app.html
hasOutput : Model -> Bool
hasOutput m =
case m of
ModelWithOutput _ -> True
ModelX _ -> False
getOutput : Model -> String
getOutput m =
case m of
ModelWithOutput d -> d.o
ModelX _ -> ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment