Hi guys,
I am trying to implement a router that scales with halogen. Here is the link of the repo https://github.com/Woody88/dev-cheatsheets/tree/routing/src. So my first approach uses output message to push the history state, however, @natefaubion suggested that if I wanted it to scale better an approach like this https://github.com/vladciobanu/purescript-halogen-example/blob/master/src/Control/Monad.purs might be better.
Based on the vladciobanu’s repo, he created a Free Monad (which I am not really familiar with) that allows him to pass use it as the “m” in the halogen components that he creates. With this Monad he can execute a function that he named navigation which will push the new state of the route. In his main he created an Event using purscript-event library which will have a handler that listens to a route change from the component and executes a Query action to actually perform the route change.
Now I was told that I could easily implement it using ReaderT by trying to replicate vladciobanu as much as possible. However, I am not sure how I would execute it in the main because I must initially create a ReaderT which will handle my computation but I dont understand how do I pass it to the halogen and how will I handle event route change when a component executes the navigate function. I still haven’t perfectly grasped Transformer so I think that is why I am having difficult figuring out the approach.
Thanks in advance.
module App.AppMonad where
import Control.Monad.Reader.Trans
import Prelude
import App.Navigation (class NavigateDSL)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Router (Route)
type AppEnv =
{ navigate :: Route -> Effect Unit }
newtype AppM a = AppM (ReaderT AppEnv Aff a)
derive newtype instance functorAppM :: Functor AppM
derive newtype instance applyAppM :: Apply AppM
derive newtype instance applicativeAppM :: Applicative AppM
derive newtype instance bindAppM :: Bind AppM
derive newtype instance monadAppM :: Monad AppM
instance navigateDSLAppM :: NavigateDSL AppM where
navigate route = AppM do
env <- ask
liftEffect $ env.navigate route