Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using match and matchWith #42

Open
Rizary opened this issue Jul 21, 2017 · 0 comments
Open

Using match and matchWith #42

Rizary opened this issue Jul 21, 2017 · 0 comments

Comments

@Rizary
Copy link

Rizary commented Jul 21, 2017

Hi, I notice some strange behavior when I want to combine purescript-routing and coroutine in purescript-halogen example:

So my main.purs contained this code:

hashChangeProducer
  :: forall eff
   . CR.Producer DOM.HashChangeEvent (Aff (avar :: AVAR, dom :: DOM | eff)) Unit
hashChangeProducer = CRA.produce \emit ->
  let
    emitter e =
      case runExcept (DOM.readHashChangeEvent (toForeign e)) of
        Left _ -> pure unit
        Right hce -> emit (Left hce)
  in
    liftEff $
      DOM.window
        >>= DOM.windowToEventTarget
        >>> DOM.addEventListener ET.hashchange (DOM.eventListener emitter) false

hashChangeConsumer
  :: forall eff
   . (Container.Query ~> Aff (HA.HalogenEffects eff))
  -> CR.Consumer DOM.HashChangeEvent (Aff (HA.HalogenEffects eff)) Unit
hashChangeConsumer query = CR.consumer \event -> do
  let hash = Str.drop 1 $ Str.dropWhile (_ /= '#') $ HCE.newURL event
      -- route = R.matchWith (Str.drop 1 <<< Str.dropWhile (_ /= '#')) Container.routing $ HCE.newURL event
  _ <- query $ H.action $ Container.RouteChange (R.match Container.routing hash)
  pure Nothing

main :: forall eff. Eff (HA.HalogenEffects (api :: Api.API, ref :: REF | eff)) Unit
main = HA.runHalogenAff do
  body <- HA.awaitBody
  packageList <- liftEff (newRef RD.NotAsked)
  matrixClient <- liftEff (Api.newApi "/api" "/api")
  io <- runUI (H.hoist (\x -> runReaderT x { matrixClient, packageList }) Container.ui) unit body
  --T.Tuple old new <- R.matchesAff Container.routing
  --io.query $ H.action $ Container.RouteChange new
  CR.runProcess (hashChangeProducer CR.$$ hashChangeConsumer io.query)

Then in my container.purs (which contain the routing, I have the eval code:

eval (RouteChange str next) = 
      case str of
        (Right pg ) -> do
          _ <- H.modify (_ { route = pg })
          _ <- traceAnyA pg
          pure next
        _ -> do
          _ <- H.modify _ { route = T.ErrorPage}
          _ <- traceAnyA str
          pure next

one thing that i notice is that the routing works if i am using match like in the above code. But, when I use matchWith like the one in route, it doesn't work.

The next problem with purescript-routing that I encounter is when i try to change the URI manually in address bar. It seems it didn't notice the change, so that my page content doesn't change too. It happens also with the makeAff function, which I have to push enter twice.

Is there any suggestion on using purescript-routing with the coroutine?

Here is how i do the routing:
https://github.com/Rizary/hackage-matrix-builder/blob/purescript/purescript-halogen/src/Container.purs

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant