{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
module Observe.Event.Explicit
  ( Event,
    hoistEvent,
    
    addField,
    reference,
    
    NewEventArgs (..),
    emitImmediateEvent,
    allocateEvent,
    allocateEventArgs,
    withEvent,
    withEventArgs,
    
    EventBackend,
    
    subEventBackend,
    causedEventBackend,
    hoistEventBackend,
    narrowEventBackend,
    InjectSelector,
    injectSelector,
    idInjectSelector,
    setAncestorEventBackend,
    setInitialCauseEventBackend,
    
    unitEventBackend,
    pairEventBackend,
    noopEventBackend,
    
    
    
    finalize,
    newEvent,
  )
where
import Control.Monad.With
import Data.Exceptable
import Data.GeneralAllocate
import Observe.Event.Backend
allocateEvent ::
  (Monad m, Exceptable e) =>
  EventBackend m r s ->
  forall f.
  s f ->
  GeneralAllocate m e () releaseArg (Event m r f)
allocateEvent :: forall (m :: * -> *) e r (s :: * -> *) releaseArg.
(Monad m, Exceptable e) =>
EventBackend m r s
-> forall f. s f -> GeneralAllocate m e () releaseArg (Event m r f)
allocateEvent EventBackend m r s
backend = forall (m :: * -> *) e r (s :: * -> *) releaseArg.
(Monad m, Exceptable e) =>
EventBackend m r s
-> forall f.
   NewEventArgs r s f
   -> GeneralAllocate m e () releaseArg (Event m r f)
allocateEventArgs EventBackend m r s
backend forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: * -> *) f r. s f -> NewEventArgs r s f
simpleNewEventArgs
allocateEventArgs ::
  (Monad m, Exceptable e) =>
  EventBackend m r s ->
  forall f.
  NewEventArgs r s f ->
  GeneralAllocate m e () releaseArg (Event m r f)
allocateEventArgs :: forall (m :: * -> *) e r (s :: * -> *) releaseArg.
(Monad m, Exceptable e) =>
EventBackend m r s
-> forall f.
   NewEventArgs r s f
   -> GeneralAllocate m e () releaseArg (Event m r f)
allocateEventArgs EventBackend m r s
backend NewEventArgs r s f
args = forall (m :: * -> *) e releaseReturn releaseArg a.
((forall x. m x -> m x)
 -> m (GeneralAllocated m e releaseReturn releaseArg a))
-> GeneralAllocate m e releaseReturn releaseArg a
GeneralAllocate forall a b. (a -> b) -> a -> b
$ \forall x. m x -> m x
restore -> do
  Event m r f
ev <- forall x. m x -> m x
restore forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r (s :: * -> *).
EventBackend m r s
-> forall f. NewEventArgs r s f -> m (Event m r f)
newEvent EventBackend m r s
backend NewEventArgs r s f
args
  let release :: GeneralReleaseType e a -> m ()
release (ReleaseFailure e
e) = forall (m :: * -> *) r f.
Event m r f -> Maybe SomeException -> m ()
finalize Event m r f
ev forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall e. Exceptable e => e -> SomeException
toSomeException e
e
      release (ReleaseSuccess a
_) = forall (m :: * -> *) r f.
Event m r f -> Maybe SomeException -> m ()
finalize Event m r f
ev forall a. Maybe a
Nothing
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) e releaseReturn releaseArg a.
a
-> (GeneralReleaseType e releaseArg -> m releaseReturn)
-> GeneralAllocated m e releaseReturn releaseArg a
GeneralAllocated Event m r f
ev forall {e} {a}. Exceptable e => GeneralReleaseType e a -> m ()
release
withEvent ::
  (MonadWithExceptable m) =>
  EventBackend m r s ->
  forall f.
  
  s f ->
  (Event m r f -> m a) ->
  m a
withEvent :: forall (m :: * -> *) r (s :: * -> *) a.
MonadWithExceptable m =>
EventBackend m r s -> forall f. s f -> (Event m r f -> m a) -> m a
withEvent EventBackend m r s
backend = forall (m :: * -> *) b a.
MonadWith m =>
With m b a -> (a -> m b) -> m b
generalWith forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) e r (s :: * -> *) releaseArg.
(Monad m, Exceptable e) =>
EventBackend m r s
-> forall f. s f -> GeneralAllocate m e () releaseArg (Event m r f)
allocateEvent EventBackend m r s
backend
withEventArgs ::
  (MonadWithExceptable m) =>
  EventBackend m r s ->
  forall f.
  
  NewEventArgs r s f ->
  (Event m r f -> m a) ->
  m a
withEventArgs :: forall (m :: * -> *) r (s :: * -> *) a.
MonadWithExceptable m =>
EventBackend m r s
-> forall f. NewEventArgs r s f -> (Event m r f -> m a) -> m a
withEventArgs EventBackend m r s
backend = forall (m :: * -> *) b a.
MonadWith m =>
With m b a -> (a -> m b) -> m b
generalWith forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) e r (s :: * -> *) releaseArg.
(Monad m, Exceptable e) =>
EventBackend m r s
-> forall f.
   NewEventArgs r s f
   -> GeneralAllocate m e () releaseArg (Event m r f)
allocateEventArgs EventBackend m r s
backend
subEventBackend ::
  (Functor m) =>
  
  InjectSelector s t ->
  
  Event m r f ->
  EventBackend m r t ->
  EventBackend m r s
subEventBackend :: forall (m :: * -> *) (s :: * -> *) (t :: * -> *) r f.
Functor m =>
InjectSelector s t
-> Event m r f -> EventBackend m r t -> EventBackend m r s
subEventBackend InjectSelector s t
inj Event m r f
ev =
  forall (m :: * -> *) (s :: * -> *) (t :: * -> *) r.
Functor m =>
InjectSelector s t -> EventBackend m r t -> EventBackend m r s
narrowEventBackend InjectSelector s t
inj
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) (s :: * -> *).
r -> EventBackend m r s -> EventBackend m r s
setAncestorEventBackend (forall (m :: * -> *) r f. Event m r f -> r
reference Event m r f
ev)
causedEventBackend ::
  (Functor m) =>
  
  InjectSelector s t ->
  
  [Event m r f] ->
  EventBackend m r t ->
  EventBackend m r s
causedEventBackend :: forall (m :: * -> *) (s :: * -> *) (t :: * -> *) r f.
Functor m =>
InjectSelector s t
-> [Event m r f] -> EventBackend m r t -> EventBackend m r s
causedEventBackend InjectSelector s t
inj [Event m r f]
evs =
  forall (m :: * -> *) (s :: * -> *) (t :: * -> *) r.
Functor m =>
InjectSelector s t -> EventBackend m r t -> EventBackend m r s
narrowEventBackend InjectSelector s t
inj
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) (s :: * -> *).
[r] -> EventBackend m r s -> EventBackend m r s
setInitialCauseEventBackend (forall a b. (a -> b) -> [a] -> [b]
map forall (m :: * -> *) r f. Event m r f -> r
reference [Event m r f]
evs)