module Data.MonadicStreamFunction.ReactHandle where
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import Data.MonadicStreamFunction (MSF)
import Data.MonadicStreamFunction.InternalCore (unMSF)
type ReactHandle m = IORef (MSF m () ())
reactInit :: MonadIO m => MSF m () () -> m (ReactHandle m)
reactInit :: forall (m :: * -> *). MonadIO m => MSF m () () -> m (ReactHandle m)
reactInit = IO (ReactHandle m) -> m (ReactHandle m)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ReactHandle m) -> m (ReactHandle m))
-> (MSF m () () -> IO (ReactHandle m))
-> MSF m () ()
-> m (ReactHandle m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MSF m () () -> IO (ReactHandle m)
forall a. a -> IO (IORef a)
newIORef
react :: MonadIO m => ReactHandle m -> m ()
react :: forall (m :: * -> *). MonadIO m => ReactHandle m -> m ()
react ReactHandle m
handle = do
MSF m () ()
msf <- IO (MSF m () ()) -> m (MSF m () ())
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (MSF m () ()) -> m (MSF m () ()))
-> IO (MSF m () ()) -> m (MSF m () ())
forall a b. (a -> b) -> a -> b
$ ReactHandle m -> IO (MSF m () ())
forall a. IORef a -> IO a
readIORef ReactHandle m
handle
(()
_, MSF m () ()
msf') <- MSF m () () -> () -> m ((), MSF m () ())
forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF m () ()
msf ()
IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ ReactHandle m -> MSF m () () -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef ReactHandle m
handle MSF m () ()
msf'