module Miso.Concurrent (
Notify (..)
, newNotify
, EventWriter (..)
, newEventWriter
) where
import Control.Concurrent hiding (readChan)
import Control.Concurrent.BoundedChan
import Control.Monad
data EventWriter action = EventWriter {
writeEvent :: action -> IO ()
, getEvent :: IO action
}
newEventWriter :: IO () -> IO (EventWriter m)
newEventWriter notify' = do
chan <- newBoundedChan 50
pure $ EventWriter (write chan) (readChan chan)
where
write chan event =
void . forkIO $ do
void $ tryWriteChan chan $! event
notify'
data Notify = Notify {
wait :: IO ()
, notify :: IO ()
}
newNotify :: IO Notify
newNotify = do
mvar <- newMVar ()
pure $ Notify
(takeMVar mvar)
(() <$ do tryPutMVar mvar $! ())