Skip to content

Instantly share code, notes, and snippets.

@hoichi hoichi/Main.elm
Last active Apr 1, 2017

Embed
What would you like to do?
Nested subscriptions?
module Main exposing (..)
import Config exposing (..)
import Storage exposing (..)
--
import Html exposing (..)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ cfg : Config.Model
, storage : Storage.Model
}
initModel : Model
initModel =
{ cfg = Config.initModel
, storage =
Storage.initModel
-- #todo:0 pass config to js-land
}
init : ( Model, Cmd msg )
init =
( initModel, Cmd.none )
-- UPDATE
type Msg
= Storage Storage.Msg
update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
case msg of
Storage msg ->
Storage.update msg model.storage
|> \( storage, cmd ) ->
{ model | storage = storage } ! [ cmd ]
-- SUBSCRIPTIONS
-- subscriptions : Model -> Sub Msg
-- subscriptions model = ???
-- VIEW
view : Model -> Html Msg
view model =
p []
[ text model.storage.plainText
]
port module Storage exposing (..)
-- MODEL
type alias Model =
{ plainText : String }
initModel : Model
initModel =
Model ""
-- UPDATE
port requestRead : () -> Cmd msg
type Msg
= Read
| Update String
update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
case msg of
Read ->
( model, requestRead () )
Update text ->
( { model | plainText = text }
, Cmd.none
)
-- SUBSCRIPTIONS
-- port for reading plain text content from a single hardcoded file
port onRead : (String -> msg) -> Sub msg
subscriptions : Model -> Sub Msg
subscriptions model =
onRead Update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.