miso-0.16.0.0: A tasty Haskell front-end framework

Copyright(C) 2016-2018 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <djohnson.m@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Types

Contents

Description

 

Synopsis

Documentation

data App model action Source

Application entry point

Constructors

App 

Fields

model :: model

initial model

update :: action -> model -> Effect action model

Function to update model, optionally provide effects. See the Transition monad for succinctly expressing model transitions.

view :: model -> View action

Function to draw View

subs :: [Sub action]

List of subscriptions to run during application lifetime

events :: Map MisoString Bool

List of delegated events that the body element will listen for

initialAction :: action

Initial action that is run after the application has loaded

mountPoint :: Maybe MisoString

root element for DOM diff

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!"
  , ...
  }

fromTransition Source

Arguments

:: Transition action model () 
-> model -> Effect action model

model update function.

Convert a Transition computation to a function that can be given to update.

toTransition Source

Arguments

:: (model -> Effect action model)

model update function

-> Transition action model () 

Convert an update function to a Transition computation.

scheduleIO :: IO 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.

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 Subscription which introduces a leaky-abstraction.