I was am using reflex-dom on the frontend and quite liking it, and so I find myself on the backend working with websockets and I'd like to use plain old reflex to coordinate this stuff. But there's no real documentation on how to wire up arbitrary IO into event triggers, all examples use reflex-dom and, and reflex-dom itself is too complicated for me to understand.
The only source I can find that has a simple example is reflex-gloss. I'm trying to set up a simple timer event but I can't seem to get the types to be what I think they should be, or what reflex-gloss managed, so I don't think I'm using it correctly.
http://hackage.haskell.org/package/reflex-gloss-0.2/docs/src/Reflex-Gloss.html#GlossAppI should mention I'm testing with reflex on the HEAD of the develop branch.
import Control.Monad.Identity
import Data.IORef
import Data.Dependent.Sum (DSum ((:=>)))
import Control.Monad.Trans
import Control.Concurrent (threadDelay)
import Reflex
import Reflex.Class
import Reflex.Host.Class
import Reflex.Spider.Internal (SpiderHostFrame)
-- I don't know what this type should be be, but I was imaginging something like this:
-- test :: (Reflex t, MonadIO m) => (Event t () -> m ()) -> IO ()
test network = do
runSpiderHost $ do
(tickEvent, tickTriggerRef) <- newEventWithTriggerRef
runHostFrame $ network tickEvent
liftIO $ tick (runSpiderHost $ handleTrigger tickTriggerRef)
where
handleTrigger trigger = do
mETrigger <- liftIO $ readIORef trigger
case mETrigger of
Nothing -> return ()
Just eTrigger -> fireEvents [eTrigger :=> Identity ()]
tick :: MonadIO m => m () -> m ()
tick f = f >> liftIO (threadDelay 2000000) >> tick f
The actual type of test is
(Reflex.Class.Event Reflex.Spider.Internal.Spider ()
-> Reflex.Spider.Internal.SpiderHostFrame a)
-> IO ()
which doesn't seem like what I wanted.