1+ -- | Renderless components use `Control.Comonad.Store` wrapped around
2+ -- | their state type. This allows true renderless components, as the
3+ -- | `extract` function from `Store` serves as the render function.
4+ -- | However, this means that state update functions in the component's
5+ -- | eval function are not actually operating on the component's state
6+ -- | type; they're operating on the `Store` instead. This module contains
7+ -- | helper functions to make this work easy to do.
18module Renderless.State where
29
310import Prelude
@@ -14,25 +21,33 @@ import Data.Tuple (fst, snd)
1421-- | use it as you ordinarily would.
1522-- |
1623-- | ```purescript
24+ -- | -- Old
25+ -- | st <- H.get
26+ -- | -- New
1727-- | st <- getState
1828-- | ```
1929getState :: ∀ m s a . MonadState (Store s a ) m => m s
2030getState = map snd <<< pure <<< runStore =<< get
2131
22- -- | You can also retrieve the render function, if you need to.
32+ -- | You can also retrieve the render function, if you need to,
33+ -- | from within the `Store` comonad.
2334-- |
2435-- | ```purescript
25- -- | render <- getRender
36+ -- | renderFunction <- getRender
2637-- | ```
2738getRender :: ∀ m s a . MonadState (Store s a ) m => m (s -> a )
2839getRender = map fst <<< pure <<< runStore =<< get
2940
30- -- | When you are modifying the state type, you need to again get into
31- -- | `Store` and apply the function there. We can do this with `seeks`
32- -- | from the module. You could use this directly, or write helpers to
41+ -- | When you are modifying the state type, you need to apply a function
42+ -- | (`state -> state`) within the `Store`. We can do this with the `seeks`
43+ -- | function from `Control.Comonad.Store`. You could use this directly, or
44+ -- | write helpers like the ones provided here.
3345-- | make it feel more natural.
3446-- |
3547-- | ```purescript
48+ -- | -- without helpers
49+ -- | H.modify_ $ seeks $ \st -> st { field = newValue }
50+ -- | -- with helpers
3651-- | modifyState_ \st -> st { field = newValue }
3752-- | ```
3853modifyState :: ∀ m s a . MonadState (Store s a ) m => (s -> s ) -> m (Store s a )
@@ -64,7 +79,8 @@ updateStore :: ∀ state html
6479updateStore r f
6580 = store r <<< snd <<< runStore <<< seeks f
6681
67- -- | You could also use these helper functions directly
82+ -- | You can also use these helper functions directly, rather than
83+ -- | pass `updateStore` to `modify`.
6884-- |
6985-- | ```purescript
7086-- | newStore <- modifyStore render stateTransform
0 commit comments