module Mpv.Interpreter.Ipc where

import Data.Aeson (Value)
import qualified Data.Map.Strict as Map
import Data.Some (Some)
import Exon (exon)
import qualified Polysemy.Conc as Race
import qualified Polysemy.Conc as Events
import Polysemy.Conc (ChanConsumer, interpretEventsChan, withAsync)
import Polysemy.Conc.Interpreter.Queue.TBM (interpretQueueTBMWith)
import Polysemy.Conc.Interpreter.Scoped (runScoped)
import qualified Polysemy.Conc.Queue as Queue
import Polysemy.Internal.Tactics (liftT)
import qualified Polysemy.Log as Log
import Polysemy.Time (Seconds (Seconds))

import Mpv.Data.Command (Command)
import Mpv.Data.Event (Event)
import Mpv.Data.EventName (EventName, eventNameText)
import Mpv.Data.MpvError (MpvError (MpvError))
import Mpv.Data.MpvEvent (MpvEvent (MpvEvent))
import Mpv.Data.MpvProcessConfig (MpvProcessConfig)
import qualified Mpv.Data.MpvResources as MpvResources
import Mpv.Data.MpvResources (MpvResources (MpvResources), OutMessage (OutMessage), Requests (Requests))
import Mpv.Data.RequestId (RequestId)
import Mpv.Data.Response (ResponseError (ResponseError))
import qualified Mpv.Effect.Commands as Commands
import Mpv.Effect.Commands (Commands)
import qualified Mpv.Effect.Ipc as Ipc
import Mpv.Effect.Ipc (Ipc)
import Mpv.Interpreter.Commands (interpretCommandsJson)
import Mpv.MpvResources (withMpvResources)

createRequest ::
  Members [AtomicState (Requests fmt), Embed IO] r =>
  Sem r (RequestId, MVar (Either ResponseError fmt))
createRequest :: forall fmt (r :: [(* -> *) -> * -> *]).
Members '[AtomicState (Requests fmt), Embed IO] r =>
Sem r (RequestId, MVar (Either ResponseError fmt))
createRequest = do
  MVar (Either ResponseError fmt)
mv <- IO (MVar (Either ResponseError fmt))
-> Sem r (MVar (Either ResponseError fmt))
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
embed IO (MVar (Either ResponseError fmt))
forall a. IO (MVar a)
newEmptyMVar
  RequestId
i <- (Requests fmt -> (Requests fmt, RequestId)) -> Sem r RequestId
forall s a (r :: [(* -> *) -> * -> *]).
Member (AtomicState s) r =>
(s -> (s, a)) -> Sem r a
atomicState' \ (Requests RequestId
n Map RequestId (MVar (Either ResponseError fmt))
p) -> (RequestId
-> Map RequestId (MVar (Either ResponseError fmt)) -> Requests fmt
forall fmt.
RequestId
-> Map RequestId (MVar (Either ResponseError fmt)) -> Requests fmt
Requests (RequestId
n RequestId -> RequestId -> RequestId
forall a. Num a => a -> a -> a
+ RequestId
1) (RequestId
-> MVar (Either ResponseError fmt)
-> Map RequestId (MVar (Either ResponseError fmt))
-> Map RequestId (MVar (Either ResponseError fmt))
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert RequestId
n MVar (Either ResponseError fmt)
mv Map RequestId (MVar (Either ResponseError fmt))
p), RequestId
n)
  pure (RequestId
i, MVar (Either ResponseError fmt)
mv)

sendRequest ::
  Members [Commands fmt command, AtomicState (Requests fmt)] r =>
  Members [Queue (OutMessage fmt) !! MpvError, Stop MpvError, Race, Embed IO] r =>
  command a ->
  Sem r (MVar (Either ResponseError fmt))
sendRequest :: forall fmt (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
(Members '[Commands fmt command, AtomicState (Requests fmt)] r,
 Members
   '[Queue (OutMessage fmt) !! MpvError, Stop MpvError, Race,
     Embed IO]
   r) =>
