Skip to content

Instantly share code, notes, and snippets.

@kexoth
Last active April 16, 2018 14:41
Show Gist options
  • Save kexoth/9fbbc8eda829871cec43f09610f5bf4f to your computer and use it in GitHub Desktop.
Save kexoth/9fbbc8eda829871cec43f09610f5bf4f to your computer and use it in GitHub Desktop.
This is a wrapped Elm `Html.program` in order to add additional logic around it.
module Main exposing (..)
import Html exposing (Html, text)
type Msg msg
= Internal
| Wrapped msg
type alias WrappedProgram flags model msg =
Program flags (Model model) (Msg msg)
type alias Model model =
{ internal :
String
-- some internal state
, wrapped : model
}
subscriptions : (model -> Sub msg) -> Model model -> Sub (Msg msg)
subscriptions wrappedSubscription { wrapped } =
Sub.batch
[ Sub.map Wrapped (wrappedSubscription wrapped)
, Sub.none
-- rather than "none", this is where you setup extra subscriptions
]
update : (msg -> model -> ( model, Cmd msg )) -> Msg msg -> Model model -> ( Model model, Cmd (Msg msg) )
update wrappedUpdate msg model =
case msg of
Internal ->
-- handle the message
model ! []
Wrapped wrappedMsg ->
-- let the wrapped app handle the message
let
( wrappedModel, fx ) =
wrappedUpdate wrappedMsg model.wrapped
in
( { model | wrapped = wrappedModel }, Cmd.map Wrapped fx )
view : (model -> Html msg) -> Model model -> Html (Msg msg)
view wrappedView { wrapped } =
wrappedView wrapped |> Html.map Wrapped
init : ( model, Cmd msg ) -> ( Model model, Cmd (Msg msg) )
init wrapped =
wrapped
|> Tuple.mapFirst (Model "foo")
|> Tuple.mapSecond (Cmd.map Wrapped)
program :
{ init : ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg )
, view : model -> Html msg
, subscriptions : model -> Sub msg
}
-> WrappedProgram Never model msg
program config =
Html.program
{ init = init config.init
, update = update config.update
, view = view config.view
, subscriptions = subscriptions config.subscriptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment