Skip to content

Instantly share code, notes, and snippets.

@prozacchiwawa
Created February 2, 2016 09:36
Show Gist options
  • Save prozacchiwawa/6ffc6d1da157abf1056d to your computer and use it in GitHub Desktop.
Save prozacchiwawa/6ffc6d1da157abf1056d to your computer and use it in GitHub Desktop.
module PortTest2 where
import Debug
import Signal exposing (Mailbox, mailbox, Address)
import Effects exposing (Effects, none, Never)
import Basics exposing (toString)
import String
import Task
import Html exposing (Html, text, button, div)
import Html.Events exposing (onClick)
{--
This https://groups.google.com/forum/#!topic/elm-discuss/FL1kiu8TrIw
But edited because Effect requires Signal (List a) which this code
didn't do.
Also added my output port to javascript.
HTML
<html>
<head>
<script src='port-test2.js'></script>
</head>
<body>
<div id='port-test'></div>
<script>
var portTest = Elm.embed(Elm.PortTest2, document.getElementById('port-test'));
portTest.ports.outputPort.subscribe(function (f) {
console.log("js-output",f);
});
console.log(portTest.ports);
</script>
</body>
</html>
--}
-- MODEL
type alias Model = Int
-- UPDATE
type alias Action = String
update : Action -> Model -> (Model, Effects Action)
update act model =
case act of
-- Recur: re-send Recur repeatedly without end
"Recur" -> Debug.log "What Goes Here?" (
(model + 1, Signal.send mbox.address ["NoOp"]
|> Effects.task
|> Effects.map (always "Recur")) )
"NoOp" -> ( model, Effects.none )
otherwise -> ( model + 1, Effects.none)
-- VIEW
view : Address (List Action) -> Model -> Html
view addr model =
div [] [
div [] [text (toString model)],
button [onClick addr ["Inc"]] [text "+"],
button [onClick addr ["Recur"]] [text "Recur"]
]
-- INIT
init : (Model, Effects Action)
init = (0, Effects.none)
-- MAIN
mbox : Mailbox (List Action)
mbox = Signal.mailbox ["NoOp"]
updateFromListOfActions m la =
List.foldr (\ a (m, e) -> update a m) la m
modelEffectStream : Signal (Model, Effects Action)
modelEffectStream = Signal.foldp updateFromListOfActions init mbox.signal
port tasks : Signal (Task.Task Never ())
port tasks = modelEffectStream
|> Signal.map (snd >> Effects.toTask mbox.address)
main : Signal Html
main = Signal.map (view mbox.address) (Signal.map fst modelEffectStream)
port outputPort : Signal String
port outputPort = Signal.map (String.join ",") mbox.signal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment