-- | This module defines 'TriggerEventT', the standard implementation of
-- 'TriggerEvent'.
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
module Reflex.TriggerEvent.Base
  ( TriggerEventT (..)
  , runTriggerEventT
  , askEvents
  , TriggerInvocation (..)
  , EventTriggerRef (..)
  ) where

import Control.Applicative (liftA2)
import Control.Concurrent
import Control.Monad.Exception
import Control.Monad.Primitive
import Control.Monad.Reader
import Control.Monad.Ref
import Data.Coerce
import Data.Dependent.Sum
import Data.IORef
import Data.Monoid ((<>))
import qualified Data.Semigroup as S
import Reflex.Class
import Reflex.Adjustable.Class
import Reflex.Host.Class
import Reflex.PerformEvent.Class
import Reflex.PostBuild.Class
import Reflex.TriggerEvent.Class

-- | A value with which to fire an 'Event', as well as a callback to invoke
-- after its propagation has completed.
data TriggerInvocation a = TriggerInvocation a (IO ())

-- | A reference to an 'EventTrigger' suitable for firing with 'TriggerEventT'.
newtype EventTriggerRef t a = EventTriggerRef { forall t a. EventTriggerRef t a -> IORef (Maybe (EventTrigger t a))
unEventTriggerRef :: IORef (Maybe (EventTrigger t a)) }

-- | A basic implementation of 'TriggerEvent'.
newtype TriggerEventT t m a = TriggerEventT { forall t (m :: * -> *) a.
TriggerEventT t m a
-> ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
unTriggerEventT :: ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a }
  deriving ((forall a b.
 (a -> b) -> TriggerEventT t m a -> TriggerEventT t m b)
-> (forall a b. a -> TriggerEventT t m b -> TriggerEventT t m a)
-> Functor (TriggerEventT t m)
forall a b. a -> TriggerEventT t m b -> TriggerEventT t m a
forall a b. (a -> b) -> TriggerEventT t m a -> TriggerEventT t m b
forall t (m :: * -> *) a b.
Functor m =>
a -> TriggerEventT t m b -> TriggerEventT t m a
forall t (m :: * -> *) a b.
Functor m =>
(a -> b) -> TriggerEventT t m a -> TriggerEventT t m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall t (m :: * -> *) a b.
Functor m =>
(a -> b) -> TriggerEventT t m a -> TriggerEventT t m b
fmap :: forall a b. (a -> b) -> TriggerEventT t m a -> TriggerEventT t m b
$c<$ :: forall t (m :: * -> *) a b.
Functor m =>
a -> TriggerEventT t m b -> TriggerEventT t m a
<$ :: forall a b. a -> TriggerEventT t m b -> TriggerEventT t m a
Functor, Functor (TriggerEventT t m)
Functor (TriggerEventT t m)
-> (forall a. a -> TriggerEventT t m a)
-> (forall a b.
    TriggerEventT t m (a -> b)
    -> TriggerEventT t m a -> TriggerEventT t m b)
-> (forall a b c.
    (a -> b -> c)
    -> TriggerEventT t m a
    -> TriggerEventT t m b
    -> TriggerEventT t m c)
-> (forall a b.
    TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b)
-> (forall a b.
    TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a)
-> Applicative (TriggerEventT t m)
forall a. a -> TriggerEventT t m a
forall a b.
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a
forall a b.
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b
forall a b.
TriggerEventT t m (a -> b)
-> TriggerEventT t m a -> TriggerEventT t m b
forall a b c.
(a -> b -> c)
-> TriggerEventT t m a
-> TriggerEventT t m b
-> TriggerEventT t m c
forall {t} {m :: * -> *}.
Applicative m =>
Functor (TriggerEventT t m)
forall t (m :: * -> *) a. Applicative m => a -> TriggerEventT t m a
forall t (m :: * -> *) a b.
Applicative m =>
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a
forall t (m :: * -> *) a b.
Applicative m =>
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b
forall t (m :: * -> *) a b.
Applicative m =>
TriggerEventT t m (a -> b)
-> TriggerEventT t m a -> TriggerEventT t m b
forall t (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> TriggerEventT t m a
-> TriggerEventT t m b
-> TriggerEventT t m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall t (m :: * -> *) a. Applicative m => a -> TriggerEventT t m a
pure :: forall a. a -> TriggerEventT t m a
$c<*> :: forall t (m :: * -> *) a b.
Applicative m =>
TriggerEventT t m (a -> b)
-> TriggerEventT t m a -> TriggerEventT t m b
<*> :: forall a b.
TriggerEventT t m (a -> b)
-> TriggerEventT t m a -> TriggerEventT t m b
$cliftA2 :: forall t (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> TriggerEventT t m a
-> TriggerEventT t m b
-> TriggerEventT t m c
liftA2 :: forall a b c.
(a -> b -> c)
-> TriggerEventT t m a
-> TriggerEventT t m b
-> TriggerEventT t m c
$c*> :: forall t (m :: * -> *) a b.
Applicative m =>
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b
*> :: forall a b.
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b
$c<* :: forall t (m :: * -> *) a b.
Applicative m =>
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a
<* :: forall a b.
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a
Applicative, Applicative (TriggerEventT t m)
Applicative (TriggerEventT t m)
-> (forall a b.
    TriggerEventT t m a
    -> (a -> TriggerEventT t m b) -> TriggerEventT t m b)
-> (forall a b.
    TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b)
-> (forall a. a -> TriggerEventT t m a)
-> Monad (TriggerEventT t m)
forall a. a -> TriggerEventT t m a
forall a b.
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b
forall a b.
TriggerEventT t m a
-> (a -> TriggerEventT t m b) -> TriggerEventT t m b
forall {t} {m :: * -> *}.
Monad m =>
Applicative (TriggerEventT t m)
forall t (m :: * -> *) a. Monad m => a -> TriggerEventT t m a
forall t (m :: * -> *) a b.
Monad m =>
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b
forall t (m :: * -> *) a b.
Monad m =>
TriggerEventT t m a
-> (a -> TriggerEventT t m b) -> TriggerEventT t m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall t (m :: * -> *) a b.
Monad m =>
TriggerEventT t m a
-> (a -> TriggerEventT t m b) -> TriggerEventT t m b
>>= :: forall a b.
TriggerEventT t m a
-> (a -> TriggerEventT t m b) -> TriggerEventT t m b
$c>> :: forall t (m :: * -> *) a b.
Monad m =>
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b
>> :: forall a b.
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m b
$creturn :: forall t (m :: * -> *) a. Monad m => a -> TriggerEventT t m a
return :: forall a. a -> TriggerEventT t m a
Monad, Monad (TriggerEventT t m)
Monad (TriggerEventT t m)
-> (forall a. (a -> TriggerEventT t m a) -> TriggerEventT t m a)
-> MonadFix (TriggerEventT t m)
forall a. (a -> TriggerEventT t m a) -> TriggerEventT t m a
forall {t} {m :: * -> *}. MonadFix m => Monad (TriggerEventT t m)
forall t (m :: * -> *) a.
MonadFix m =>
(a -> TriggerEventT t m a) -> TriggerEventT t m a
forall (m :: * -> *).
Monad m -> (forall a. (a -> m a) -> m a) -> MonadFix m
$cmfix :: forall t (m :: * -> *) a.
MonadFix m =>
(a -> TriggerEventT t m a) -> TriggerEventT t m a
mfix :: forall a. (a -> TriggerEventT t m a) -> TriggerEventT t m a
MonadFix, Monad (TriggerEventT t m)
Monad (TriggerEventT t m)
-> (forall a. IO a -> TriggerEventT t m a)
-> MonadIO (TriggerEventT t m)
forall a. IO a -> TriggerEventT t m a
forall {t} {m :: * -> *}. MonadIO m => Monad (TriggerEventT t m)
forall t (m :: * -> *) a. MonadIO m => IO a -> TriggerEventT t m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
$cliftIO :: forall t (m :: * -> *) a. MonadIO m => IO a -> TriggerEventT t m a
liftIO :: forall a. IO a -> TriggerEventT t m a
MonadIO, Monad (TriggerEventT t m)
Monad (TriggerEventT t m)
-> (forall e a. Exception e => e -> TriggerEventT t m a)
-> (forall e a.
    Exception e =>
    TriggerEventT t m a
    -> (e -> TriggerEventT t m a) -> TriggerEventT t m a)
-> (forall a b.
    TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a)
-> MonadException (TriggerEventT t m)
forall e a. Exception e => e -> TriggerEventT t m a
forall e a.
Exception e =>
TriggerEventT t m a
-> (e -> TriggerEventT t m a) -> TriggerEventT t m a
forall a b.
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a
forall {t} {m :: * -> *}.
MonadException m =>
Monad (TriggerEventT t m)
forall t (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> TriggerEventT t m a
forall t (m :: * -> *) e a.
(MonadException m, Exception e) =>
TriggerEventT t m a
-> (e -> TriggerEventT t m a) -> TriggerEventT t m a
forall t (m :: * -> *) a b.
MonadException m =>
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a
forall (m :: * -> *).
Monad m
-> (forall e a. Exception e => e -> m a)
-> (forall e a. Exception e => m a -> (e -> m a) -> m a)
-> (forall a b. m a -> m b -> m a)
-> MonadException m
$cthrow :: forall t (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> TriggerEventT t m a
throw :: forall e a. Exception e => e -> TriggerEventT t m a
$ccatch :: forall t (m :: * -> *) e a.
(MonadException m, Exception e) =>
TriggerEventT t m a
-> (e -> TriggerEventT t m a) -> TriggerEventT t m a
catch :: forall e a.
Exception e =>
TriggerEventT t m a
-> (e -> TriggerEventT t m a) -> TriggerEventT t m a
$cfinally :: forall t (m :: * -> *) a b.
MonadException m =>
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a
finally :: forall a b.
TriggerEventT t m a -> TriggerEventT t m b -> TriggerEventT t m a
MonadException, MonadIO (TriggerEventT t m)
MonadException (TriggerEventT t m)
MonadIO (TriggerEventT t m)
-> MonadException (TriggerEventT t m)
-> (forall b.
    ((forall a. TriggerEventT t m a -> TriggerEventT t m a)
     -> TriggerEventT t m b)
    -> TriggerEventT t m b)
-> MonadAsyncException (TriggerEventT t m)
forall b.
((forall a. TriggerEventT t m a -> TriggerEventT t m a)
 -> TriggerEventT t m b)
-> TriggerEventT t m b
forall {t} {m :: * -> *}.
MonadAsyncException m =>
MonadIO (TriggerEventT t m)
forall {t} {m :: * -> *}.
MonadAsyncException m =>
MonadException (TriggerEventT t m)
forall t (m :: * -> *) b.
MonadAsyncException m =>
((forall a. TriggerEventT t m a -> TriggerEventT t m a)
 -> TriggerEventT t m b)
-> TriggerEventT t m b
forall (m :: * -> *).
MonadIO m
-> MonadException m
-> (forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> MonadAsyncException m
$cmask :: forall t (m :: * -> *) b.
MonadAsyncException m =>
((forall a. TriggerEventT t m a -> TriggerEventT t m a)
 -> TriggerEventT t m b)
-> TriggerEventT t m b
mask :: forall b.
((forall a. TriggerEventT t m a -> TriggerEventT t m a)
 -> TriggerEventT t m b)
-> TriggerEventT t m b
MonadAsyncException)

-- | Run a 'TriggerEventT' action.  The argument should be a 'Chan' into which
-- 'TriggerInvocation's can be passed; it is expected that some other thread
-- will be responsible for popping values out of the 'Chan' and firing their
-- 'EventTrigger's.
runTriggerEventT :: TriggerEventT t m a -> Chan [DSum (EventTriggerRef t) TriggerInvocation] -> m a
runTriggerEventT :: forall t (m :: * -> *) a.
TriggerEventT t m a
-> Chan [DSum (EventTriggerRef t) TriggerInvocation] -> m a
runTriggerEventT = ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> Chan [DSum (EventTriggerRef t) TriggerInvocation] -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
 -> Chan [DSum (EventTriggerRef t) TriggerInvocation] -> m a)
-> (TriggerEventT t m a
    -> ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a)
-> TriggerEventT t m a
-> Chan [DSum (EventTriggerRef t) TriggerInvocation]
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TriggerEventT t m a
-> ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
forall t (m :: * -> *) a.
TriggerEventT t m a
-> ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
unTriggerEventT

instance MonadTrans (TriggerEventT t) where
  {-# INLINABLE lift #-}
  lift :: forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
lift = ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> TriggerEventT t m a
forall t (m :: * -> *) a.
ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> TriggerEventT t m a
TriggerEventT (ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
 -> TriggerEventT t m a)
-> (m a
    -> ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a)
-> m a
-> TriggerEventT t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a
-> ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
forall (m :: * -> *) a.
Monad m =>
m a
-> ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

instance PrimMonad m => PrimMonad (TriggerEventT t m) where
  type PrimState (TriggerEventT t m) = PrimState m
  {-# INLINABLE primitive #-}
  primitive :: forall a.
(State# (PrimState (TriggerEventT t m))
 -> (# State# (PrimState (TriggerEventT t m)), a #))
-> TriggerEventT t m a
primitive = m a -> TriggerEventT t m a
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> TriggerEventT t m a)
-> ((State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a)
-> (State# (PrimState m) -> (# State# (PrimState m), a #))
-> TriggerEventT t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive

instance PerformEvent t m => PerformEvent t (TriggerEventT t m) where
  type Performable (TriggerEventT t m) = Performable m
  {-# INLINABLE performEvent_ #-}
  performEvent_ :: Event t (Performable (TriggerEventT t m) ())
-> TriggerEventT t m ()
performEvent_ Event t (Performable (TriggerEventT t m) ())
e = m () -> TriggerEventT t m ()
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> TriggerEventT t m ()) -> m () -> TriggerEventT t m ()
forall a b. (a -> b) -> a -> b
$ Event t (Performable m ()) -> m ()
forall t (m :: * -> *).
PerformEvent t m =>
Event t (Performable m ()) -> m ()
performEvent_ Event t (Performable m ())
Event t (Performable (TriggerEventT t m) ())
e
  {-# INLINABLE performEvent #-}
  performEvent :: forall a.
Event t (Performable (TriggerEventT t m) a)
-> TriggerEventT t m (Event t a)
performEvent Event t (Performable (TriggerEventT t m) a)
e = m (Event t a) -> TriggerEventT t m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> TriggerEventT t m (Event t a))
-> m (Event t a) -> TriggerEventT t m (Event t a)
forall a b. (a -> b) -> a -> b
$ Event t (Performable m a) -> m (Event t a)
forall a. Event t (Performable m a) -> m (Event t a)
forall t (m :: * -> *) a.
PerformEvent t m =>
Event t (Performable m a) -> m (Event t a)
performEvent Event t (Performable m a)
Event t (Performable (TriggerEventT t m) a)
e

instance PostBuild t m => PostBuild t (TriggerEventT t m) where
  {-# INLINABLE getPostBuild #-}
  getPostBuild :: TriggerEventT t m (Event t ())
getPostBuild = m (Event t ()) -> TriggerEventT t m (Event t ())
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t ())
forall t (m :: * -> *). PostBuild t m => m (Event t ())
getPostBuild

instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (TriggerEventT t m) where
  {-# INLINABLE newEventWithTrigger #-}
  newEventWithTrigger :: forall a.
(EventTrigger t a -> IO (IO ())) -> TriggerEventT t m (Event t a)
newEventWithTrigger = m (Event t a) -> TriggerEventT t m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> TriggerEventT t m (Event t a))
-> ((EventTrigger t a -> IO (IO ())) -> m (Event t a))
-> (EventTrigger t a -> IO (IO ()))
-> TriggerEventT t m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EventTrigger t a -> IO (IO ())) -> m (Event t a)
forall a. (EventTrigger t a -> IO (IO ())) -> m (Event t a)
forall t (m :: * -> *) a.
MonadReflexCreateTrigger t m =>
(EventTrigger t a -> IO (IO ())) -> m (Event t a)
newEventWithTrigger
  {-# INLINABLE newFanEventWithTrigger #-}
  newFanEventWithTrigger :: forall (k :: * -> *).
GCompare k =>
(forall a. k a -> EventTrigger t a -> IO (IO ()))
-> TriggerEventT t m (EventSelector t k)
newFanEventWithTrigger forall a. k a -> EventTrigger t a -> IO (IO ())
f = m (EventSelector t k) -> TriggerEventT t m (EventSelector t k)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (EventSelector t k) -> TriggerEventT t m (EventSelector t k))
-> m (EventSelector t k) -> TriggerEventT t m (EventSelector t k)
forall a b. (a -> b) -> a -> b
$ (forall a. k a -> EventTrigger t a -> IO (IO ()))
-> m (EventSelector t k)
forall t (m :: * -> *) (k :: * -> *).
(MonadReflexCreateTrigger t m, GCompare k) =>
(forall a. k a -> EventTrigger t a -> IO (IO ()))
-> m (EventSelector t k)
forall (k :: * -> *).
GCompare k =>
(forall a. k a -> EventTrigger t a -> IO (IO ()))
-> m (EventSelector t k)
newFanEventWithTrigger k a -> EventTrigger t a -> IO (IO ())
forall a. k a -> EventTrigger t a -> IO (IO ())
f

instance (Monad m, MonadRef m, Ref m ~ Ref IO, MonadReflexCreateTrigger t m) => TriggerEvent t (TriggerEventT t m) where
  {-# INLINABLE newTriggerEvent #-}
  newTriggerEvent :: forall a. TriggerEventT t m (Event t a, a -> IO ())
newTriggerEvent = do
    (Event t a
e, a -> IO () -> IO ()
t) <- TriggerEventT t m (Event t a, a -> IO () -> IO ())
forall a. TriggerEventT t m (Event t a, a -> IO () -> IO ())
forall t (m :: * -> *) a.
TriggerEvent t m =>
m (Event t a, a -> IO () -> IO ())
newTriggerEventWithOnComplete
    (Event t a, a -> IO ())
-> TriggerEventT t m (Event t a, a -> IO ())
forall a. a -> TriggerEventT t m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Event t a
e, \a
a -> a -> IO () -> IO ()
t a
a (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
  {-# INLINABLE newTriggerEventWithOnComplete #-}
  newTriggerEventWithOnComplete :: forall a. TriggerEventT t m (Event t a, a -> IO () -> IO ())
newTriggerEventWithOnComplete = do
    Chan [DSum (EventTriggerRef t) TriggerInvocation]
events <- TriggerEventT
  t m (Chan [DSum (EventTriggerRef t) TriggerInvocation])
forall (m :: * -> *) t.
Monad m =>
TriggerEventT
  t m (Chan [DSum (EventTriggerRef t) TriggerInvocation])
askEvents
    (Event t a
eResult, IORef (Maybe (EventTrigger t a))
reResultTrigger) <- m (Event t a, IORef (Maybe (EventTrigger t a)))
-> TriggerEventT t m (Event t a, IORef (Maybe (EventTrigger t a)))
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t a, IORef (Maybe (EventTrigger t a)))
m (Event t a, Ref m (Maybe (EventTrigger t a)))
forall t (m :: * -> *) a.
(MonadReflexCreateTrigger t m, MonadRef m, Ref m ~ Ref IO) =>
m (Event t a, Ref m (Maybe (EventTrigger t a)))
newEventWithTriggerRef
    (Event t a, a -> IO () -> IO ())
-> TriggerEventT t m (Event t a, a -> IO () -> IO ())
forall a. a -> TriggerEventT t m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Event t a, a -> IO () -> IO ())
 -> TriggerEventT t m (Event t a, a -> IO () -> IO ()))
-> ((a -> IO () -> IO ()) -> (Event t a, a -> IO () -> IO ()))
-> (a -> IO () -> IO ())
-> TriggerEventT t m (Event t a, a -> IO () -> IO ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,) Event t a
eResult ((a -> IO () -> IO ())
 -> TriggerEventT t m (Event t a, a -> IO () -> IO ()))
-> (a -> IO () -> IO ())
-> TriggerEventT t m (Event t a, a -> IO () -> IO ())
forall a b. (a -> b) -> a -> b
$ \a
a IO ()
cb ->
      Chan [DSum (EventTriggerRef t) TriggerInvocation]
-> [DSum (EventTriggerRef t) TriggerInvocation] -> IO ()
forall a. Chan a -> a -> IO ()
writeChan Chan [DSum (EventTriggerRef t) TriggerInvocation]
events [IORef (Maybe (EventTrigger t a)) -> EventTriggerRef t a
forall t a. IORef (Maybe (EventTrigger t a)) -> EventTriggerRef t a
EventTriggerRef IORef (Maybe (EventTrigger t a))
reResultTrigger EventTriggerRef t a
-> TriggerInvocation a
-> DSum (EventTriggerRef t) TriggerInvocation
forall {k} (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> a -> IO () -> TriggerInvocation a
forall a. a -> IO () -> TriggerInvocation a
TriggerInvocation a
a IO ()
cb]
  {-# INLINABLE newEventWithLazyTriggerWithOnComplete #-}
  newEventWithLazyTriggerWithOnComplete :: forall a.
((a -> IO () -> IO ()) -> IO (IO ()))
-> TriggerEventT t m (Event t a)
newEventWithLazyTriggerWithOnComplete (a -> IO () -> IO ()) -> IO (IO ())
f = do
    Chan [DSum (EventTriggerRef t) TriggerInvocation]
events <- TriggerEventT
  t m (Chan [DSum (EventTriggerRef t) TriggerInvocation])
forall (m :: * -> *) t.
Monad m =>
TriggerEventT
  t m (Chan [DSum (EventTriggerRef t) TriggerInvocation])
askEvents
    m (Event t a) -> TriggerEventT t m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> TriggerEventT t m (Event t a))
-> ((EventTrigger t a -> IO (IO ())) -> m (Event t a))
-> (EventTrigger t a -> IO (IO ()))
-> TriggerEventT t m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EventTrigger t a -> IO (IO ())) -> m (Event t a)
forall a. (EventTrigger t a -> IO (IO ())) -> m (Event t a)
forall t (m :: * -> *) a.
MonadReflexCreateTrigger t m =>
(EventTrigger t a -> IO (IO ())) -> m (Event t a)
newEventWithTrigger ((EventTrigger t a -> IO (IO ())) -> TriggerEventT t m (Event t a))
-> (EventTrigger t a -> IO (IO ()))
-> TriggerEventT t m (Event t a)
forall a b. (a -> b) -> a -> b
$ \EventTrigger t a
t ->
      (a -> IO () -> IO ()) -> IO (IO ())
f ((a -> IO () -> IO ()) -> IO (IO ()))
-> (a -> IO () -> IO ()) -> IO (IO ())
forall a b. (a -> b) -> a -> b
$ \a
a IO ()
cb -> do
        IORef (Maybe (EventTrigger t a))
reResultTrigger <- Maybe (EventTrigger t a) -> IO (Ref IO (Maybe (EventTrigger t a)))
forall a. a -> IO (Ref IO a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef (Maybe (EventTrigger t a)
 -> IO (Ref IO (Maybe (EventTrigger t a))))
-> Maybe (EventTrigger t a)
-> IO (Ref IO (Maybe (EventTrigger t a)))
forall a b. (a -> b) -> a -> b
$ EventTrigger t a -> Maybe (EventTrigger t a)
forall a. a -> Maybe a
Just EventTrigger t a
t
        Chan [DSum (EventTriggerRef t) TriggerInvocation]
-> [DSum (EventTriggerRef t) TriggerInvocation] -> IO ()
forall a. Chan a -> a -> IO ()
writeChan Chan [DSum (EventTriggerRef t) TriggerInvocation]
events [IORef (Maybe (EventTrigger t a)) -> EventTriggerRef t a
forall t a. IORef (Maybe (EventTrigger t a)) -> EventTriggerRef t a
EventTriggerRef IORef (Maybe (EventTrigger t a))
reResultTrigger EventTriggerRef t a
-> TriggerInvocation a
-> DSum (EventTriggerRef t) TriggerInvocation
forall {k} (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> a -> IO () -> TriggerInvocation a
forall a. a -> IO () -> TriggerInvocation a
TriggerInvocation a
a IO ()
cb]

instance MonadRef m => MonadRef (TriggerEventT t m) where
  type Ref (TriggerEventT t m) = Ref m
  {-# INLINABLE newRef #-}
  newRef :: forall a. a -> TriggerEventT t m (Ref (TriggerEventT t m) a)
newRef = m (Ref m a) -> TriggerEventT t m (Ref m a)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> TriggerEventT t m (Ref m a))
-> (a -> m (Ref m a)) -> a -> TriggerEventT t m (Ref m a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (Ref m a)
forall a. a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef
  {-# INLINABLE readRef #-}
  readRef :: forall a. Ref (TriggerEventT t m) a -> TriggerEventT t m a
readRef = m a -> TriggerEventT t m a
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> TriggerEventT t m a)
-> (Ref m a -> m a) -> Ref m a -> TriggerEventT t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref m a -> m a
forall a. Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef
  {-# INLINABLE writeRef #-}
  writeRef :: forall a. Ref (TriggerEventT t m) a -> a -> TriggerEventT t m ()
writeRef Ref (TriggerEventT t m) a
r = m () -> TriggerEventT t m ()
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> TriggerEventT t m ())
-> (a -> m ()) -> a -> TriggerEventT t m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref m a -> a -> m ()
forall a. Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef Ref m a
Ref (TriggerEventT t m) a
r

instance MonadAtomicRef m => MonadAtomicRef (TriggerEventT t m) where
  {-# INLINABLE atomicModifyRef #-}
  atomicModifyRef :: forall a b.
Ref (TriggerEventT t m) a -> (a -> (a, b)) -> TriggerEventT t m b
atomicModifyRef Ref (TriggerEventT t m) a
r = m b -> TriggerEventT t m b
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> TriggerEventT t m b)
-> ((a -> (a, b)) -> m b) -> (a -> (a, b)) -> TriggerEventT t m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref m a -> (a -> (a, b)) -> m b
forall a b. Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef Ref m a
Ref (TriggerEventT t m) a
r

instance MonadSample t m => MonadSample t (TriggerEventT t m) where
  {-# INLINABLE sample #-}
  sample :: forall a. Behavior t a -> TriggerEventT t m a
sample = m a -> TriggerEventT t m a
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> TriggerEventT t m a)
-> (Behavior t a -> m a) -> Behavior t a -> TriggerEventT t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t a -> m a
forall a. Behavior t a -> m a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

instance MonadHold t m => MonadHold t (TriggerEventT t m) where
  {-# INLINABLE hold #-}
  hold :: forall a. a -> Event t a -> TriggerEventT t m (Behavior t a)
hold a
v0 Event t a
v' = m (Behavior t a) -> TriggerEventT t m (Behavior t a)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Behavior t a) -> TriggerEventT t m (Behavior t a))
-> m (Behavior t a) -> TriggerEventT t m (Behavior t a)
forall a b. (a -> b) -> a -> b
$ a -> Event t a -> m (Behavior t a)
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
v0 Event t a
v'
  {-# INLINABLE holdDyn #-}
  holdDyn :: forall a. a -> Event t a -> TriggerEventT t m (Dynamic t a)
holdDyn a
v0 Event t a
v' = m (Dynamic t a) -> TriggerEventT t m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> TriggerEventT t m (Dynamic t a))
-> m (Dynamic t a) -> TriggerEventT t m (Dynamic t a)
forall a b. (a -> b) -> a -> b
$ a -> Event t a -> m (Dynamic t a)
forall a. a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
v0 Event t a
v'
  {-# INLINABLE holdIncremental #-}
  holdIncremental :: forall p.
Patch p =>
PatchTarget p -> Event t p -> TriggerEventT t m (Incremental t p)
holdIncremental PatchTarget p
v0 Event t p
v' = m (Incremental t p) -> TriggerEventT t m (Incremental t p)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Incremental t p) -> TriggerEventT t m (Incremental t p))
-> m (Incremental t p) -> TriggerEventT t m (Incremental t p)
forall a b. (a -> b) -> a -> b
$ PatchTarget p -> Event t p -> m (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
v0 Event t p
v'
  {-# INLINABLE buildDynamic #-}
  buildDynamic :: forall a. PushM t a -> Event t a -> TriggerEventT t m (Dynamic t a)
buildDynamic PushM t a
a0 = m (Dynamic t a) -> TriggerEventT t m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> TriggerEventT t m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> TriggerEventT t m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PushM t a -> Event t a -> m (Dynamic t a)
forall a. PushM t a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
PushM t a -> Event t a -> m (Dynamic t a)
buildDynamic PushM t a
a0
  {-# INLINABLE headE #-}
  headE :: forall a. Event t a -> TriggerEventT t m (Event t a)
headE = m (Event t a) -> TriggerEventT t m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> TriggerEventT t m (Event t a))
-> (Event t a -> m (Event t a))
-> Event t a
-> TriggerEventT t m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t a -> m (Event t a)
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE
  {-# INLINABLE now #-}
  now :: TriggerEventT t m (Event t ())
now = m (Event t ()) -> TriggerEventT t m (Event t ())
forall (m :: * -> *) a. Monad m => m a -> TriggerEventT t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t ())
forall {k} (t :: k) (m :: * -> *). MonadHold t m => m (Event t ())
now

instance Adjustable t m => Adjustable t (TriggerEventT t m) where
  {-# INLINABLE runWithReplace #-}
  runWithReplace :: forall a b.
TriggerEventT t m a
-> Event t (TriggerEventT t m b)
-> TriggerEventT t m (a, Event t b)
runWithReplace (TriggerEventT ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
a0) Event t (TriggerEventT t m b)
a' = ReaderT
  (Chan [DSum (EventTriggerRef t) TriggerInvocation])
  m
  (a, Event t b)
-> TriggerEventT t m (a, Event t b)
forall t (m :: * -> *) a.
ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> TriggerEventT t m a
TriggerEventT (ReaderT
   (Chan [DSum (EventTriggerRef t) TriggerInvocation])
   m
   (a, Event t b)
 -> TriggerEventT t m (a, Event t b))
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (a, Event t b)
-> TriggerEventT t m (a, Event t b)
forall a b. (a -> b) -> a -> b
$ ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> Event
     t (ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m b)
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (a, Event t b)
forall a b.
ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> Event
     t (ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m b)
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (a, Event t b)
forall t (m :: * -> *) a b.
Adjustable t m =>
m a -> Event t (m b) -> m (a, Event t b)
runWithReplace ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
a0 (Event t (TriggerEventT t m b)
-> Event
     t (ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m b)
forall {k} (t :: k) a b.
(Reflex t, Coercible a b) =>
Event t a -> Event t b
coerceEvent Event t (TriggerEventT t m b)
a')
  {-# INLINABLE traverseIntMapWithKeyWithAdjust #-}
  traverseIntMapWithKeyWithAdjust :: forall v v'.
(Key -> v -> TriggerEventT t m v')
-> IntMap v
-> Event t (PatchIntMap v)
-> TriggerEventT t m (IntMap v', Event t (PatchIntMap v'))
traverseIntMapWithKeyWithAdjust Key -> v -> TriggerEventT t m v'
f IntMap v
dm0 Event t (PatchIntMap v)
dm' = ReaderT
  (Chan [DSum (EventTriggerRef t) TriggerInvocation])
  m
  (IntMap v', Event t (PatchIntMap v'))
-> TriggerEventT t m (IntMap v', Event t (PatchIntMap v'))
forall t (m :: * -> *) a.
ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> TriggerEventT t m a
TriggerEventT (ReaderT
   (Chan [DSum (EventTriggerRef t) TriggerInvocation])
   m
   (IntMap v', Event t (PatchIntMap v'))
 -> TriggerEventT t m (IntMap v', Event t (PatchIntMap v')))
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (IntMap v', Event t (PatchIntMap v'))
-> TriggerEventT t m (IntMap v', Event t (PatchIntMap v'))
forall a b. (a -> b) -> a -> b
$ (Key
 -> v
 -> ReaderT
      (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m v')
-> IntMap v
-> Event t (PatchIntMap v)
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (IntMap v', Event t (PatchIntMap v'))
forall v v'.
(Key
 -> v
 -> ReaderT
      (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m v')
-> IntMap v
-> Event t (PatchIntMap v)
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (IntMap v', Event t (PatchIntMap v'))
forall t (m :: * -> *) v v'.
Adjustable t m =>
(Key -> v -> m v')
-> IntMap v
-> Event t (PatchIntMap v)
-> m (IntMap v', Event t (PatchIntMap v'))
traverseIntMapWithKeyWithAdjust ((v -> TriggerEventT t m v')
-> v
-> ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m v'
forall a b. Coercible a b => a -> b
coerce ((v -> TriggerEventT t m v')
 -> v
 -> ReaderT
      (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m v')
-> (Key -> v -> TriggerEventT t m v')
-> Key
-> v
-> ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m v'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> v -> TriggerEventT t m v'
f) IntMap v
dm0 Event t (PatchIntMap v)
dm'
  {-# INLINABLE traverseDMapWithKeyWithAdjust #-}
  traverseDMapWithKeyWithAdjust :: forall (k :: * -> *) (v :: * -> *) (v' :: * -> *).
GCompare k =>
(forall a. k a -> v a -> TriggerEventT t m (v' a))
-> DMap k v
-> Event t (PatchDMap k v)
-> TriggerEventT t m (DMap k v', Event t (PatchDMap k v'))
traverseDMapWithKeyWithAdjust forall a. k a -> v a -> TriggerEventT t m (v' a)
f DMap k v
dm0 Event t (PatchDMap k v)
dm' = ReaderT
  (Chan [DSum (EventTriggerRef t) TriggerInvocation])
  m
  (DMap k v', Event t (PatchDMap k v'))
-> TriggerEventT t m (DMap k v', Event t (PatchDMap k v'))
forall t (m :: * -> *) a.
ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> TriggerEventT t m a
TriggerEventT (ReaderT
   (Chan [DSum (EventTriggerRef t) TriggerInvocation])
   m
   (DMap k v', Event t (PatchDMap k v'))
 -> TriggerEventT t m (DMap k v', Event t (PatchDMap k v')))
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (DMap k v', Event t (PatchDMap k v'))
-> TriggerEventT t m (DMap k v', Event t (PatchDMap k v'))
forall a b. (a -> b) -> a -> b
$ (forall a.
 k a
 -> v a
 -> ReaderT
      (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a))
-> DMap k v
-> Event t (PatchDMap k v)
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (DMap k v', Event t (PatchDMap k v'))
forall t (m :: * -> *) (k :: * -> *) (v :: * -> *) (v' :: * -> *).
(Adjustable t m, GCompare k) =>
(forall a. k a -> v a -> m (v' a))
-> DMap k v
-> Event t (PatchDMap k v)
-> m (DMap k v', Event t (PatchDMap k v'))
forall (k :: * -> *) (v :: * -> *) (v' :: * -> *).
GCompare k =>
(forall a.
 k a
 -> v a
 -> ReaderT
      (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a))
-> DMap k v
-> Event t (PatchDMap k v)
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (DMap k v', Event t (PatchDMap k v'))
traverseDMapWithKeyWithAdjust ((v a -> TriggerEventT t m (v' a))
-> v a
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a)
forall a b. Coercible a b => a -> b
coerce ((v a -> TriggerEventT t m (v' a))
 -> v a
 -> ReaderT
      (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a))
-> (k a -> v a -> TriggerEventT t m (v' a))
-> k a
-> v a
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k a -> v a -> TriggerEventT t m (v' a)
forall a. k a -> v a -> TriggerEventT t m (v' a)
f) DMap k v
dm0 Event t (PatchDMap k v)
dm'
  {-# INLINABLE traverseDMapWithKeyWithAdjustWithMove #-}
  traverseDMapWithKeyWithAdjustWithMove :: forall (k :: * -> *) (v :: * -> *) (v' :: * -> *).
GCompare k =>
(forall a. k a -> v a -> TriggerEventT t m (v' a))
-> DMap k v
-> Event t (PatchDMapWithMove k v)
-> TriggerEventT t m (DMap k v', Event t (PatchDMapWithMove k v'))
traverseDMapWithKeyWithAdjustWithMove forall a. k a -> v a -> TriggerEventT t m (v' a)
f DMap k v
dm0 Event t (PatchDMapWithMove k v)
dm' = ReaderT
  (Chan [DSum (EventTriggerRef t) TriggerInvocation])
  m
  (DMap k v', Event t (PatchDMapWithMove k v'))
-> TriggerEventT t m (DMap k v', Event t (PatchDMapWithMove k v'))
forall t (m :: * -> *) a.
ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> TriggerEventT t m a
TriggerEventT (ReaderT
   (Chan [DSum (EventTriggerRef t) TriggerInvocation])
   m
   (DMap k v', Event t (PatchDMapWithMove k v'))
 -> TriggerEventT t m (DMap k v', Event t (PatchDMapWithMove k v')))
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (DMap k v', Event t (PatchDMapWithMove k v'))
-> TriggerEventT t m (DMap k v', Event t (PatchDMapWithMove k v'))
forall a b. (a -> b) -> a -> b
$ (forall a.
 k a
 -> v a
 -> ReaderT
      (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a))
-> DMap k v
-> Event t (PatchDMapWithMove k v)
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (DMap k v', Event t (PatchDMapWithMove k v'))
forall t (m :: * -> *) (k :: * -> *) (v :: * -> *) (v' :: * -> *).
(Adjustable t m, GCompare k) =>
(forall a. k a -> v a -> m (v' a))
-> DMap k v
-> Event t (PatchDMapWithMove k v)
-> m (DMap k v', Event t (PatchDMapWithMove k v'))
forall (k :: * -> *) (v :: * -> *) (v' :: * -> *).
GCompare k =>
(forall a.
 k a
 -> v a
 -> ReaderT
      (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a))
-> DMap k v
-> Event t (PatchDMapWithMove k v)
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation])
     m
     (DMap k v', Event t (PatchDMapWithMove k v'))
traverseDMapWithKeyWithAdjustWithMove ((v a -> TriggerEventT t m (v' a))
-> v a
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a)
forall a b. Coercible a b => a -> b
coerce ((v a -> TriggerEventT t m (v' a))
 -> v a
 -> ReaderT
      (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a))
-> (k a -> v a -> TriggerEventT t m (v' a))
-> k a
-> v a
-> ReaderT
     (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m (v' a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k a -> v a -> TriggerEventT t m (v' a)
forall a. k a -> v a -> TriggerEventT t m (v' a)
f) DMap k v
dm0 Event t (PatchDMapWithMove k v)
dm'

-- TODO: Monoid and Semigroup can likely be derived once ReaderT has them.
instance (Monoid a, Applicative m) => Monoid (TriggerEventT t m a) where
  mempty :: TriggerEventT t m a
mempty = a -> TriggerEventT t m a
forall a. a -> TriggerEventT t m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
  mappend :: TriggerEventT t m a -> TriggerEventT t m a -> TriggerEventT t m a
mappend = TriggerEventT t m a -> TriggerEventT t m a -> TriggerEventT t m a
forall a. Semigroup a => a -> a -> a
(<>)

instance (S.Semigroup a, Applicative m) => S.Semigroup (TriggerEventT t m a) where
  <> :: TriggerEventT t m a -> TriggerEventT t m a -> TriggerEventT t m a
(<>) = (a -> a -> a)
-> TriggerEventT t m a
-> TriggerEventT t m a
-> TriggerEventT t m a
forall a b c.
(a -> b -> c)
-> TriggerEventT t m a
-> TriggerEventT t m b
-> TriggerEventT t m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(S.<>)


-- | Retrieve the current 'Chan'; event trigger invocations pushed into it will
-- be fired.
askEvents :: Monad m => TriggerEventT t m (Chan [DSum (EventTriggerRef t) TriggerInvocation])
askEvents :: forall (m :: * -> *) t.
Monad m =>
TriggerEventT
  t m (Chan [DSum (EventTriggerRef t) TriggerInvocation])
askEvents = ReaderT
  (Chan [DSum (EventTriggerRef t) TriggerInvocation])
  m
  (Chan [DSum (EventTriggerRef t) TriggerInvocation])
-> TriggerEventT
     t m (Chan [DSum (EventTriggerRef t) TriggerInvocation])
forall t (m :: * -> *) a.
ReaderT (Chan [DSum (EventTriggerRef t) TriggerInvocation]) m a
-> TriggerEventT t m a
TriggerEventT ReaderT
  (Chan [DSum (EventTriggerRef t) TriggerInvocation])
  m
  (Chan [DSum (EventTriggerRef t) TriggerInvocation])
forall r (m :: * -> *). MonadReader r m => m r
ask