Halogen.Store.Monad
- Package
- purescript-halogen-store
- Repository
- thomashoneyman/purescript-halogen-store
#MonadStore Source
class MonadStore :: Type -> Type -> (Type -> Type) -> Constraint
class (MonadEffect m) <= MonadStore a s m | m -> s a where
The MonadStore
class captures monads which implement a stored value,
along with methods to get, update (via an action type, a
), or subscribe
to changes in the stored value.
An instance is provided for StoreT
, which is the standard way to use
the MonadStore
class.
Members
getStore :: m s
updateStore :: a -> m Unit
emitSelected :: forall s'. Selector s s' -> m (Emitter s')
Instances
(MonadEffect m) => MonadStore a s (StoreT a s m)
(MonadStore a s m) => MonadStore a s (HalogenM st act slots out m)
(MonadStore a s m) => MonadStore a s (HookM m)
(MonadStore a s m) => MonadStore a s (ContT r m)
(MonadStore a s m) => MonadStore a s (ExceptT e m)
(MonadStore a s m) => MonadStore a s (IdentityT m)
(MonadStore a s m) => MonadStore a s (MaybeT m)
(MonadStore a s m, Monoid w) => MonadStore a s (RWST r w s m)
(MonadStore a s m) => MonadStore a s (ReaderT r m)
(MonadStore a s m) => MonadStore a s (StateT s m)
(MonadStore a s m, Monoid w) => MonadStore a s (WriterT w m)
#HalogenStore Source
type HalogenStore a s = { emitter :: Emitter s, listener :: Listener s, reducer :: s -> a -> s, value :: Ref s }
#StoreT Source
newtype StoreT :: Type -> Type -> (Type -> Type) -> Type -> Type
newtype StoreT a s m b
The StoreT
monad transformer is the standard way to use the MonadStore
class. It extends the base monad with a global action a
used to update
a global state s
.
The MonadStore
type class describes the operations supported by this monad.
Constructors
StoreT (ReaderT (HalogenStore a s) m b)
Instances
(Functor m) => Functor (StoreT a s m)
(Apply m) => Apply (StoreT a s m)
(Applicative m) => Applicative (StoreT a s m)
(Bind m) => Bind (StoreT a s m)
(Monad m) => Monad (StoreT a s m)
(MonadEffect m) => MonadEffect (StoreT a s m)
(MonadAff m) => MonadAff (StoreT a s m)
(MonadThrow e m) => MonadThrow e (StoreT a s m)
(MonadError e m) => MonadError e (StoreT a s m)
(MonadTell w m) => MonadTell w (StoreT a s m)
(MonadWriter w m) => MonadWriter w (StoreT a s m)
(MonadState s m) => MonadState s (StoreT a s m)
(MonadCont m) => MonadCont (StoreT a s m)
(MonadRec m) => MonadRec (StoreT a s m)
(MonadFork f m) => MonadFork f (StoreT a s m)
(MonadKill e f m) => MonadKill e f (StoreT a s m)
(MonadBracket e f m) => MonadBracket e f (StoreT a s m)
(Distributive g) => Distributive (StoreT a s g)
MonadTrans (StoreT a s)
(MonadAsk r m) => MonadAsk r (StoreT a s m)
(MonadReader r m) => MonadReader r (StoreT a s m)
(MonadEffect m) => MonadStore a s (StoreT a s m)
#runStoreT Source
runStoreT :: forall a s q i o m. Monad m => s -> (s -> a -> s) -> Component q i o (StoreT a s m) -> Aff (Component q i o m)
Run a component in the StoreT
monad.
Requires an initial value for the store, s
, and a reducer that updates
the store in response to an action, a
.
This can be used directly on the root component of your application to
produce a component that Halogen can run, so long as the base monad can
be fixed to Aff
.
main = launchAff_ do
body <- Halogen.Aff.awaitBody
root <- runStoreT initialStore reducer rootComponent
runUI root unit body
#runAndEmitStoreT Source
runAndEmitStoreT :: forall a s q i o m. Monad m => s -> (s -> a -> s) -> Component q i o (StoreT a s m) -> Aff ({ component :: Component q i o m, emitter :: Emitter s })
Run a component in the StoreT
monad.
Requires an initial value for the store, s
, and a reducer that updates
the store in response to an action, a
.
This can be used directly on the root component of your application to
produce a component that Halogen can run, so long as the base monad can
be fixed to Aff
.
Returns a component that can be run with runUI
and an emitter with can
be used to react to store changes. This can be used, for example, to
persist parts of the store to local storage or some other persistence
mechanism, allowing you to push these concerns to the boundaries of the
application, outside of the component.
main = do
-- load initial store values from local storage.
field <- LocalStorage.getItem "field"
let initialStore = mkStore field
launchAff_ do
body <- Halogen.Aff.awaitBody
{ emitter, component } <- runAndEmitStoreT initialStore reducer rootComponent
runUI component unit body
let selectField = selectEq _.field
liftEffect do
-- save new store values to local storage as they change
void $ H.subscribe $ selectEmitter selectField emitter $ LocalStorage.setItem "field"