Created
May 14, 2016 14:37
-
-
Save schnittchen/0ee8d1af5d616c6609d5b28e098b8c52 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 TwoCounters exposing (init, update, view) | |
import Html exposing (..) | |
import Html.App exposing (..) | |
import Counter exposing (Model, init, update, view) | |
type Slot = Top | Bottom | |
type alias Model = Slot -> Counter.Model | |
initialModel : Model | |
initialModel = \_ -> 0 | |
init : (Model, Cmd Msg) | |
init = | |
( initialModel | |
, Cmd.none | |
) | |
-- UPDATE | |
type alias Msg = (Slot, Counter.Msg) | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
(slot, counterMsg) -> | |
let | |
counterModel = model(slot) | |
in | |
transformUpdate model slot (Counter.update counterMsg counterModel) | |
transformUpdate : Model -> Slot -> (Counter.Model, Cmd Counter.Msg) -> (Model, Cmd Msg) | |
transformUpdate model slot counterUpdate = | |
( | |
\mslot -> | |
if mslot == slot then | |
fst counterUpdate | |
else | |
model mslot | |
, Cmd.map (\cmdMsg -> (slot, cmdMsg)) (snd counterUpdate) | |
) | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ | |
transformView model Top | |
, transformView model Bottom | |
] | |
transformView : Model -> Slot -> (Html Msg) | |
transformView model slot = | |
model(slot) | |
|> Counter.view | |
|> Html.App.map (\counterMsg -> (slot, counterMsg)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I bet this leaks memory b/c old versions of the model are being kept around :)