Question about Event firing in reflex-frp

84 Views Asked by At

This is the first Haskell and reflex-frp project I'm working on and I've put together what I have from a bunch of examples and projects I found on github. I am basically trying to log three events to the console, but only two seem to be firing and I don't understand why.

I have a route function (this function is really longer with a bunch of other definitions for other routes, but focusing on this one).

route :: R FrontendRoute -> State -> FTNet t m (Event t State)
route (FrontendRoute_Logout :/ ()) (StateLoggedIn _) = do
    pb <- getPostBuild
    el "h1" $ text "logging out..."
    setRoute $ FrontendRoute_Login :/ () <$ traceEvent "logA" pb
    setRoute $ FrontendRoute_Main :/ () <$ traceEvent "logB" pb
    pure $ StateLoggedOut <$ traceEvent "logC" pb

and some other things that may be relevant

data State = StateLoggedOut | StateLoggedIn User deriving stock (Eq, Show)

type FTNet t m = RequesterT t AuthApi (Either Text) m

data AuthApi a where
   …

I get this output in the console

logB: ()
logA: ()

I'm confused why logC isn't printed, also confused how the order of output gets decided, why does logB get printed before logA?

1

There are 1 best solutions below

0
On BEST ANSWER

Through help on https://web.libera.chat/#reflex-frp this can be marked solved.

I was using the result of the route function in a switchHold like this

    initState :: FTNet t m ()
    initState = do
      rec dyState <- holdDyn StateLoggedOut stateUpdatedEv
          dyRoute <- lift askRoute
          el "div" $ dynText (T.pack . show <$> dyState) -- temporary for debugging
          stateUpdatedEv <- switchHold never =<< dyn (routeWithMenu <$> dyRoute <*> dyState)
      pure ()
    routeWithMenu :: R FrontendRoute -> State -> FTNet t m (Event t State)
    routeWithMenu r s = do
      lift $ el "div" $ menu r s
      route r s

but I needed to be using switchHoldPromptly instead