a
    Ò�éi§
  ã                   @   sb   d dl mZmZmZmZ d dlmZmZmZm	Z	m
Z
mZmZ d dlmZ dgZG dd„ deƒZdS )é    )ÚAbstractEventLoopÚensure_futureÚFutureÚiscoroutine)ÚAnyÚCallableÚcastÚDictÚOptionalÚSetÚTuple)ÚEventEmitterÚAsyncIOEventEmitterc                       sN   e Zd ZdZd
ee dœ‡ fdd„Zeee	df e
ee	f dœdd	„Z‡  ZS )r   a(  An event emitter class which can run asyncio coroutines in addition to
    synchronous blocking functions. For example:

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

    On emit, the event emitter  will automatically schedule the coroutine using
    `asyncio.ensure_future` and the configured event loop (defaults to
    `asyncio.get_event_loop()`).

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

    When `loop` is specified, the supplied event loop will be used when
    scheduling work with `ensure_future`. Otherwise, the default asyncio
    event loop is used.

    For asyncio 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.
    N©Úloopc                    s    t t| ƒ ¡  || _tƒ | _d S )N)Úsuperr   Ú__init__Ú_loopÚsetÚ_waiting)Úselfr   ©Ú	__class__© ú</home/Claro/venv/lib/python3.9/site-packages/pyee/asyncio.pyr   '   s    zAsyncIOEventEmitter.__init__.)ÚfÚargsÚkwargsc              
      s¸   z||i |¤Ž}W n0 t yB } zˆ  d|¡ W Y d }~nzd }~0 0 t|ƒrxˆ jrhttt|ƒˆ jd�}q’ttt|ƒƒ}nt|tƒrŽtt|ƒ}nd S ‡ fdd„}| 	|¡ ˆ j
 |¡ d S )NÚerrorr   c                    s4   ˆ j  | ¡ |  ¡ rd S |  ¡ }|r0ˆ  d|¡ d S )Nr   )r   ÚremoveZ	cancelledÚ	exceptionÚemit)r   Úexc©r   r   r   ÚcallbackE   s    z/AsyncIOEventEmitter._emit_run.<locals>.callback)Ú	Exceptionr!   r   r   r   r   r   Ú
isinstancer   Zadd_done_callbackr   Úadd)r   r   r   r   Úcoror"   Zfutr$   r   r#   r   Ú	_emit_run,   s    "


zAsyncIOEventEmitter._emit_run)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r   r   r	   Ústrr)   Ú__classcell__r   r   r   r   r      s   

üN)Zasyncior   r   r   r   Útypingr   r   r   r	   r
   r   r   Z	pyee.baser   Ú__all__r   r   r   r   r   Ú<module>   s   $