Skip to content

Instantly share code, notes, and snippets.

@hoichi
Last active April 1, 2017 05:20
Show Gist options
  • Save hoichi/a1d747aa16957eb000431be9581c300c to your computer and use it in GitHub Desktop.
Save hoichi/a1d747aa16957eb000431be9581c300c to your computer and use it in GitHub Desktop.
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