
    Yh                     |    S SK Jr  S SKJrJrJrJrJrJr  S SK	J
r
Jr  S SKJr  S SKJrJr  \rS/r " S S\5      rg)	    )iscoroutine)AnyCallablecastDictOptionalTuple)DeferredensureDeferred)Failure)EventEmitter	PyeeErrorTwistedEventEmitterc            
          ^  \ rS rSrSrS\SS4U 4S jjrS\S\S\\	S	4   S
\
\\	4   SS4
S jrS\S\S\	SS4U 4S jjrSrU =r$ )r      a@  An event emitter class which can run twisted coroutines and handle
returned Deferreds, in addition to synchronous blocking functions. For
example:

```py
@ee.on('event')
@inlineCallbacks
def async_handler(*args, **kwargs):
    yield returns_a_deferred()
```

or:

```py
@ee.on('event')
async def async_handler(*args, **kwargs):
    await returns_a_deferred()
```


When async handlers fail, Failures are first emitted on the `failure`
event. If there are no `failure` handlers, the Failure's associated
exception is then emitted on the `error` event. If there are no `error`
handlers, the exception is raised. For consistency, when handlers raise
errors synchronously, they're captured, wrapped in a Failure and treated
as an async failure. This is unlike the behavior of EventEmitter,
which have no special error handling.

For twisted coroutine event handlers, calling emit is non-blocking.
In other words, you do not have to await any results from emit, and the
coroutine is scheduled in a fire-and-forget fashion.

Similar behavior occurs for "sync" functions which return Deferreds.
selfreturnNc                 *   > [         [        U ]  5         g )N)superr   __init__)r   	__class__s    >/var/www/html/env/lib/python3.13/site-packages/pyee/twisted.pyr   TwistedEventEmitter.__init__5   s    !413    fargs.kwargsc                 *  ^  S n U" U0 UD6n[        U5      (       a  [        U5      nO [        U[        5      (       a  UnOU(       d  g S[        SS 4U 4S jjnUR                  U5        g ! [         a    T R                  S[	        5       5         g f = f)Nfailurer   c                 :   > U (       a  TR                  SU 5        g g )Nr   )emit)r   r   s    r   errback.TwistedEventEmitter._emit_run.<locals>.errbackK   s    IIi1 r   )r   r   
isinstancer
   r   
addErrback	Exceptionr!   )r   r   r   r   dresultr"   s   `      r   	_emit_runTwistedEventEmitter._emit_run8   s     &*	"''F 6"""6*FH--2 2T 2 LL!  	,IIi+	,s   A+ +$BBeventerrorc                   > US:X  an  [        U[        5      (       a   UR                  5         g [        U[        5      (       a  U R	                  SU5        g U R	                  S[        SU 35      5        g [        [        [        [        U 5      5      R                  X5        g ! [         a  nU R	                  SU5         S nAg S nAff = f)Nr   r,   zUnexpected failure object: )r$   r   raiseExceptionr&   r!   r   r   r   r   r   _emit_handle_potential_error)r   r+   r,   excr   s       r   r/   0TwistedEventEmitter._emit_handle_potential_errorQ   s    I%)),((* E9--		'5)		'9/J5'-R#STe/67TT ! ,IIgs++,s   B$ $
C
.CC
 )__name__
__module____qualname____firstlineno____doc__Selfr   r   r	   r   r   strr)   r/   __static_attributes____classcell__)r   s   @r   r   r      s    !F4t 4 4""" CHo" S#X	"
 
"24  C D  r   N)asyncior   typingr   r   r   r   r   r	   twisted.internet.deferr
   r   twisted.python.failurer   	pyee.baser   r   r8   __all__r   r2   r   r   <module>rB      s6      = = ; * -
 !
!N, Nr   