
    &th                         d dl mZmZmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZmZ 	 d dlmZ n# e$ r dZY nw xY wdgZ G d de          ZdS )	    )AnyCallableDictOptionalTuple)DeferredensureDeferred)Failure)EventEmitterPyeeException)iscoroutineNTwistedEventEmitterc                   r     e Zd ZdZ fdZdedeedf         dee	ef         ddfd	Z
d
e	deddf fdZ xZS )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.
    c                 V    t          t          |                                            d S )N)superr   __init__)self	__class__s    `/var/www/html/mycamper/aliexpress-site/backend/venv/lib/python3.11/site-packages/pyee/twisted.pyr   zTwistedEventEmitter.__init__7   s&    !4((1133333    fargs.kwargsreturnNc                 P    d }	  ||i |}t           rt          |          rt          |          }nt          |t                    r|}n|sd S dt          dd f fd}|                    |           d S # t          $ r&                      dt	                                 Y d S w xY w)Nfailurer   c                 <    | r                     d|            d S d S )Nr   )emit)r   r   s    r   errbackz.TwistedEventEmitter._emit_run.<locals>.errbackM   s/     2IIi111112 2r   )r   r	   
isinstancer   r
   
addErrback	Exceptionr   )r   r   r   r   dresultr   s   `      r   	_emit_runzTwistedEventEmitter._emit_run:   s     &*	"Q'''F  {622 "6**FH--  2 2T 2 2 2 2 2 2 LL!!!!!  	, 	, 	,IIi++++++	,s   A5 5,B%$B%eventerrorc                    |dk    rt          |t                    rE	 |                                 d S # t          $ r!}|                     d|           Y d }~d S d }~ww xY wt          |t                    r|                     d|           d S |                     dt          d|                      d S t          t          |                               ||           d S )Nr   r'   zUnexpected failure object: )	r    r
   raiseExceptionr"   r   r   r   r   _emit_handle_potential_error)r   r&   r'   excr   s       r   r*   z0TwistedEventEmitter._emit_handle_potential_errorS   s   I%)) Y,((*****  , , ,IIgs+++++++++,E9-- Y		'5)))))		'=1Vu1V1V#W#WXXXXX&--KKu    s   4 
AAA)__name__
__module____qualname____doc__r   r   r   r   r   strr%   r*   __classcell__)r   s   @r   r   r      s        ! !F4 4 4 4 4"" CHo" S#X	"
 
" " " "2# c d          r   )typingr   r   r   r   r   twisted.internet.deferr   r	   twisted.python.failurer
   	pyee.baser   r   asyncior   ImportError__all__r    r   r   <module>r:      s    8 7 7 7 7 7 7 7 7 7 7 7 7 7 ; ; ; ; ; ; ; ; * * * * * * 1 1 1 1 1 1 1 1#######   KKK !
!N N N N N, N N N N Ns   - 77