{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE CPP #-}
-- |
-- Description : Core interface for instrumentation with eventuo11y
-- Copyright   : Copyright 2022 Shea Levy.
-- License     : Apache-2.0
-- Maintainer  : shea@shealevy.com
--
-- This is the primary module needed to instrument code with eventuo11y.
--
-- Instrumentors should first define selector types and field types
-- appropriate to the unit of code they're instrumenting:
--
-- Selectors are values which designate the general category of event
-- being created, parameterized by the type of fields that can be added to it.
-- For example, a web service's selector type may have a @ServicingRequest@
-- constructor, whose field type includes a @ResponseCode@ constructor which
-- records the HTTP status code. Selectors are intended to be of a domain-specific
-- type per unit of functionality within an instrumented codebase, implemented as a GADT
-- (but see t'Observe.Event.Dynamic.DynamicEventSelector' for a generic option).
--
-- Fields make up the basic data captured in an event. They should be added
-- to an 'Event' as the code progresses through various phases of work, and can
-- be both milestone markers ("we got this far in the process") or more detailed
-- instrumentation ("we've processed N records"). They are intended to be of a
-- domain-specific type per unit of functionality within an instrumented codebase
-- (but see t'Observe.Event.Dynamic.DynamicField' for a generic option).
--
-- Instrumentation then centers around 'Event's, populated using the
-- <#g:eventmanip event manipulation functions>. 'Event's are initialized
-- with 'EventBackend's, typically via the
-- <#g:resourcesafe resource-safe event allocation functions>.
--
-- Depending on which 'EventBackend's may end up consuming the 'Event's,
-- instrumentors will also need to define renderers for their selectors
-- and fields. For example, they may need to implement values of types
--  t'Observe.Event.Render.JSON.RenderSelectorJSON' and
--  t'Observe.Event.Render.JSON.RenderFieldJSON' to use JSON rendering 'EventBackend's.
module Observe.Event
  ( Event,
    hoistEvent,

    -- * Event manipulation #eventmanip#
    reference,
    addField,
    addParent,
    addProximate,

    -- * Resource-safe event allocation #resourcesafe#
    withEvent,
    withSubEvent,

    -- ** MonadMask variants
    withEventMask,
    withSubEventMask,

    -- ** Acquire/MonadResource variants
    acquireEvent,
    acquireSubEvent,

    -- * 'EventBackend's
    EventBackend,
    subEventBackend,
    causedEventBackend,
    unitEventBackend,
    pairEventBackend,
    hoistEventBackend,
    narrowEventBackend,
    narrowEventBackend',

    -- * Primitive 'Event' resource management.

    -- | Prefer the [resource-safe event allocation functions](#g:resourcesafe)
    -- to these when possible.
    finalize,
    failEvent,
    newEvent,
    newSubEvent,
  )
where

import Control.Exception.Safe
import Control.Monad.Cleanup
import Control.Monad.IO.Unlift
import Data.Acquire
import Observe.Event.Backend
import Observe.Event.BackendModification

-- | An instrumentation event.
--
-- 'Event's are the core of the instrumenting user's interface
-- to eventuo11y. Typical usage would be to create an 'Event'
-- from an 'EventBackend' with 'withEvent', or as a child of
-- an another 'Event' with 'withSubEvent', and add fields to
-- the 'Event' at appropriate points in your code with
-- 'addField'.
--
-- [@m@]: The monad we're instrumenting in.
-- [@r@]: The type of event references. See 'reference'.
-- [@s@]: The type of event selectors for child events. See 'EventBackend'.
-- [@f@]: The type of fields on this event. See 'addField'.
data Event m r s f = Event
  { -- | The 'EventBackend' this 'Event' was generated from.
    forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
backend :: !(EventBackend m r s),
    -- | The underlying 'EventImpl' implementing the event functionality.
    forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
impl :: !(EventImpl m r f),
    -- | A 'OnceFlag' to ensure we only finish ('finalize' or 'failEvent') once.
    forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
finishFlag :: !(OnceFlag m)
  }

-- | Hoist an 'Event' along a given natural transformation into a new monad.
hoistEvent :: (Functor m, Functor n) => (forall x. m x -> n x) -> Event m r s f -> Event n r s f
hoistEvent :: forall (m :: * -> *) (n :: * -> *) r (s :: * -> *) f.
(Functor m, Functor n) =>
(forall x. m x -> n x) -> Event m r s f -> Event n r s f
hoistEvent forall x. m x -> n x
nt Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..} =
  Event
    { backend :: EventBackend n r s
backend = forall (m :: * -> *) (n :: * -> *) r (s :: * -> *).
(Functor m, Functor n) =>
(forall x. m x -> n x) -> EventBackend m r s -> EventBackend n r s
hoistEventBackend forall x. m x -> n x
nt EventBackend m r s
backend,
      impl :: EventImpl n r f
impl = forall (m :: * -> *) (n :: * -> *) r f.
(forall x. m x -> n x) -> EventImpl m r f -> EventImpl n r f
hoistEventImpl forall x. m x -> n x
nt EventImpl m r f
impl,
      finishFlag :: OnceFlag n
finishFlag = forall (f :: * -> *) (g :: * -> *).
(forall x. f x -> g x) -> OnceFlag f -> OnceFlag g
hoistOnceFlag forall x. m x -> n x
nt OnceFlag m
finishFlag
    }

-- | Obtain a reference to an 'Event'.
--
-- References are used to link 'Event's together, either in
-- parent-child relationships with 'addParent' or in
-- cause-effect relationships with 'addProximate'.
--
-- References can live past when an event has been 'finalize'd or
-- 'failEvent'ed.
--
-- Code being instrumented should always have @r@ as an unconstrained
-- type parameter, both because it is an implementation concern for
-- 'EventBackend's and because references are backend-specific and it
-- would be an error to reference an event in one backend from an event
-- in a different backend.
reference :: Event m r s f -> r
reference :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> r
reference (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) = forall (m :: * -> *) r f. EventImpl m r f -> r
referenceImpl EventImpl m r f
impl

-- | Add a field to an 'Event'.
--
-- Fields make up the basic data captured in an event. They should be added
-- to an 'Event' as the code progresses through various phases of work, and can
-- be both milestone markers ("we got this far in the process") or more detailed
-- instrumentation ("we've processed N records").
--
-- They are intended to be of a domain specific type per unit of functionality
-- within an instrumented codebase (but see t'Observe.Event.Dynamic.DynamicField'
-- for a generic option).
addField ::
  Event m r s f ->
  -- | The field to add to the event.
  f ->
  m ()
addField :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> f -> m ()
addField (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) = forall (m :: * -> *) r f. EventImpl m r f -> f -> m ()
addFieldImpl EventImpl m r f
impl

-- | Mark another 'Event' as a parent of this 'Event'.
addParent ::
  Event m r s f ->
  -- | A reference to the parent, obtained via 'reference'.
  r ->
  m ()
addParent :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> r -> m ()
addParent (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) = forall (m :: * -> *) r f. EventImpl m r f -> r -> m ()
addParentImpl EventImpl m r f
impl

-- | Mark another 'Event' as a proximate cause of this 'Event'.
addProximate ::
  Event m r s f ->
  -- | A reference to the proximate cause, obtained via 'reference'.
  r ->
  m ()
addProximate :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> r -> m ()
addProximate (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) = forall (m :: * -> *) r f. EventImpl m r f -> r -> m ()
addProximateImpl EventImpl m r f
impl

-- | Mark an 'Event' as finished.
--
-- In normal usage, this should be automatically called via the use of
-- the [resource-safe event allocation functions](#g:resourcesafe).
--
-- This is a no-op if the 'Event' has already been 'finalize'd or
-- 'failEvent'ed. As a result, it is likely pointless to call
-- 'addField', 'addParent', or 'addProximate' after this call,
-- though it still may be reasonable to call 'reference'.
finalize :: (Monad m) => Event m r s f -> m ()
finalize :: forall (m :: * -> *) r (s :: * -> *) f.
Monad m =>
Event m r s f -> m ()
finalize (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) = forall (m :: * -> *). Monad m => OnceFlag m -> m () -> m ()
runOnce OnceFlag m
finishFlag forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r f. EventImpl m r f -> m ()
finalizeImpl EventImpl m r f
impl

-- | Mark an 'Event' as having failed due to an 'Exception'.
--
-- In normal usage, this should be automatically called via the use of
-- the [resource-safe event allocation functions](#g:resourcesafe).
--
-- This is a no-op if the 'Event' has already been 'finalize'd or
-- 'failEvent'ed. As a result, it is likely pointless to call
-- 'addField', 'addParent', or 'addProximate' after this call,
-- though it still may be reasonable to call 'reference'.
failEvent :: (Monad m) => Event m r s f -> SomeException -> m ()
failEvent :: forall (m :: * -> *) r (s :: * -> *) f.
Monad m =>
Event m r s f -> SomeException -> m ()
failEvent (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) = forall (m :: * -> *). Monad m => OnceFlag m -> m () -> m ()
runOnce OnceFlag m
finishFlag forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) r f. EventImpl m r f -> SomeException -> m ()
failImpl EventImpl m r f
impl

-- | Create a new 'Event', selected by the given selector.
--
-- The selector specifies the category of new event we're creating, as well
-- as the type of fields that can be added to it (with 'addField').
--
-- Selectors are intended to be of a domain specific type per unit of
-- functionality within an instrumented codebase, implemented as a GADT
-- (but see t'Observe.Event.Dynamic.DynamicEventSelector' for a generic option).
--
-- Consider the [resource-safe event allocation functions](#g:resourcesafe) instead
-- of calling this directly.
newEvent ::
  (Applicative m) =>
  EventBackend m r s ->
  forall f.
  -- | The event selector.
  s f ->
  m (Event m r s f)
newEvent :: forall (m :: * -> *) r (s :: * -> *).
Applicative m =>
EventBackend m r s -> forall f. s f -> m (Event m r s f)
newEvent backend :: EventBackend m r s
backend@(EventBackend {m (OnceFlag m)
forall f. s f -> m (EventImpl m r f)
newOnceFlag :: forall (m :: * -> *) r (s :: * -> *).
EventBackend m r s -> m (OnceFlag m)
newEventImpl :: forall (m :: * -> *) r (s :: * -> *).
EventBackend m r s -> forall f. s f -> m (EventImpl m r f)
newOnceFlag :: m (OnceFlag m)
newEventImpl :: forall f. s f -> m (EventImpl m r f)
..}) s f
sel = do
  EventImpl m r f
impl <- forall f. s f -> m (EventImpl m r f)
newEventImpl s f
sel
  OnceFlag m
finishFlag <- m (OnceFlag m)
newOnceFlag
  pure Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
..}

-- | Create a new 'Event' as a child of the given 'Event', selected by the given selector.
--
-- The selector specifies the category of new event we're creating, as well
-- as the type of fields that can be added to it (with 'addField').
--
-- Selectors are intended to be of a domain specific type per unit of
-- functionality within an instrumented codebase, implemented as a GADT
-- (but see t'Observe.Event.Dynamic.DynamicEventSelector' for a generic option).
--
-- Consider the [resource-safe event allocation functions](#g:resourcesafe) instead
-- of calling this directly.
newSubEvent ::
  (Monad m) =>
  -- | The parent event.
  Event m r s f ->
  forall f'.
  -- | The child event selector.
  s f' ->
  m (Event m r s f')
newSubEvent :: forall (m :: * -> *) r (s :: * -> *) f.
Monad m =>
Event m r s f -> forall f'. s f' -> m (Event m r s f')
newSubEvent (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) s f'
sel = do
  Event m r s f'
child <- forall (m :: * -> *) r (s :: * -> *).
Applicative m =>
EventBackend m r s -> forall f. s f -> m (Event m r s f)
newEvent EventBackend m r s
backend s f'
sel
  forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> r -> m ()
addParent Event m r s f'
child forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r f. EventImpl m r f -> r
referenceImpl EventImpl m r f
impl
  pure Event m r s f'
child

-- | Run an action with a new 'Event', selected by the given selector.
--
-- The selector specifies the category of new event we're creating, as well
-- as the type of fields that can be added to it (with 'addField').
--
-- Selectors are intended to be of a domain specific type per unit of
-- functionality within an instrumented codebase, implemented as a GADT
-- (but see t'Observe.Event.Dynamic.DynamicEventSelector' for a generic option).
--
-- The 'Event' is automatically 'finalize'd (or, if appropriate, 'failEvent'ed)
-- at the end of the function it's passed to.
withEvent ::
  (MonadCleanup m) =>
  EventBackend m r s ->
  forall f.
  -- | The event selector.
  s f ->
  (Event m r s f -> m a) ->
  m a
withEvent :: forall (m :: * -> *) r (s :: * -> *) a.
MonadCleanup m =>
EventBackend m r s
-> forall f. s f -> (Event m r s f -> m a) -> m a
withEvent EventBackend m r s
backend s f
sel Event m r s f -> m a
go = forall (m :: * -> *) a b c.
MonadCleanup m =>
m a -> (Maybe SomeException -> a -> m b) -> (a -> m c) -> m c
withCleanup (forall (m :: * -> *) r (s :: * -> *).
Applicative m =>
EventBackend m r s -> forall f. s f -> m (Event m r s f)
newEvent EventBackend m r s
backend s f
sel) forall {m :: * -> *} {r} {s :: * -> *} {f}.
Monad m =>
Maybe SomeException -> Event m r s f -> m ()
cleanup Event m r s f -> m a
go
  where
    cleanup :: Maybe SomeException -> Event m r s f -> m ()
cleanup Maybe SomeException
Nothing = forall (m :: * -> *) r (s :: * -> *) f.
Monad m =>
Event m r s f -> m ()
finalize
    cleanup (Just SomeException
e) = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) r (s :: * -> *) f.
Monad m =>
Event m r s f -> SomeException -> m ()
failEvent SomeException
e

-- | Run an action with a new 'Event' as a child of the given 'Event', selected by the given selector.
--
-- The selector specifies the category of new event we're creating, as well
-- as the type of fields that can be added to it (with 'addField').
--
-- Selectors are intended to be of a domain specific type per unit of
-- functionality within an instrumented codebase, implemented as a GADT
-- (but see t'Observe.Event.Dynamic.DynamicEventSelector' for a generic option).
--
-- The 'Event' is automatically 'finalize'd (or, if appropriate, 'failEvent'ed)
-- at the end of the function it's passed to.
withSubEvent ::
  (MonadCleanup m) =>
  -- | The parent 'Event'.
  Event m r s f ->
  forall f'.
  -- | The child event selector.
  s f' ->
  (Event m r s f' -> m a) ->
  m a
withSubEvent :: forall (m :: * -> *) r (s :: * -> *) f a.
MonadCleanup m =>
Event m r s f -> forall f'. s f' -> (Event m r s f' -> m a) -> m a
withSubEvent (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) s f'
sel Event m r s f' -> m a
go = forall (m :: * -> *) r (s :: * -> *) a.
MonadCleanup m =>
EventBackend m r s
-> forall f. s f -> (Event m r s f -> m a) -> m a
withEvent EventBackend m r s
backend s f'
sel forall a b. (a -> b) -> a -> b
$ \Event m r s f'
child -> do
  forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> r -> m ()
addParent Event m r s f'
child forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r f. EventImpl m r f -> r
referenceImpl EventImpl m r f
impl
  Event m r s f' -> m a
go Event m r s f'
child

-- TODO Implement in terms of withEvent + CleanupFromMask

-- | 'withEvent' in 'MonadMask'
withEventMask ::
  forall m r s.
  (MonadMask m) =>
  EventBackend m r s ->
  forall f.
  -- | The event selector.
  s f ->
  forall a.
  (Event m r s f -> m a) ->
  m a
withEventMask :: forall (m :: * -> *) r (s :: * -> *).
MonadMask m =>
EventBackend m r s
-> forall f. s f -> forall a. (Event m r s f -> m a) -> m a
withEventMask EventBackend m r s
backend s f
sel Event m r s f -> m a
go = forall (m :: * -> *) a b c.
MonadMask m =>
m a -> (Maybe SomeException -> a -> m b) -> (a -> m c) -> m c
bracketWithError (forall (m :: * -> *) r (s :: * -> *).
Applicative m =>
EventBackend m r s -> forall f. s f -> m (Event m r s f)
newEvent EventBackend m r s
backend s f
sel) forall {m :: * -> *} {r} {s :: * -> *} {f}.
Monad m =>
Maybe SomeException -> Event m r s f -> m ()
release Event m r s f -> m a
go
  where
    release :: Maybe SomeException -> Event m r s f -> m ()
release Maybe SomeException
Nothing = forall (m :: * -> *) r (s :: * -> *) f.
Monad m =>
Event m r s f -> m ()
finalize
    release (Just SomeException
e) = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) r (s :: * -> *) f.
Monad m =>
Event m r s f -> SomeException -> m ()
failEvent SomeException
e

-- TODO implement in terms of withSubEvent + CleanupFromMask

-- | 'withSubEvent' in 'MonadMask'
withSubEventMask ::
  (MonadMask m) =>
  -- | The parent 'Event'.
  Event m r s f ->
  forall f'.
  -- | The child event selector.
  s f' ->
  (Event m r s f' -> m a) ->
  m a
withSubEventMask :: forall (m :: * -> *) r (s :: * -> *) f a.
MonadMask m =>
Event m r s f -> forall f'. s f' -> (Event m r s f' -> m a) -> m a
withSubEventMask (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) s f'
sel Event m r s f' -> m a
go = forall (m :: * -> *) r (s :: * -> *).
MonadMask m =>
EventBackend m r s
-> forall f. s f -> forall a. (Event m r s f -> m a) -> m a
withEventMask EventBackend m r s
backend s f'
sel forall a b. (a -> b) -> a -> b
$ \Event m r s f'
child -> do
  forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> r -> m ()
addParent Event m r s f'
child forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r f. EventImpl m r f -> r
referenceImpl EventImpl m r f
impl
  Event m r s f' -> m a
go Event m r s f'
child

-- | An 'Acquire' variant of 'withEvent', usable in a t'Control.Monad.Trans.Resource.MonadResource' with 'allocateAcquire'.
--
-- Prior to @resourcet@ version @1.3.0@, exceptional exit will 'failEvent' with 'AbortException'.
acquireEvent ::
  (MonadUnliftIO m) =>
  EventBackend m r s ->
  forall f.
  -- | The event selector.
  s f ->
  m (Acquire (Event m r s f))
acquireEvent :: forall (m :: * -> *) r (s :: * -> *).
MonadUnliftIO m =>
EventBackend m r s -> forall f. s f -> m (Acquire (Event m r s f))
acquireEvent EventBackend m r s
backend s f
sel = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
runInIO ->
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
    forall a. IO a -> (a -> ReleaseType -> IO ()) -> Acquire a
mkAcquireType
      (forall a. m a -> IO a
runInIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r (s :: * -> *).
Applicative m =>
EventBackend m r s -> forall f. s f -> m (Event m r s f)
newEvent EventBackend m r s
backend s f
sel)
      (forall {m :: * -> *} {b} {r} {s :: * -> *} {f}.
Monad m =>
(m () -> b) -> Event m r s f -> ReleaseType -> b
release forall a. m a -> IO a
runInIO)
  where
#if MIN_VERSION_resourcet(1,3,0)
    release :: (m () -> b) -> Event m r s f -> ReleaseType -> b
release m () -> b
runInIO Event m r s f
ev (ReleaseExceptionWith SomeException
e) = m () -> b
runInIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r (s :: * -> *) f.
Monad m =>
Event m r s f -> SomeException -> m ()
failEvent Event m r s f
ev SomeException
e
#else
    release runInIO ev ReleaseException = runInIO . failEvent ev $ toException AbortException
#endif
    release m () -> b
runInIO Event m r s f
ev ReleaseType
_ = m () -> b
runInIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r (s :: * -> *) f.
Monad m =>
Event m r s f -> m ()
finalize Event m r s f
ev

-- | An 'Acquire' variant of 'withSubEvent', usable in a t'Control.Monad.Trans.Resource.MonadResource' with 'allocateAcquire'.
--
-- Prior to @resourcet@ version @1.3.0@, exceptional exit will 'failEvent' with 'AbortException'.
acquireSubEvent ::
  (MonadUnliftIO m) =>
  -- | The parent event.
  Event m r s f ->
  forall f'.
  -- | The child event selector.
  s f' ->
  m (Acquire (Event m r s f'))
acquireSubEvent :: forall (m :: * -> *) r (s :: * -> *) f.
MonadUnliftIO m =>
Event m r s f -> forall f'. s f' -> m (Acquire (Event m r s f'))
acquireSubEvent (Event {OnceFlag m
EventImpl m r f
EventBackend m r s
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r s
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..}) s f'
sel = do
  Acquire (Event m r s f')
childAcq <- forall (m :: * -> *) r (s :: * -> *).
MonadUnliftIO m =>
EventBackend m r s -> forall f. s f -> m (Acquire (Event m r s f))
acquireEvent EventBackend m r s
backend s f'
sel
  forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
runInIO -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ do
    Event m r s f'
child <- Acquire (Event m r s f')
childAcq
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. m a -> IO a
runInIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> r -> m ()
addParent Event m r s f'
child forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r f. EventImpl m r f -> r
referenceImpl EventImpl m r f
impl
    pure Event m r s f'
child

-- | An 'EventBackend' where every otherwise parentless event will be marked
-- as a child of the given 'Event'.
subEventBackend ::
  (Monad m) =>
  -- | Bring selectors from the new backend into the parent event's backend.
  --
  -- Use 'id' here plus 'narrowEventBackend'' if you need a more general mapping
  -- between selector types.
  (forall f'. s f' -> t f') ->
  -- | The parent event.
  Event m r t f ->
  EventBackend m r s
subEventBackend :: forall (m :: * -> *) (s :: * -> *) (t :: * -> *) r f.
Monad m =>
(forall f'. s f' -> t f') -> Event m r t f -> EventBackend m r s
subEventBackend forall f'. s f' -> t f'
inj Event {OnceFlag m
EventImpl m r f
EventBackend m r t
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r t
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..} =
  forall (m :: * -> *) (s :: * -> *) (t :: * -> *) r.
Functor m =>
(forall f. s f -> t f) -> EventBackend m r t -> EventBackend m r s
narrowEventBackend forall f'. s f' -> t f'
inj forall a b. (a -> b) -> a -> b
$
    forall (m :: * -> *) r r' (s :: * -> *).
Monad m =>
EventBackendModifiers r r'
-> EventBackend m r s -> EventBackend m r' s
modifyEventBackend (forall r. r -> EventBackendModifiers r r
setAncestor forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r f. EventImpl m r f -> r
referenceImpl EventImpl m r f
impl) EventBackend m r t
backend

-- | An 'EventBackend' where every otherwise causeless event will be marked
-- as caused by the given 'Event'.
causedEventBackend ::
  (Monad m) =>
  -- | Bring selectors from the new backend into the causing event's backend.
  --
  -- Use 'id' here plus 'narrowEventBackend'' if you need a more general mapping
  -- between selector types.
  (forall f'. s f' -> t f') ->
  -- | The causing event.
  Event m r t f ->
  EventBackend m r s
causedEventBackend :: forall (m :: * -> *) (s :: * -> *) (t :: * -> *) r f.
Monad m =>
(forall f'. s f' -> t f') -> Event m r t f -> EventBackend m r s
causedEventBackend forall f'. s f' -> t f'
inj Event {OnceFlag m
EventImpl m r f
EventBackend m r t
finishFlag :: OnceFlag m
impl :: EventImpl m r f
backend :: EventBackend m r t
finishFlag :: forall (m :: * -> *) r (s :: * -> *) f. Event m r s f -> OnceFlag m
impl :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventImpl m r f
backend :: forall (m :: * -> *) r (s :: * -> *) f.
Event m r s f -> EventBackend m r s
..} =
  forall (m :: * -> *) (s :: * -> *) (t :: * -> *) r.
Functor m =>
(forall f. s f -> t f) -> EventBackend m r t -> EventBackend m r s
narrowEventBackend forall f'. s f' -> t f'
inj forall a b. (a -> b) -> a -> b
$
    forall (m :: * -> *) r r' (s :: * -> *).
Monad m =>
EventBackendModifiers r r'
-> EventBackend m r s -> EventBackend m r' s
modifyEventBackend (forall r. r -> EventBackendModifiers r r
setInitialCause forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r f. EventImpl m r f -> r
referenceImpl EventImpl m r f
impl) EventBackend m r t
backend