HeinrichApfelmus / threepenny-gui

GUI framework that uses the web browser as a display.
https://heinrichapfelmus.github.io/threepenny-gui/
Other
437 stars 77 forks source link

Change dom asyncronously/with a delay? — Call buffering #163

Closed Wizek closed 6 years ago

Wizek commented 7 years ago

As an example, I'd like a piece of text visible for some seconds, then change it to some other text.

I've tried two approaches:


import            Control.Concurrent.Async
import qualified  Graphics.UI.Threepenny        as UI
import            Graphics.UI.Threepenny.Core

testGui1 = startGUI defaultConfig $ \w -> do
  e <- UI.h1 # set UI.text "1"
  getBody w #+
    [ UI.span
    , element e
    ]
  liftIO $ threadDelay $ 1000 * 500
  element e # set UI.text "2"
  liftIO $ threadDelay $ 1000 * 500
  return ()

testGui2 = startGUI defaultConfig $ \w -> do
  e <- UI.h1 # set UI.text "1"
  getBody w #+
    [ UI.span
    , element e
    ]
  liftIO $ async $ runUI w $ do
    liftIO $ threadDelay $ 1000 * 500
    element e # set UI.text "2"
  return ()

Am I missing something here? Perhaps there is a third approach I haven't considered? Or is this a bug in threepenny-gui?

HeinrichApfelmus commented 6 years ago

Sorry for taking so long to reply.

The behavior you are seeing is due to "call buffering". See the note in the documentation for runFunction. I should probably mention it in the documentation more prominently.

HeinrichApfelmus commented 6 years ago

I have improved the documentation in commit c9a7077810fbe95ee2a4186afae9d24d0bc10ced . Feel free to reopen the issue if you feel that this is not good enough!