Skip to content

Instantly share code, notes, and snippets.

@prozacchiwawa
Created March 22, 2016 01:28
Show Gist options
  • Save prozacchiwawa/b14e54616034b4f2af89 to your computer and use it in GitHub Desktop.
Save prozacchiwawa/b14e54616034b4f2af89 to your computer and use it in GitHub Desktop.
import Effects exposing (Effects(..), Never)
import Html exposing (Html, div, button, text)
import Html.Attributes exposing (value)
import Html.Events exposing (onClick)
import Signal exposing (Mailbox)
import StartApp as SA
import Task exposing (Task(..))
type Request = DummyRequest | Request String
type Response = Response String
type Action = NoOp | Click | Incoming Response
requestBox : Mailbox Request
requestBox = Signal.mailbox DummyRequest
request : Request -> Effects Action
request r = Effects.task (Signal.send requestBox.address r) |> Effects.map (\_ -> NoOp)
update : Action -> String -> (String, Effects Action)
update action model =
case action of
Click -> (model, request (Request "test"))
Incoming (Response s) -> (s, Effects.none)
_ -> (model, Effects.none)
view : Signal.Address Action -> String -> Html
view address model =
div [] [
text ("last response " ++ model)
, button [value "send request", onClick address Click] []
]
app : SA.App String
app = SA.start {
init = ("", Effects.none)
, update = update
, view = view
, inputs = [Signal.map (\y -> Incoming (Response y)) responsePort]
}
port tasks : Signal (Task Never ())
port tasks = app.tasks
main = app.html
port responsePort : Signal String
port requestPort : Signal (List String)
port requestPort = Signal.map (\r ->
case r of
Request s -> [s]
DummyRequest -> []) requestBox.signal
<html>
<body>
<script src='testelmd.js'></script>
<script>
var testelmd = Elm.fullscreen(Elm.Main, { responsePort: "" });
testelmd.ports.requestPort.subscribe(function (x) {
for (var i = 0; i < x.length; i++) {
var s = x[i];
console.log('request ' + s);
testelmd.ports.responsePort.send("hi " + s);
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment