{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# LANGUAGE BlockArguments #-}

-- | MVar Transport Instance. Blocks when item hasn't been consumed downstream.

module Control.Churro.Transport.MVar where
    
import Control.Churro.Types
import Control.Churro.Prelude

import Control.Concurrent
import Data.Void

instance Transport MVar where
    data In  MVar a = ChanIn  (MVar a)
    data Out MVar a = ChanOut (MVar a)
    yank :: forall a. Out MVar a -> IO a
yank (ChanOut MVar a
c) = forall a. MVar a -> IO a
takeMVar MVar a
c
    yeet :: forall a. In MVar a -> a -> IO ()
yeet (ChanIn  MVar a
c) = forall a. MVar a -> a -> IO ()
putMVar  MVar a
c
    flex :: forall a. IO (In MVar a, Out MVar a)
flex = do 
        MVar a
c <- forall a. IO (MVar a)
newEmptyMVar
        forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. MVar a -> In MVar a
ChanIn MVar a
c, forall a. MVar a -> Out MVar a
ChanOut MVar a
c)

type ChurroMVar a = Churro a MVar

-- | Convenience function for running a Churro with an MVar Transport.
-- 
runWaitMVar :: ChurroMVar a Void Void -> IO a
runWaitMVar :: forall a. ChurroMVar a Void Void -> IO a
runWaitMVar = forall (t :: * -> *) a. Transport t => Churro a t Void Void -> IO a
runWait

-- | Convenience function for running a Churro into a List with an MVar Transport.
-- 
runWaitListMVar :: ChurroMVar () Void o -> IO [o]
runWaitListMVar :: forall o. ChurroMVar () Void o -> IO [o]
runWaitListMVar = forall (t :: * -> *) a b.
(Transport t, Monoid a) =>
Churro a t Void b -> IO [b]
runWaitList