使用 Reflex-DOM,我想Event t ()
在浏览器准备好绘制下一帧时触发,即触发时requestAnimationFrame
触发。我试过这样:
{-# 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
我的测试应用程序如下:
main :: IO ()
main = mainWidget $ do
Just win <- liftIO currentWindow
tick <- refresh win
display =<< count tick
但是,计数并没有增加。然而,在浏览器的 JS 控制台上,我确实看到了两者about to fire the event
并event fired
重复打印。