
    Yh                          S SK JrJr  S SKJr  S SKJrJrJrJ	r	J
r
JrJrJrJr  S SKrS SKJrJr  \rS/r\R*                  r " S S\5      rg)    )AbstractAsyncContextManagerasynccontextmanager)TracebackType)	AnyAsyncGenerator	AwaitableCallablecastDictOptionalTupleTypeN)EventEmitter	PyeeErrorTrioEventEmitterc                   4  ^  \ rS rSrSr  SS\S\\   S\S   4U 4S jjjrS\S	\	S
\
\S4   S\\\4   S\	/ \S   4   4
S jrS\S	\	S
\
\S4   S\\\4   SS4
S jr\S\S\S   4S j5       rS\SS 4S jrS\S\\\      S\\   S\\   S\\   4
S jrSrU =r$ )r      a  An event emitter class which can run trio tasks in a trio nursery.

By default, this class will lazily create both a nursery manager (the
object returned from `trio.open_nursery()` and a nursery (the object
yielded by using the nursery manager as an async context manager). It is
also possible to supply an existing nursery manager via the `manager`
argument, or an existing nursery via the `nursery` argument.

Instances of TrioEventEmitter are themselves async context managers, so
that they may manage the lifecycle of the underlying trio nursery. For
example, typical usage of this library may look something like this::

```py
async with TrioEventEmitter() as ee:
    # Underlying nursery is instantiated and ready to go
    @ee.on('data')
    async def handler(data):
        print(data)

    ee.emit('event')

# Underlying nursery and manager have been cleaned up
```

Unlike the case with the EventEmitter, all exceptions raised by event
handlers are automatically emitted on the `error` event. This is
important for trio coroutines specifically but is also handled for
synchronous functions for consistency.

For trio coroutine event handlers, calling emit is non-blocking. In other
words, you should not attempt to await emit; the coroutine is scheduled
in a fire-and-forget fashion.
Nselfnurserymanagerz)AbstractAsyncContextManager[trio.Nursery]c                    > [         [        U ]  5         S U l        S U l        U(       a  U(       a  [        S5      eXl        g U(       a  X l        g [        R                  " 5       U l        g )Nz?You may either pass a nursery or a nursery manager but not both)superr   __init___nursery_managerr   trioopen_nursery)r   r   r   	__class__s      ;/var/www/html/env/lib/python3.13/site-packages/pyee/trio.pyr   TrioEventEmitter.__init__@   sV    
 	.0+/OSX  $M#M --/DM    fargs.kwargsreturnc                 $   ^ ^^^ SUUUU 4S jjnU$ )Nc                     >#     T" T0 TD6I S h  vN   g  N! [          a  n TR                  SU 5         S n A g S n A ff = f7f)Nerror)	Exceptionemit)excr#   r"   r$   r   s    r   runner.TrioEventEmitter._async_runner.<locals>.runnerY   s>     ((((( (		'3''(s0   A  A 
>9A>A)r%   N )r   r"   r#   r$   r,   s   ```` r   _async_runnerTrioEventEmitter._async_runnerS   s    	( 	( r!   c                     U R                   (       d  [        S5      eU R                   R                  U R                  XU5      5        g )NzUninitialized trio nursery)r   r   
start_soonr/   )r   r"   r#   r$   s       r   	_emit_runTrioEventEmitter._emit_runa   s6     }}899  !3!3AV!DEr!   )r   Nc                   #    U R                   b  U 7v   gU R                  b1  U R                   ISh  vN nXl         U 7v   SSS5      ISh  vN   g[        S5      e N+ N! , ISh  vN  (       d  f       g= f7f)zReturns an async contextmanager which manages the underlying
nursery to the EventEmitter. The `TrioEventEmitter`'s
async context management methods are implemented using this
function, but it may also be used directly for clarity.
Nz(Uninitialized nursery or nursery manager)r   r   r   )r   r   s     r   contextTrioEventEmitter.contextk   sW      ==$J]]&}}} '
 %}} FGG	 %}}}sD   1A=AA=A#A=A!A=!A=#A:)A,*A:6A=c                    #    U R                  5       U l        [        [        U R                  5      R	                  5       I S h  vN $  N7f)N)r6   _contextr
   r   
__aenter__)r   s    r   r:   TrioEventEmitter.__aenter__}   s7     LLN 	 #t}}-88::::s   AA
AA
typevalue	tracebackc                    #    U R                   c  [        S5      eU R                   R                  XU5      I S h  vN nS U l         S U l        S U l        U$  N7f)Nz(Attempting to exit uninitialized context)r9   r   	__aexit__r   r   )r   r<   r=   r>   rvs        r   r@   TrioEventEmitter.__aexit__   sV      == FGG==**4	BB		 Cs   8AAA)r9   r   r   )NN)__name__
__module____qualname____firstlineno____doc__Selfr   Nurseryr   r	   r   r   r   strr   r/   r3   r   r   r6   r:   r   BaseExceptionr   boolr@   __static_attributes____classcell__)r   s   @r   r   r      sl    H &*IM00'"0 EF0 0& CHo S#X	
 
"io%	&FFF CHoF S#X	F
 
F HH	0	1H H";t ;(: ;tM*+ & M*	
 
$ r!   )
contextlibr   r   typesr   typingr   r   r   r	   r
   r   r   r   r   r   	pyee.baser   r   rH   __all__rI   r   r.   r!   r   <module>rT      sK    H 
 
 
  -

 ,,r| rr!   