Copyright | (C) 2016-2018 David M. Johnson |
---|---|
License | BSD3-style (see the file LICENSE) |
Maintainer | David M. Johnson <djohnson.m@gmail.com> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- data App model action = App {
- model :: model
- update :: action -> model -> Effect action model
- view :: model -> View action
- subs :: [Sub action]
- events :: Map MisoString Bool
- initialAction :: action
- mountPoint :: Maybe MisoString
- logLevel :: LogLevel
- data LogLevel
- data Effect action model
- type Sub action = Sink action -> JSM ()
- type Transition action model = StateT model (Writer [Sub action])
- mapAction :: (actionA -> actionB) -> Transition actionA model r -> Transition actionB model r
- fromTransition :: Transition action model () -> model -> Effect action model
- toTransition :: (model -> Effect action model) -> Transition action model ()
- scheduleIO :: JSM action -> Transition action model ()
- scheduleIO_ :: JSM () -> Transition action model ()
- scheduleIOFor_ :: Foldable f => JSM (f action) -> Transition action model ()
- scheduleSub :: Sub action -> Transition action model ()
Documentation
data App model action Source #
Application entry point
App | |
|
Optional Logging for debugging miso internals (useful to see if prerendering is successful)
data Effect action model Source #
An effect represents the results of an update action.
It consists of the updated model and a list of subscriptions. Each Sub
is
run in a new thread so there is no risk of accidentally blocking the
application.
Instances
Bifunctor Effect Source # | |
Monad (Effect action) Source # | |
Functor (Effect action) Source # | |
Applicative (Effect action) Source # | |
Defined in Miso.Effect pure :: a -> Effect action a Source # (<*>) :: Effect action (a -> b) -> Effect action a -> Effect action b Source # liftA2 :: (a -> b -> c) -> Effect action a -> Effect action b -> Effect action c Source # (*>) :: Effect action a -> Effect action b -> Effect action b Source # (<*) :: Effect action a -> Effect action b -> Effect action a Source # |
The Transition Monad
type Transition action model = StateT model (Writer [Sub action]) Source #
A monad for succinctly expressing model transitions in the update
function.
Transition
is a state monad so it abstracts over manually passing the model
around. It's also a writer monad where the accumulator is a list of scheduled
IO actions. Multiple actions can be scheduled using
Control.Monad.Writer.Class.tell
from the mtl
library and a single action
can be scheduled using scheduleIO
.
Tip: use the Transition
monad in combination with the stateful
lens
operators (all operators ending in "=
"). The following example assumes
the lenses field1
, counter
and field2
are in scope and that the
LambdaCase
language extension is enabled:
myApp = App
{ update = fromTransition
. \case
MyAction1 -> do
field1 .= value1
counter += 1
MyAction2 -> do
field2 %= f
scheduleIO $ do
putStrLn "Hello"
putStrLn "World!"
, ...
}
mapAction :: (actionA -> actionB) -> Transition actionA model r -> Transition actionB model r Source #
Turn a transition that schedules subscriptions that consume
actions of type a
into a transition that schedules subscriptions
that consume actions of type b
using the supplied function of
type a -> b
.
:: Transition action model () | |
-> model -> Effect action model | model |
Convert a Transition
computation to a function that can be given to update
.
:: (model -> Effect action model) | model |
-> Transition action model () |
Convert an update
function to a Transition
computation.
scheduleIO :: JSM action -> Transition action model () Source #
Schedule a single IO action for later execution.
Note that multiple IO action can be scheduled using
Control.Monad.Writer.Class.tell
from the mtl
library.
scheduleIO_ :: JSM () -> Transition action model () Source #
Like scheduleIO
but doesn't cause an action to be dispatched to
the update
function.
This is handy for scheduling IO computations where you don't care about their results or when they complete.
scheduleIOFor_ :: Foldable f => JSM (f action) -> Transition action model () Source #
Like scheduleIO_
but generalized to any instance of Foldable
This is handy for scheduling IO computations that return a Maybe
value
scheduleSub :: Sub action -> Transition action model () Source #
Like scheduleIO
but schedules a subscription which is an IO
computation that has access to a Sink
which can be used to
asynchronously dispatch actions to the update
function.
A use-case is scheduling an IO computation which creates a
3rd-party JS widget which has an associated callback. The callback
can then call the sink to turn events into actions. To do this
without accessing a sink requires going via a
which introduces a leaky-abstraction.Sub
scription