Last active
May 13, 2016 00:09
-
-
Save szabba/e4da6543a5c0796e77c42bb1d70ff171 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Embedding exposing ( .. ) | |
type alias Embedding part message container = | |
{ wrapOpaque : OpaqueUpdate message container -> message | |
, liftUpdate : (part -> part) -> container -> container | |
} | |
type alias OpaqueUpdate msg model = | |
model -> (model, Cmd msg) | |
updateToMessage | |
: Embedding part msg model | |
-> List (Cmd msg) | |
-> (part -> part) | |
-> msg | |
updateToMessage embedding cmds update = | |
let | |
opaqueUpdate model = | |
(model |> embedding.liftUpdate update) ! cmds | |
in | |
opaqueUpdate |> embedding.wrapOpaque |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing ( .. ) | |
import Html exposing (Html, Attribute) | |
import Html.App as App | |
import Button exposing (Button) | |
import Embedding exposing ( Embedding, OpaqueUpdate ) | |
main : Program Never | |
main = | |
App.program | |
{ init = (init, Cmd.none) | |
, update = update | |
, view = view | |
, subscriptions = always Sub.none | |
} | |
-- MODEL | |
type Counter | |
= Counter | |
{ count : Int | |
, upButton : Button Message Counter | |
, downButton : Button Message Counter | |
} | |
init : Counter | |
init = | |
Counter | |
{ count = 0 | |
, upButton = Button.new Opaque upButtonL | |
, downButton = Button.new Opaque downButtonL | |
} | |
upButtonL f (Counter counter) = | |
Counter { counter | upButton = f counter.upButton } | |
downButtonL f (Counter counter) = | |
Counter { counter | downButton = f counter.downButton } | |
-- UPDATE | |
type Message | |
= Decrement | |
| Increment | |
| Opaque (OpaqueUpdate Message Counter) | |
update : Message -> Counter -> (Counter, Cmd Message) | |
update msg model = | |
let | |
(Counter inner) = model | |
withoutEffects = Counter >> flip (!) [ Cmd.none ] | |
in | |
case msg of | |
Decrement -> | |
{ inner | count = inner.count - 1 } |> withoutEffects | |
Increment -> | |
{ inner | count = inner.count + 1 } |> withoutEffects | |
Opaque f -> | |
model |> f | |
-- VIEW | |
view : Counter -> Html Message | |
view (Counter counter) = | |
Html.div | |
[] | |
[ Button.view counter.downButton | |
[ Button.onClick Decrement ] | |
[ Html.text "-" ] | |
, Html.text <| toString counter.count | |
, Button.view counter.upButton | |
[ Button.onClick Increment ] | |
[ Html.text "+" ] | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Platform.Cmd.Extra exposing ( .. ) | |
import Process | |
import Task | |
import Time exposing ( Time ) | |
wrap : msg -> Cmd msg | |
wrap msg = | |
Task.succeed () | |
|> Task.perform (always msg) (always msg) | |
delay : Time -> a -> Cmd a | |
delay t msg = | |
Process.sleep t | |
|> Task.perform (always msg) (always msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment