Using Reflex-DOM, I'd like to make an Event t ()
that fires when the browser is ready to paint the next frame, i.e. when requestAnimationFrame
fires. I tried it like this:
{-# LANGUAGE RecursiveDo, TypeFamilies #-}
import Reflex.Dom
import Reflex.Host.Class
import GHCJS.DOM (currentWindow)
import GHCJS.DOM.Window as Window
import GHCJS.DOM.Types (RequestAnimationFrameCallback(..))
import GHCJS.Foreign.Callback
import Control.Monad
import Control.Monad.IO.Class
refresh win = do
(event, ref) <- newEventWithTriggerRef
postGui <- askPostGui
rec cb <- liftIO $ asyncCallback1 $ \_timestamp -> do
scheduleNext
putStrLn "about to fire the event"
postGui $ void $ fireEventRef ref ()
putStrLn "event fired"
let scheduleNext = Window.requestAnimationFrame win $ Just $ RequestAnimationFrameCallback cb
liftIO scheduleNext
return event
My test app is the following:
main :: IO ()
main = mainWidget $ do
Just win <- liftIO currentWindow
tick <- refresh win
display =<< count tick
However, the count doesn't increase. On the browser's JS console, however, I do see both about to fire the event
and event fired
printed repeatedly.