-- |Description: Mpv Client/Server Interpreters
module Mpv.Interpreter.MpvServer where

import Data.Some (withSome)
import Exon (exon)
import qualified Polysemy.Conc as Conc
import Polysemy.Conc (ChanConsumer, interpretQueueTBM, withAsync_)
import qualified Polysemy.Conc.Data.QueueResult as QueueResult
import qualified Polysemy.Conc.Queue as Queue
import Polysemy.Internal.Tactics (liftT)
import qualified Polysemy.Log as Log

import Mpv.Data.Command (Command)
import qualified Mpv.Data.Event as Event
import qualified Mpv.Data.EventPayload as EventPayload
import qualified Mpv.Data.EventPayload as EndReason
import Mpv.Data.MpvError (MpvError)
import Mpv.Data.MpvEvent (MpvEvent (MpvEvent))
import Mpv.Data.MpvProcessConfig (MpvProcessConfig)
import qualified Mpv.Effect.Ipc as Ipc
import Mpv.Effect.Ipc (Ipc)
import Mpv.Effect.Mpv (Mpv)
import qualified Mpv.Effect.MpvServer as MpvServer
import Mpv.Effect.MpvServer (MpvServer)
import Mpv.Interpreter.Ipc (interpretIpcNative, waitEventAndRun, withIpc)
import Mpv.Interpreter.Mpv (interpretMpvIpc)

data Control command where
  SendCommand :: command a -> MVar (Either MpvError a) -> Control command
  Terminate :: Control command

dispatch ::
   fmt command r a .
  Members [Ipc fmt command !! MpvError, Embed IO] r =>
  command a ->
  MVar (Either MpvError a) ->
  Sem r ()
dispatch :: forall fmt (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Members '[Ipc fmt command !! MpvError, Embed IO] r =>
command a -> MVar (Either MpvError a) -> Sem r ()
dispatch command a
cmd MVar (Either MpvError a)
result = do
  Either MpvError a
r <- (MpvError -> Sem r (Either MpvError a))
-> Sem (Ipc fmt command : r) (Either MpvError a)
-> Sem r (Either MpvError a)
forall err (eff :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *])
       a.
Member (Resumable err eff) r =>
(err -> Sem r a) -> Sem (eff : r) a -> Sem r a
resuming (Either MpvError a -> Sem r (Either MpvError a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either MpvError a -> Sem r (Either MpvError a))
-> (MpvError -> Either MpvError a)
-> MpvError
-> Sem r (Either MpvError a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MpvError -> Either MpvError a
forall a b. a -> Either a b
Left) (a -> Either MpvError a
forall a b. b -> Either a b
Right (a -> Either MpvError a)
-> Sem (Ipc fmt command : r) a
-> Sem (Ipc fmt command : r) (Either MpvError a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> command a -> Sem (Ipc fmt command : r) a
forall fmt (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
MemberWithError (Ipc fmt command) r =>
command a -> Sem r a
Ipc.sync command a
cmd)
  IO () -> Sem r ()
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
embed (MVar (Either MpvError a) -> Either MpvError a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (Either MpvError a)
result Either MpvError a
r)

serverActive ::
  Members [Queue (Control command), Scoped resource (Ipc fmt command !! MpvError), Log, Embed IO] r =>
  Sem r ()
serverActive :: forall (command :: * -> *) resource fmt
       (r :: [(* -> *) -> * -> *]).
Members
  '[Queue (Control command),
    Scoped resource (Ipc fmt command !! MpvError), Log, Embed IO]
  r =>
Sem r ()
serverActive =
  Sem ((Ipc fmt command !! MpvError) : r) () -> Sem r ()
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) ()
spin
  where
    spin :: Sem ((Ipc fmt command !! MpvError) : r) ()
spin =
      Sem
  ((Ipc fmt command !! MpvError) : r) (QueueResult (Control command))
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Queue d) r =>
Sem r (QueueResult d)
Queue.read Sem
  ((Ipc fmt command !! MpvError) : r) (QueueResult (Control command))
-> (QueueResult (Control command)
    -> Sem ((Ipc fmt command !! MpvError) : r) ())
-> Sem ((Ipc fmt command !! MpvError) : r) ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        QueueResult.Success Control command
Terminate ->
          Sem ((Ipc fmt command !! MpvError) : r) ()
forall (f :: * -> *). Applicative f => f ()
unit
        QueueResult.Success (SendCommand command a
cmd MVar (Either MpvError a)
result) -> do
          command a
-> MVar (Either MpvError a)
-> Sem ((Ipc fmt command !! MpvError) : r) ()
forall fmt (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Members '[Ipc fmt command !! MpvError, Embed IO] r =>
command a -> MVar (Either MpvError a) -> Sem r ()
dispatch command a
cmd MVar (Either MpvError a)
result
          Sem ((Ipc fmt command !! MpvError) : r) ()
spin
        QueueResult (Control command)
QueueResult.NotAvailable ->
          Sem ((Ipc fmt command !! MpvError) : r) ()
forall (f :: * -> *). Applicative f => f ()
unit
        QueueResult (Control command)
QueueResult.Closed ->
          Sem ((Ipc fmt command !! MpvError) : r) ()
forall (f :: * -> *). Applicative f => f ()
unit

serverIdle ::
  Members [Queue (Control command), Scoped resource (Ipc fmt command !! MpvError), Log, Embed IO] r =>
  Sem r ()
serverIdle :: forall (command :: * -> *) resource fmt
       (r :: [(* -> *) -> * -> *]).
Members
  '[Queue (Control command),
    Scoped resource (Ipc fmt command !! MpvError), Log, Embed IO]
  r =>
Sem r ()
serverIdle =
  Sem r (QueueResult (Control command))
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Queue d) r =>
Sem r (QueueResult d)
Queue.peek Sem r (QueueResult (Control command))
-> (QueueResult (Control command) -> Sem r ()) -> Sem r ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    QueueResult.Success Control command
Terminate ->
      Sem r (QueueResult (Control command))
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Queue d) r =>
Sem r (QueueResult d)
Queue.read Sem r (QueueResult (Control command)) -> Sem r () -> Sem r ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Text -> Sem r ()
forall (r :: [(* -> *) -> * -> *]).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
Log.warn Text
"mpv server: received Terminate in idle server"
    QueueResult.Success (SendCommand command a
_ MVar (Either MpvError a)
_) ->
      Sem r ()
forall (command :: * -> *) resource fmt
       (r :: [(* -> *) -> * -> *]).
Members
  '[Queue (Control command),
    Scoped resource (Ipc fmt command !! MpvError), Log, Embed IO]
  r =>
Sem r ()
serverActive Sem r () -> Sem r () -> Sem r ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Sem r ()
forall (command :: * -> *) resource fmt
       (r :: [(* -> *) -> * -> *]).
Members
  '[Queue (Control command),
    Scoped resource (Ipc fmt command !! MpvError), Log, Embed IO]
  r =>
Sem r ()
serverIdle
    QueueResult (Control command)
QueueResult.NotAvailable -> Sem r ()
forall (f :: * -> *). Applicative f => f ()
unit
    QueueResult (Control command)
QueueResult.Closed -> Sem r ()
forall (f :: * -> *). Applicative f => f ()
unit

serverEventListener ::
  Members [EventConsumer token MpvEvent, MpvServer command !! MpvError, Log] r =>
  Sem r ()
serverEventListener :: forall token (command :: * -> *) (r :: [(* -> *) -> * -> *]).
Members
  '[EventConsumer token MpvEvent, MpvServer command !! MpvError, Log]
  r =>
Sem r ()
serverEventListener =
  (MpvEvent -> Sem r ()) -> Sem r ()
forall e token (r :: [(* -> *) -> * -> *]).
Member (EventConsumer token e) r =>
(e -> Sem r ()) -> Sem r ()
Conc.subscribeLoop \case
    MpvEvent EventName
_ Some Event
payload ->
      Some Event
-> (forall (a :: EventName). Event a -> Sem r ()) -> Sem r ()
forall {k} (tag :: k -> *) b.
Some tag -> (forall (a :: k). tag a -> b) -> b
withSome Some Event
payload \case
        Event.EndFile (EventPayload.EndFile Int
_ EndReason
EndReason.Stop) ->
          Sem r ()
forall (f :: * -> *). Applicative f => f ()
unit
        Event.EndFile EndFile
_ ->
          Sem (MpvServer command : r) ()
-> (MpvError -> Sem r ()) -> Sem r ()
forall err (eff :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *])
       a.
Member (Resumable err eff) r =>
Sem (eff : r) a -> (err -> Sem r a) -> Sem r a
resume Sem (MpvServer command : r) ()
forall (command :: * -> *) (r :: [(* -> *) -> * -> *]).
MemberWithError (MpvServer command) r =>
Sem r ()
MpvServer.terminate \ MpvError
e ->
            Text -> Sem r ()
forall (r :: [(* -> *) -> * -> *]).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
Log.warn [exon|mpv server event listener: failed to send Terminate: #{show e}|]
        Event a
_ ->
          Sem r ()
forall (f :: * -> *). Applicative f => f ()
unit

interpretMpvServer ::
  Members [Queue (Control Command), Embed IO] r =>
  InterpreterFor (MpvServer Command !! MpvError) r
interpretMpvServer :: forall (r :: [(* -> *) -> * -> *]).
Members '[Queue (Control Command), Embed IO] r =>
InterpreterFor (MpvServer Command !! MpvError) r
interpretMpvServer =
  (forall x (r0 :: [(* -> *) -> * -> *]).
 MpvServer Command (Sem r0) x -> Sem (Stop MpvError : r) x)
-> InterpreterFor (MpvServer Command !! MpvError) r
forall err (eff :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]).
FirstOrder eff "interpretResumable" =>
(forall x (r0 :: [(* -> *) -> * -> *]).
 eff (Sem r0) x -> Sem (Stop err : r) x)
-> InterpreterFor (Resumable err eff) r
interpretResumable \case
    MpvServer.Send Command x
cmd -> do
      MVar (Either MpvError x)
result <- IO (MVar (Either MpvError x))
-> Sem (Stop MpvError : r) (MVar (Either MpvError x))
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
embed IO (MVar (Either MpvError x))
forall a. IO (MVar a)
newEmptyMVar
      Control Command -> Sem (Stop MpvError : r) ()
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Queue d) r =>
d -> Sem r ()
Queue.write (Command x -> MVar (Either MpvError x) -> Control Command
forall (command :: * -> *) a.
command a -> MVar (Either MpvError a) -> Control command
SendCommand Command x
cmd MVar (Either MpvError x)
result)
      Either MpvError x -> Sem (Stop MpvError : r) x
forall err (r :: [(* -> *) -> * -> *]) a.
Member (Stop err) r =>
Either err a -> Sem r a
stopEither (Either MpvError x -> Sem (Stop MpvError : r) x)
-> Sem (Stop MpvError : r) (Either MpvError x)
-> Sem (Stop MpvError : r) x
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO (Either MpvError x)
-> Sem (Stop MpvError : r) (Either MpvError x)
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
embed (MVar (Either MpvError x) -> IO (Either MpvError x)
forall a. MVar a -> IO a
takeMVar MVar (Either MpvError x)
result)
    MpvServer Command (Sem r0) x
MpvServer.Terminate ->
      Control Command -> Sem (Stop MpvError : r) ()
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Queue d) r =>
d -> Sem r ()
Queue.write Control Command
forall (command :: * -> *). Control command
Terminate