command a -> Sem r (MVar (Either ResponseError fmt))
sendRequest command a
cmd = do
  (RequestId
requestId, MVar (Either ResponseError fmt)
result) <- Sem r (RequestId, MVar (Either ResponseError fmt))
forall fmt (r :: [(* -> *) -> * -> *]).
Members '[AtomicState (Requests fmt), Embed IO] r =>
Sem r (RequestId, MVar (Either ResponseError fmt))
createRequest
  fmt
msg <- RequestId -> Bool -> command a -> Sem r fmt
forall fmt (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
MemberWithError (Commands fmt command) r =>
RequestId -> Bool -> command a -> Sem r fmt
Commands.encode RequestId
requestId Bool
False command a
cmd
  MVar (Either ResponseError fmt)
result MVar (Either ResponseError fmt)
-> Sem r () -> Sem r (MVar (Either ResponseError fmt))
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Sem (Queue (OutMessage fmt) : r) () -> Sem r ()
forall err (eff :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]).
Members '[Resumable err eff, Stop err] r =>
InterpreterFor eff r
restop (OutMessage fmt -> Sem (Queue (OutMessage fmt) : r) ()
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Queue d) r =>
d -> Sem r ()
Queue.write (fmt -> OutMessage fmt
forall fmt. fmt -> OutMessage fmt
OutMessage fmt
msg))

syncRequest ::
  Members [Commands fmt command, AtomicState (Requests fmt)] r =>
  Members [Queue (OutMessage fmt) !! MpvError, Stop MpvError, Race, Embed IO] r =>
  command a ->
  Sem r a
syncRequest :: forall fmt (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
(Members '[Commands fmt command, AtomicState (Requests fmt)] r,
 Members
   '[Queue (OutMessage fmt) !! MpvError, Stop MpvError, Race,
     Embed IO]
   r) =>
command a -> Sem r a
syncRequest command a
cmd = do
  MVar (Either ResponseError fmt)
result <- command a -> Sem r (MVar (Either ResponseError fmt))
forall fmt (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
(Members '[Commands fmt command, AtomicState (Requests fmt)] r,
 Members
   '[Queue (OutMessage fmt) !! MpvError, Stop MpvError, Race,
     Embed IO]
   r) =>
command a -> Sem r (MVar (Either ResponseError fmt))
sendRequest command a
cmd
  Either ResponseError fmt
response <- Sem r (Either ResponseError fmt)
-> Seconds
-> Sem r (Either ResponseError fmt)
-> Sem r (Either ResponseError fmt)
forall u (r :: [(* -> *) -> * -> *]) a.
(TimeUnit u, Member Race r) =>
Sem r a -> u -> Sem r a -> Sem r a
Race.timeout_ (Either ResponseError fmt -> Sem r (Either ResponseError fmt)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ResponseError -> Either ResponseError fmt
forall a b. a -> Either a b
Left ResponseError
"mpv request timed out")) (Int64 -> Seconds
Seconds Int64
3) (IO (Either ResponseError fmt) -> Sem r (Either ResponseError fmt)
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
embed (MVar (Either ResponseError fmt) -> IO (Either ResponseError fmt)
forall a. MVar a -> IO a
takeMVar MVar (Either ResponseError fmt)
result))
  fmt
