Module

Halogen.Store.Monad

Package
purescript-halogen-store
Repository
thomashoneyman/purescript-halogen-store

#MonadStore Source

class MonadStore :: Type -> Type -> (Type -> Type) -> Constraintclass (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

Instances

#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 -> Typenewtype 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

Instances

#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"

#mapStoreT Source

mapStoreT :: forall a s m1 m2 b c. (m1 b -> m2 c) -> StoreT a s m1 b -> StoreT a s m2 c

Change the type of the result in a StoreT monad.