withMpvServer ::
  Members [Reader MpvProcessConfig, Time t d, Log, Resource, Race, Async, Embed IO, Final IO] r =>
  InterpretersFor [MpvServer Command !! MpvError, ChanConsumer MpvEvent] r
withMpvServer :: forall t d (r :: [(* -> *) -> * -> *]).
Members
  '[Reader MpvProcessConfig, Time t d, Log, Resource, Race, Async,
    Embed IO, Final IO]
  r =>
InterpretersFor
  '[MpvServer Command !! MpvError, ChanConsumer MpvEvent] r
withMpvServer =
  Int -> InterpreterFor (Queue (Control Command)) r
forall d (r :: [(* -> *) -> * -> *]).
Members '[Resource, Race, Embed IO] r =>
Int -> InterpreterFor (Queue d) r
interpretQueueTBM Int
64 (Sem (Queue (Control Command) : r) a -> Sem r a)
-> (Sem
      ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
    -> Sem (Queue (Control Command) : r) a)
-> Sem
     ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  (Scoped
     (Either MpvError (MpvResources Value))
     (Ipc Value Command !! MpvError)
     : ChanConsumer MpvEvent : Queue (Control Command) : r)
  a
-> Sem (Queue (Control Command) : r) a
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
   (Scoped
      (Either MpvError (MpvResources Value))
      (Ipc Value Command !! MpvError)
      : ChanConsumer MpvEvent : Queue (Control Command) : r)
   a
 -> Sem (Queue (Control Command) : r) a)
-> (Sem
      ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
    -> Sem
         (Scoped
            (Either MpvError (MpvResources Value))
            (Ipc Value Command !! MpvError)
            : ChanConsumer MpvEvent : Queue (Control Command) : r)
         a)
-> Sem
     ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
