Skip to content

Instantly share code, notes, and snippets.

@schnittchen
Created May 14, 2016 14:37
Show Gist options
  • Save schnittchen/0ee8d1af5d616c6609d5b28e098b8c52 to your computer and use it in GitHub Desktop.
Save schnittchen/0ee8d1af5d616c6609d5b28e098b8c52 to your computer and use it in GitHub Desktop.
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))
@schnittchen
Copy link
Author

I bet this leaks memory b/c old versions of the model are being kept around :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment