-- |
-- Copyright  : (c) Ivan Perez and Manuel Baerenz, 2016
-- License    : BSD3
-- Maintainer : ivan.perez@keera.co.uk
--
-- 'ReactHandle's.
--
-- Sometimes it is beneficial to give control to an external main loop, for
-- example OpenGL or a hardware-clocked audio server like JACK. This module
-- makes Dunai compatible with external main loops.
module Data.MonadicStreamFunction.ReactHandle where

-- External imports
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.IORef             (IORef, newIORef, readIORef, writeIORef)

-- Internal imports
import Data.MonadicStreamFunction              (MSF)
import Data.MonadicStreamFunction.InternalCore (unMSF)

-- | A storage for the current state of an 'MSF'. The 'MSF' may not require
-- input or produce output data, all such data must be handled through side
-- effects (such as wormholes).
type ReactHandle m = IORef (MSF m () ())

-- | Needs to be called before the external main loop is dispatched.
reactInit :: MonadIO m => MSF m () () -> m (ReactHandle m)
reactInit :: forall (m :: * -> *). MonadIO m => MSF m () () -> m (ReactHandle m)
reactInit = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> IO (IORef a)
newIORef

-- | The callback that needs to be called by the external loop at every cycle.
react :: MonadIO m => ReactHandle m -> m ()
react :: forall (m :: * -> *). MonadIO m => ReactHandle m -> m ()
react ReactHandle m
handle = do
  MSF m () ()
msf <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef ReactHandle m
handle
  (()
_, MSF m () ()
msf') <- forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF m () ()
msf ()
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> a -> IO ()
writeIORef ReactHandle m
handle MSF m () ()
msf'