module Haste.App.Events (
ClientCallback, CB.Event (..),
onEvent, setTimeout, CB.evtName
) where
import qualified Haste.Callback as CB
import Haste.App.Client
import Haste.Concurrent
import Haste.DOM
class ClientCallback a where
type T a
cbify :: ClientState -> a -> T a
instance ClientCallback (Client ()) where
type T (Client ()) = IO ()
cbify cs = concurrent . runClientCIO cs
instance ClientCallback b => ClientCallback (a -> b) where
type T (a -> b) = a -> T b
cbify cs f = \x -> cbify cs (f x)
onEvent :: ClientCallback a => Elem -> CB.Event Client a -> a -> Client ()
onEvent e evt f = do
cs <- get id
_ <- liftIO . CB.jsSetCB e (CB.evtName evt) . CB.mkCallback $! cbify cs f
return ()
setTimeout :: Int -> Client () -> Client ()
setTimeout delay cb = do
cs <- get id
liftIO $ CB.jsSetTimeout delay (CB.mkCallback $! cbify cs cb)