-> Sem (Queue (Control Command) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  (Scoped
     (Either MpvError (MpvResources Value))
     (Ipc Value Command !! MpvError)
     : ChanConsumer MpvEvent : Queue (Control Command) : r)
  ()
-> Sem
     (Scoped
        (Either MpvError (MpvResources Value))
        (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
-> Sem
     (Scoped
        (Either MpvError (MpvResources Value))
        (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
forall (r :: [(* -> *) -> * -> *]) b a.
Members '[Resource, Race, Async] r =>
Sem r b -> Sem r a -> Sem r a
withAsync_ Sem
  (Scoped
     (Either MpvError (MpvResources Value))
     (Ipc Value Command !! MpvError)
     : ChanConsumer MpvEvent : Queue (Control Command) : r)
  ()
forall (command :: * -> *) resource fmt
       (r :: [(* -> *) -> * -> *]).
Members
  '[Queue (Control command),
    Scoped resource (Ipc fmt command !! MpvError), Log, Embed IO]
  r =>
Sem r ()
serverIdle (Sem
   (Scoped
      (Either MpvError (MpvResources Value))
      (Ipc Value Command !! MpvError)
      : ChanConsumer MpvEvent : Queue (Control Command) : r)
   a
 -> Sem
      (Scoped
         (Either MpvError (MpvResources Value))
         (Ipc Value Command !! MpvError)
         : ChanConsumer MpvEvent : Queue (Control Command) : r)
      a)
-> (Sem
      ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
    -> Sem
         (Scoped
            (Either MpvError (MpvResources Value))
            (Ipc Value Command !! MpvError)
            : ChanConsumer MpvEvent : Queue (Control Command) : r)
         a)
-> Sem
     ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
-> Sem
     (Scoped
        (Either MpvError (MpvResources Value))
        (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  ((MpvServer Command !! MpvError)
     : Scoped
         (Either MpvError (MpvResources Value))
         (Ipc Value Command !! MpvError)
     : ChanConsumer MpvEvent : Queue (Control Command) : r)
  a
-> Sem
     (Scoped
        (Either MpvError (MpvResources Value))
        (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
forall (r :: [(* -> *) -> * -> *]).
Members '[Queue (Control Command), Embed IO] r =>
InterpreterFor (MpvServer Command !! MpvError) r
interpretMpvServer (Sem
   ((MpvServer Command !! MpvError)
      : Scoped
          (Either MpvError (MpvResources Value))
          (Ipc Value Command !! MpvError)
      : ChanConsumer MpvEvent : Queue (Control Command) : r)
   a
 -> Sem
      (Scoped
         (Either MpvError (MpvResources Value))
         (Ipc Value Command !! MpvError)
         : ChanConsumer MpvEvent : Queue (Control Command) : r)
      a)
-> (Sem
      ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
    -> Sem
         ((MpvServer Command !! MpvError)
            : Scoped
                (Either MpvError (MpvResources Value))
                (Ipc Value Command !! MpvError)
            : ChanConsumer MpvEvent : Queue (Control Command) : r)
         a)
-> Sem
     ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
-> Sem
     (Scoped
        (Either MpvError (MpvResources Value))
        (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  ((MpvServer Command !! MpvError)
     : Scoped
         (Either MpvError (MpvResources Value))
         (Ipc Value Command !! MpvError)
     : ChanConsumer MpvEvent : Queue (Control Command) : r)
  ()
-> Sem
     ((MpvServer Command !! MpvError)
        : Scoped
            (Either MpvError (MpvResources Value))
            (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
-> Sem
     ((MpvServer Command !! MpvError)
        : Scoped
            (Either MpvError (MpvResources Value))
            (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
forall (r :: [(* -> *) -> * -> *]) b a.
Members '[Resource, Race, Async] r =>
Sem r b -> Sem r a -> Sem r a
withAsync_ Sem
  ((MpvServer Command !! MpvError)
     : Scoped
         (Either MpvError (MpvResources Value))
         (Ipc Value Command !! MpvError)
     : ChanConsumer MpvEvent : Queue (Control Command) : r)
  ()
forall token (command :: * -> *) (r :: [(* -> *) -> * -> *]).
Members
  '[EventConsumer token MpvEvent, MpvServer command !! MpvError, Log]
  r =>
Sem r ()
serverEventListener (Sem
   ((MpvServer Command !! MpvError)
      : Scoped
          (Either MpvError (MpvResources Value))
          (Ipc Value Command !! MpvError)
      : ChanConsumer MpvEvent : Queue (Control Command) : r)
   a
 -> Sem
      ((MpvServer Command !! MpvError)
         : Scoped
             (Either MpvError (MpvResources Value))
             (Ipc Value Command !! MpvError)
         : ChanConsumer MpvEvent : Queue (Control Command) : r)
      a)
-> (Sem
      ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
    -> Sem
         ((MpvServer Command !! MpvError)
            : Scoped
                (Either MpvError (MpvResources Value))
                (Ipc Value Command !! MpvError)
            : ChanConsumer MpvEvent : Queue (Control Command) : r)
         a)
-> Sem
     ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
-> Sem
     ((MpvServer Command !! MpvError)
        : Scoped
            (Either MpvError (MpvResources Value))
            (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  ((MpvServer Command !! MpvError)
     : ChanConsumer MpvEvent : Queue (Control Command) : r)
  a
-> Sem
     ((MpvServer Command !! MpvError)
        : Scoped
            (Either MpvError (MpvResources Value))
            (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder (Sem
   ((MpvServer Command !! MpvError)
      : ChanConsumer MpvEvent : Queue (Control Command) : r)
   a
 -> Sem
      ((MpvServer Command !! MpvError)
         : Scoped
             (Either MpvError (MpvResources Value))
             (Ipc Value Command !! MpvError)
         : ChanConsumer MpvEvent : Queue (Control Command) : r)
      a)
-> (Sem
      ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
    -> Sem
         ((MpvServer Command !! MpvError)
            : ChanConsumer MpvEvent : Queue (Control Command) : r)
         a)
-> Sem
     ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
-> Sem
     ((MpvServer Command !! MpvError)
        : Scoped
            (Either MpvError (MpvResources Value))
            (Ipc Value Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem ((MpvServer Command !! MpvError) : ChanConsumer MpvEvent : r) a
-> Sem
     ((MpvServer Command !! MpvError)
        : ChanConsumer MpvEvent : Queue (Control Command) : r)
     a
forall (e3 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (e2 :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : e2 : r) a -> Sem (e1 : e2 : e3 : r) a
raise2Under

interpretIpcClient ::
  Member (MpvServer command !! MpvError) r =>
  Members [EventConsumer token MpvEvent, Log, Resource, Async, Race] r =>
  InterpreterFor (Ipc fmt command !! MpvError) r
interpretIpcClient :: forall (command :: * -> *) (r :: [(* -> *) -> * -> *]) token fmt.
(Member (MpvServer command !! MpvError) r,
 Members
   '[EventConsumer token MpvEvent, Log, Resource, Async, Race] r) =>
InterpreterFor (Ipc fmt command !! MpvError) r
interpretIpcClient =
  (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 ->
      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 (Sem (MpvServer command : Stop MpvError : r) x
-> Sem (Stop MpvError : r) x
forall err (eff :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]).
Members '[Resumable err eff, Stop err] r =>
InterpreterFor eff r
restop (command x -> Sem (MpvServer command : Stop MpvError : r) x
forall (command :: * -> *) (r :: [(* -> *) -> * -> *]) a.
MemberWithError (MpvServer command) r =>
command a -> Sem r a
MpvServer.send 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)

interpretMpvClient ::
  Members [MpvServer Command !! MpvError, EventConsumer token MpvEvent, Log, Resource, Async, Race] r =>
  InterpreterFor (Mpv !! MpvError) r
interpretMpvClient :: forall token (r :: [(* -> *) -> * -> *]).
Members
  '[MpvServer Command !! MpvError, EventConsumer token MpvEvent, Log,
    Resource, Async, Race]
  r =>
InterpreterFor (Mpv !! MpvError) r
interpretMpvClient =
  Sem ((Ipc Any Command !! MpvError) : r) a -> Sem r a
forall (command :: * -> *) (r :: [(* -> *) -> * -> *]) token fmt.
(Member (MpvServer command !! MpvError) r,
 Members
   '[EventConsumer token MpvEvent, Log, Resource, Async, Race] r) =>
InterpreterFor (Ipc fmt command !! MpvError) r
interpretIpcClient (Sem ((Ipc Any Command !! MpvError) : r) a -> Sem r a)
-> (Sem ((Mpv !! MpvError) : r) a
    -> Sem ((Ipc Any Command !! MpvError) : r) a)
-> Sem ((Mpv !! MpvError) : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem ((Mpv !! MpvError) : (Ipc Any Command !! MpvError) : r) a
-> Sem ((Ipc Any Command !! MpvError) : r) a
forall fmt (r :: [(* -> *) -> * -> *]).
Member (Ipc fmt Command !! MpvError) r =>
InterpreterFor (Mpv !! MpvError) r
interpretMpvIpc (Sem ((Mpv !! MpvError) : (Ipc Any Command !! MpvError) : r) a
 -> Sem ((Ipc Any Command !! MpvError) : r) a)
-> (Sem ((Mpv !! MpvError) : r) a
    -> Sem ((Mpv !! MpvError) : (Ipc Any Command !! MpvError) : r) a)
-> Sem ((Mpv !! MpvError) : r) a
-> Sem ((Ipc Any Command !! MpvError) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem ((Mpv !! MpvError) : r) a
-> Sem ((Mpv !! MpvError) : (Ipc Any Command !! MpvError) : r) a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder