purescript-halogen icon indicating copy to clipboard operation
purescript-halogen copied to clipboard

Repeatedly run Halogen app from same REPL?

Open karljs opened this issue 8 years ago • 1 comments

I wrote a question on Stack Overflow about this. I will copy it here as well in hopes of attracting some additional attention.

I am working on a small Purescript application that produces visualizations. My architecture is a bit non-traditional, however, and I'm having some issues. Here's what I want:

  1. Launch pulp psci -- --port 8080 and connect with a browser
  2. Call a function go which accepts a definition of the visualization and renders it along with some controls
  3. See the results. Go back to the REPL and possibly create some new definitions.
  4. Go to step 2 and draw the new visualization with go.

This sort of works with what I have so far, but my problem is that repeated calls to go do not replace the contents of the DOM, but instead are added to them. This means that after a few calls I end up having many copies of all the controls and everything.

I realize this is a bit of a hack, but the only other way I can think to provide this kind of interactive interface is to implement an editor/parser and that's too much work Is there a way to avoid this?

Here's my definition of go, which I imagine needs changing:

go :: Vis -> Eff (HA.HalogenEffects ()) Unit
go vis = HA.runHalogenAff do
  body <- HA.awaitBody
  runUI checks vis body

karljs avatar Dec 21 '17 20:12 karljs

You probably need to catch runUI checks vis body, e.g. create ref in

-- somewhere on callsite 
halogenIORef <- newRef Nothing
-- now we define this
go :: Viz -> Eff _ Unit
go viz r = HA.runHalogenAff do 
  io <- readRef halogenIORef
  case io of 
    Nothing -> do 
      body <- HA.awaitBody 
      io' <- runUI checks viz body
      writeRef halogenIORef $ Just io'
    Just io' -> do 
      io.query $ H.action $ MyAction

where MyAction somehow handled and change state.

It might be something like

go :: Viz -> Ref (Maybe _) -> Eff _ Unit

then you need to somehow catch that ref defined in psci. Sorry, I didn't work with psci at all and have no idea how to do it

Please take a look at this too https://github.com/slamdata/purescript-halogen/tree/6420fa5ed11d9bfe09afa896032027618411db30/examples/driver-io

cryogenian avatar Dec 22 '17 12:12 cryogenian