fmt <- (ResponseError -> MpvError)
-> Either ResponseError fmt -> Sem r fmt
forall err' (r :: [(* -> *) -> * -> *]) err a.
Member (Stop err') r =>
(err -> err') -> Either err a -> Sem r a
stopEitherWith (Text -> MpvError
MpvError (Text -> MpvError)
-> (ResponseError -> Text) -> ResponseError -> MpvError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ResponseError -> Text
coerce) Either ResponseError fmt
response
  (ResponseError -> MpvError) -> Either ResponseError a -> Sem r a
forall err' (r :: [(* -> *) -> * -> *]) err a.
Member (Stop err') r =>
(err -> err') -> Either err a -> Sem r a
stopEitherWith (Text -> MpvError
MpvError (Text -> MpvError)
-> (ResponseError -> Text) -> ResponseError -> MpvError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ResponseError -> Text
coerce) (Either ResponseError a -> Sem r a)
-> Sem r (Either ResponseError a) -> Sem r a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< command a -> fmt -> Sem r (Either ResponseError a)
forall fmt (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
MemberWithError (Commands fmt command) r =>
command a -> fmt -> Sem r (Either ResponseError a)
Commands.decode command a
cmd fmt
fmt

waitEvent ::
  Member (EventConsumer token MpvEvent) r =>
  EventName ->
  Sem r (Some Event)
waitEvent :: forall token (r :: [(* -> *) -> * -> *]).
Member (EventConsumer token MpvEvent) r =>
EventName -> Sem r (Some Event)
waitEvent EventName
target =
  Sem (Consume MpvEvent : r) (Some Event) -> Sem r (Some Event)
forall e resource (r :: [(* -> *) -> * -> *]).
Member (Scoped (EventResource resource) (Consume e)) r =>
InterpreterFor (Consume e) r
Events.subscribe Sem (Consume MpvEvent : r) (Some Event)
spin
  where
    spin :: Sem (Consume MpvEvent : r) (Some Event)
spin =
      Sem (Consume MpvEvent : r) MpvEvent
forall e (r :: [(* -> *) -> * -> *]).
Member (Consume e) r =>
Sem r e
Events.consume Sem (Consume MpvEvent : r) MpvEvent
-> (MpvEvent -> Sem (Consume MpvEvent : r) (Some Event))
-> Sem (Consume MpvEvent : r) (Some Event)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ (MpvEvent EventName
name Some Event
payload) ->
        if (EventName
target EventName -> EventName -> Bool
forall a. Eq a => a -> a -> Bool
== EventName
name) then Some Event -> Sem (Consume MpvEvent : r) (Some Event)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Some Event
payload else Sem (Consume MpvEvent : r) (Some Event)
spin

waitEventAndRun ::
  TimeUnit u =>
  Members [EventConsumer token MpvEvent, Log, Resource, Async, Race] r =>
  EventName ->
  u ->
  Sem r a ->
  Sem r (Maybe (Some Event), a)
waitEventAndRun :: forall u token (r :: [(* -> *) -> * -> *]) a.
(TimeUnit u,
 Members
   '[EventConsumer token MpvEvent, Log, Resource, Async, Race] r) =>
EventName -> u -> Sem r a -> Sem r (Maybe (Some Event), a)
waitEventAndRun EventName
name u
interval Sem r a
ma =
  Sem r (Some Event)
-> (Async (Maybe (Some Event)) -> Sem r (Maybe (Some Event), a))
-> Sem r (Maybe (Some Event), a)
forall (r :: [(* -> *) -> * -> *]) b a.
Members '[Resource, Race, Async] r =>
Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
withAsync (EventName -> Sem r (Some Event)
forall token (r :: [(* -> *) -> * -> *]).
Member (EventConsumer token MpvEvent) r =>
EventName -> Sem r (Some Event)
waitEvent EventName
name) \ Async (Maybe (Some Event))
handle -> do
    a
res <- Sem r a
ma
    Maybe (Some Event)
found <- Sem r (Maybe (Some Event))
-> u -> Sem r (Maybe (Some Event)) -> Sem r (Maybe (Some Event))
forall u (r :: [(* -> *) -> * -> *]) a.
(TimeUnit u, Member Race r) =>
Sem r a -> u -> Sem r a -> Sem r a
Race.timeout_ (Maybe (Some Event) -> Sem r (Maybe (Some Event))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Some Event)
forall a. Maybe a
Nothing) u
interval do
      Async (Maybe (Some Event)) -> Sem r (Maybe (Some Event))
forall (r :: [(* -> *) -> * -> *]) a.
MemberWithError Async r =>
Async a -> Sem r a
await Async (Maybe (Some Event))
handle
    Bool -> Sem r () -> Sem r ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe (Some Event) -> Bool
forall a. Maybe a -> Bool
isNothing Maybe (Some Event)
found) do
      Text -> Sem r ()
forall (r :: [(* -> *) -> * -> *]).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
Log.warn [exon|waiting for mpv event #{eventNameText name} failed|]
    pure (Maybe (Some Event)
found, a
res)

interpretIpcWithQueue ::
  Members [Commands fmt command, EventConsumer token MpvEvent] r =>
  Members [Queue (OutMessage fmt) !! MpvError, AtomicState (Requests fmt), Log, Resource, Async, Race, Embed IO] r =>
  InterpreterFor (Ipc fmt command !! MpvError) r
interpretIpcWithQueue :: forall fmt (command :: * -> *) token (r :: [(* -> *) -> * -> *]).
(Members '[Commands fmt command, EventConsumer token MpvEvent] r,
 Members
   '[Queue (OutMessage fmt) !! MpvError, AtomicState (Requests fmt),
     Log, Resource, Async, Race, Embed IO]
   r) =>
InterpreterFor (Ipc fmt command !! MpvError) r
interpretIpcWithQueue =
  (forall x (r0 :: [(* -> *) -> * -> *]).
 Ipc fmt command (Sem r0) x
 -> Tactical
      (Resumable MpvError (Ipc fmt command))
      (Sem r0)
      (Stop MpvError : r)
      x)
-> InterpreterFor (Resumable MpvError (Ipc fmt command)) r
forall err (eff :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]).
(forall x (r0 :: [(* -> *) -> * -> *]).
 eff (Sem r0) x
 -> Tactical (Resumable err eff) (Sem r0) (Stop err : r) x)
-> InterpreterFor (Resumable err eff) r
interpretResumableH \case
    Ipc.Sync command x
cmd -> do
      Sem (Stop MpvError : r) x
-> Sem
     (WithTactics
        (Resumable MpvError (Ipc fmt command))
        f
        (Sem r0)
        (Stop MpvError : r))
     (f x)
forall (m :: * -> *) (f :: * -> *) (r :: [(* -> *) -> * -> *])
       (e :: (* -> *) -> * -> *) a.
Functor f =>
Sem r a -> Sem (WithTactics e f m r) (f a)
liftT (command x -> Sem (Stop MpvError : r) x
forall fmt (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
(Members '[Commands fmt command, AtomicState (Requests fmt)] r,
 Members
   '[Queue (OutMessage fmt) !! MpvError, Stop MpvError, Race,
     Embed IO]
   r) =>
command a -> Sem r a
syncRequest command x
cmd)
    Ipc.WaitEvent EventName
name u
interval Sem r0 a1
ma -> do
      (Maybe (Some Event)
found, f a1
res) <- EventName
-> u
-> Sem
     (WithTactics
        (Resumable MpvError (Ipc fmt command))
        f
        (Sem r0)
        (Stop MpvError : r))
     (f a1)
-> Sem
     (WithTactics
        (Resumable MpvError (Ipc fmt command))
        f
        (Sem r0)
        (Stop MpvError : r))
     (Maybe (Some Event), f a1)
forall u token (r :: [(* -> *) -> * -> *]) a.
(TimeUnit u,
 Members
   '[EventConsumer token MpvEvent, Log, Resource, Async, Race] r) =>
EventName -> u -> Sem r a -> Sem r (Maybe (Some Event), a)
waitEventAndRun EventName
name u
interval (Sem r0 a1
-> Tactical
     (Resumable MpvError (Ipc fmt command))
     (Sem r0)
     (Stop MpvError : r)
     a1
forall (m :: * -> *) a (e :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]).
m a -> Tactical e m r a
runTSimple Sem r0 a1
ma)
      f (Maybe (Some Event), a1)
-> Sem
     (WithTactics
        (Resumable MpvError (Ipc fmt command))
        f
        (Sem r0)
        (Stop MpvError : r))
     (f (Maybe (Some Event), a1))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Maybe (Some Event)
found,) (a1 -> (Maybe (Some Event), a1))
-> f a1 -> f (Maybe (Some Event), a1)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a1
res)

interpretIpc ::
  Members [Commands fmt command, EventConsumer token MpvEvent] r =>
  Members [Log, Resource, Async, Race, Embed IO] r =>
  MpvResources fmt ->
  InterpreterFor (Ipc fmt command !! MpvError) r
interpretIpc :: forall fmt (command :: * -> *) token (r :: [(* -> *) -> * -> *]).
(Members '[Commands fmt command, EventConsumer token MpvEvent] r,
 Members '[Log, Resource, Async, Race, Embed IO] r) =>
MpvResources fmt -> InterpreterFor (Ipc fmt command !! MpvError) r
interpretIpc MpvResources{TVar (Requests fmt)
$sel:requests:MpvResources :: forall fmt. MpvResources fmt -> TVar (Requests fmt)
requests :: TVar (Requests fmt)
requests, TBMQueue (OutMessage fmt)
$sel:outQueue:MpvResources :: forall fmt. MpvResources fmt -> TBMQueue (OutMessage fmt)
outQueue :: TBMQueue (OutMessage fmt)
outQueue} =
  TVar (Requests fmt)
-> Sem (AtomicState (Requests fmt) : r) a -> Sem r a
forall (r :: [(* -> *) -> * -> *]) s a.
Member (Embed IO) r =>
TVar s -> Sem (AtomicState s : r) a -> Sem r a
runAtomicStateTVar TVar (Requests fmt)
requests (Sem (AtomicState (Requests fmt) : r) a -> Sem r a)
-> (Sem ((Ipc fmt command !! MpvError) : r) a
    -> Sem (AtomicState (Requests fmt) : r) a)
-> Sem ((Ipc fmt command !! MpvError) : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  InterpreterFor
  (Queue (OutMessage fmt))
  (Stop MpvError : AtomicState (Requests fmt) : r)
-> InterpreterFor
     (Resumable MpvError (Queue (OutMessage fmt)))
     (AtomicState (Requests fmt) : r)
forall err (eff :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]).
InterpreterFor eff (Stop err : r)
-> InterpreterFor (Resumable err eff) r
resumable (TBMQueue (OutMessage fmt)
-> InterpreterFor
     (Queue (OutMessage fmt))
     (Stop MpvError : AtomicState (Requests fmt) : r)
forall d (r :: [(* -> *) -> * -> *]).
Members '[Race, Embed IO] r =>
TBMQueue d -> InterpreterFor (Queue d) r
interpretQueueTBMWith TBMQueue (OutMessage fmt)
outQueue) (Sem
   (Resumable MpvError (Queue (OutMessage fmt))
      : AtomicState (Requests fmt) : r)
   a
 -> Sem (AtomicState (Requests fmt) : r) a)
-> (Sem ((Ipc fmt command !! MpvError) : r) a
    -> Sem
         (Resumable MpvError (Queue (OutMessage fmt))
            : AtomicState (Requests fmt) : r)
         a)
-> Sem ((Ipc fmt command !! MpvError) : r) a
-> Sem (AtomicState (Requests fmt) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  ((Ipc fmt command !! MpvError)
     : Resumable MpvError (Queue (OutMessage fmt))
     : AtomicState (Requests fmt) : r)
  a
-> Sem
     (Resumable MpvError (Queue (OutMessage fmt))
        : AtomicState (Requests fmt) : r)
     a
forall fmt (command :: * -> *) token (r :: [(* -> *) -> * -> *]).
(Members '[Commands fmt command, EventConsumer token MpvEvent] r,
 Members
   '[Queue (OutMessage fmt) !! MpvError, AtomicState (Requests fmt),
     Log, Resource, Async, Race, Embed IO]
   r) =>
InterpreterFor (Ipc fmt command !! MpvError) r
interpretIpcWithQueue (Sem
   ((Ipc fmt command !! MpvError)
      : Resumable MpvError (Queue (OutMessage fmt))
      : AtomicState (Requests fmt) : r)
   a
 -> Sem
      (Resumable MpvError (Queue (OutMessage fmt))
         : AtomicState (Requests fmt) : r)
      a)
-> (Sem ((Ipc fmt command !! MpvError) : r) a
    -> Sem
         ((Ipc fmt command !! MpvError)
            : Resumable MpvError (Queue (OutMessage fmt))
            : AtomicState (Requests fmt) : r)
         a)
-> Sem ((Ipc fmt command !! MpvError) : r) a
-> Sem
     (Resumable MpvError (Queue (OutMessage fmt))
        : AtomicState (Requests fmt) : r)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem ((Ipc fmt command !! MpvError) : r) a
-> Sem
     ((Ipc fmt command !! MpvError)
        : Resumable MpvError (Queue (OutMessage fmt))
        : AtomicState (Requests fmt) : r)
     a
forall (e2 :: (* -> *) -> * -> *) (e3 :: (* -> *) -> * -> *)
       (e1 :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : e3 : r) a
raiseUnder2

interpretIpcResources ::
  Members [EventConsumer token MpvEvent, Resource, Async, Race, Log, Embed IO, Final IO] r =>
  Either MpvError (MpvResources Value) ->
  InterpreterFor (Ipc Value Command !! MpvError) r
interpretIpcResources :: forall token (r :: [(* -> *) -> * -> *]).
Members
  '[EventConsumer token MpvEvent, Resource, Async, Race, Log,
    Embed IO, Final IO]
  r =>
Either MpvError (MpvResources Value)
-> InterpreterFor (Ipc Value Command !! MpvError) r
interpretIpcResources = \case
  Right MpvResources Value
res ->
    Sem (Commands Value Command : r) a -> Sem r a
forall (r :: [(* -> *) -> * -> *]).
InterpreterFor (Commands Value Command) r
interpretCommandsJson (Sem (Commands Value Command : r) a -> Sem r a)
-> (Sem ((Ipc Value Command !! MpvError) : r) a
    -> Sem (Commands Value Command : r) a)
-> Sem ((Ipc Value Command !! MpvError) : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MpvResources Value
-> InterpreterFor
     (Ipc Value Command !! MpvError) (Commands Value Command : r)
forall fmt (command :: * -> *) token (r :: [(* -> *) -> * -> *]).
(Members '[Commands fmt command, EventConsumer token MpvEvent] r,
 Members '[Log, Resource, Async, Race, Embed IO] r) =>
MpvResources fmt -> InterpreterFor (Ipc fmt command !! MpvError) r
interpretIpc MpvResources Value
res (Sem
   ((Ipc Value Command !! MpvError) : Commands Value Command : r) a
 -> Sem (Commands Value Command : r) a)
-> (Sem ((Ipc Value Command !! MpvError) : r) a
    -> Sem
         ((Ipc Value Command !! MpvError) : Commands Value Command : r) a)
-> Sem ((Ipc Value Command !! MpvError) : r) a
-> Sem (Commands Value Command : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem ((Ipc Value Command !! MpvError) : r) a
-> Sem
     ((Ipc Value Command !! MpvError) : Commands Value Command : r) a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder
  Left MpvError
err ->
    (forall x (r0 :: [(* -> *) -> * -> *]).
 Ipc Value Command (Sem r0) x
 -> Tactical
      (Ipc Value Command !! MpvError) (Sem r0) (Stop MpvError : r) x)
-> forall {a}.
   Sem ((Ipc Value Command !! MpvError) : r) a -> Sem r a
forall err (eff :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]).
(forall x (r0 :: [(* -> *) -> * -> *]).
 eff (Sem r0) x
 -> Tactical (Resumable err eff) (Sem r0) (Stop err : r) x)
-> InterpreterFor (Resumable err eff) r
interpretResumableH \ Ipc Value Command (Sem r0) x
_ -> MpvError
-> Sem
     (WithTactics
        (Ipc Value Command !! MpvError) f (Sem r0) (Stop MpvError : r))
     (f x)
forall e (r :: [(* -> *) -> * -> *]) a.
MemberWithError (Stop e) r =>
e -> Sem r a
stop MpvError
err

interpretIpcNative ::
  Members [Reader MpvProcessConfig, Resource, Async, Race, Log, Time t d, Embed IO, Final IO] r =>
  InterpretersFor [
    Scoped (Either MpvError (MpvResources Value)) (Ipc Value Command !! MpvError),
    ChanConsumer MpvEvent
  ] r
interpretIpcNative :: forall t d (r :: [(* -> *) -> * -> *]).
Members
  '[Reader MpvProcessConfig, Resource, Async, Race, Log, Time t d,
    Embed IO, Final IO]
  r =>
InterpretersFor
  '[Scoped
      (Either MpvError (MpvResources Value))
      (Ipc Value Command !! MpvError),
    ChanConsumer MpvEvent]
  r
interpretIpcNative =
  Sem
  (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r) a
-> Sem r a
forall e (r :: [(* -> *) -> * -> *]).
Members '[Resource, Race, Async, Embed IO] r =>
InterpretersFor '[Events (OutChan e) e, ChanConsumer e] r
interpretEventsChan (Sem
   (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r) a
 -> Sem r a)
-> (Sem
      (Scoped
         (Either MpvError (MpvResources Value))
         (Ipc Value Command !! MpvError)
         : ChanConsumer MpvEvent : r)
      a
    -> Sem
         (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r) a)
-> Sem
     (Scoped
        (Either MpvError (MpvResources Value))
        (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : r)
     a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  (forall x.
 (Either MpvError (MpvResources Value)
  -> Sem
       (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r) x)
 -> Sem
      (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r) x)
-> (Either MpvError (MpvResources Value)
    -> InterpreterFor
         (Ipc Value Command !! MpvError)
         (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r))
-> InterpreterFor
     (Scoped
        (Either MpvError (MpvResources Value))
        (Ipc Value Command !! MpvError))
     (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r)
forall resource (effect :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]).
(forall x. (resource -> Sem r x) -> Sem r x)
-> (resource -> InterpreterFor effect r)
-> InterpreterFor (Scoped resource effect) r
runScoped forall x.
(Either MpvError (MpvResources Value)
 -> Sem
      (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r) x)
-> Sem
     (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r) x
forall token (r :: [(* -> *) -> * -> *]) t d a.
(Members '[Reader MpvProcessConfig, Events token MpvEvent] r,
 Members
   '[Resource, Race, Async, Log, Time t d, Embed IO, Final IO] r) =>
(Either MpvError (MpvResources Value) -> Sem r a) -> Sem r a
withMpvResources Either MpvError (MpvResources Value)
-> InterpreterFor
     (Ipc Value Command !! MpvError)
     (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r)
forall token (r :: [(* -> *) -> * -> *]).
Members
  '[EventConsumer token MpvEvent, Resource, Async, Race, Log,
    Embed IO, Final IO]
  r =>
Either MpvError (MpvResources Value)
-> InterpreterFor (Ipc Value Command !! MpvError) r
interpretIpcResources (Sem
   (Scoped
      (Either MpvError (MpvResources Value))
      (Ipc Value Command !! MpvError)
      : Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r)
   a
 -> Sem
      (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r) a)
-> (Sem
      (Scoped
         (Either MpvError (MpvResources Value))
         (Ipc Value Command !! MpvError)
         : ChanConsumer MpvEvent : r)
      a
    -> Sem
         (Scoped
            (Either MpvError (MpvResources Value))
            (Ipc Value Command !! MpvError)
            : Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r)
         a)
-> Sem
     (Scoped
        (Either MpvError (MpvResources Value))
        (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : r)
     a
-> Sem
     (Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  (Scoped
     (Either MpvError (MpvResources Value))
     (Ipc Value Command !! MpvError)
     : ChanConsumer MpvEvent : r)
  a
-> Sem
     (Scoped
        (Either MpvError (MpvResources Value))
        (Ipc Value Command !! MpvError)
        : Events (OutChan MpvEvent) MpvEvent : ChanConsumer MpvEvent : r)
     a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder

withIpc ::
  Member (Scoped resource (Ipc fmt command !! MpvError)) r =>
  InterpreterFor (Ipc fmt command !! MpvError) r
withIpc :: forall resource fmt (command :: * -> *)
       (r :: [(* -> *) -> * -> *]).
Member (Scoped resource (Ipc fmt command !! MpvError)) r =>
InterpreterFor (Ipc fmt command !! MpvError) r
withIpc =
  Sem ((Ipc fmt command !! MpvError) : r) a -> Sem r a
forall resource (effect :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]).
Member (Scoped resource effect) r =>
InterpreterFor effect r
scoped