a
    Ò�éi»  ã                   @   sŠ   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 zd dlmZ W n eyn   dZY n0 dgZG dd„ deƒZdS )	é    )ÚAnyÚCallableÚDictÚOptionalÚTuple)ÚDeferredÚensureDeferred)ÚFailure)ÚEventEmitterÚPyeeException)ÚiscoroutineNÚTwistedEventEmitterc                       sZ   e Zd ZdZ‡ fdd„Zeeedf ee	ef ddœdd„Z
e	edd	œ‡ fd
d„Z‡  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                    s   t t| ƒ ¡  d S )N)Úsuperr   Ú__init__©Úself©Ú	__class__© ú</home/Claro/venv/lib/python3.9/site-packages/pyee/twisted.pyr   7   s    zTwistedEventEmitter.__init__.N)ÚfÚargsÚkwargsÚreturnc                    sˆ   d }z||i |¤Ž}W n  t y6   ˆ  dtƒ ¡ Y nN0 trNt|ƒrNt|ƒ}nt|tƒr^|}n|sfd S td dœ‡ fdd„}| |¡ d S )NÚfailure)r   r   c                    s   | rˆ   d| ¡ d S )Nr   )Úemit)r   r   r   r   ÚerrbackM   s    z.TwistedEventEmitter._emit_run.<locals>.errback)Ú	Exceptionr   r	   r   r   Ú
isinstancer   Z
addErrback)r   r   r   r   ÚdÚresultr   r   r   r   Ú	_emit_run:   s    

zTwistedEventEmitter._emit_run)ÚeventÚerrorr   c              
      s˜   |dkr‚t |tƒrRz| ¡  W q€ tyN } z|  d|¡ W Y d }~q€d }~0 0 q”t |tƒrj|  d|¡ q”|  dtd|› �ƒ¡ ntt| ƒ ||¡ d S )Nr   r#   zUnexpected failure object: )	r   r	   ZraiseExceptionr   r   r   r   r   Ú_emit_handle_potential_error)r   r"   r#   Úexcr   r   r   r$   S   s    
$

ÿz0TwistedEventEmitter._emit_handle_potential_error)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   Ústrr!   r$   Ú__classcell__r   r   r   r   r      s   #

û)Útypingr   r   r   r   r   Ztwisted.internet.deferr   r   Ztwisted.python.failurer	   Z	pyee.baser
   r   Zasyncior   ÚImportErrorÚ__all__r   r   r   r   r   Ú<module>   s   
