o
    fAh                     @   sj   d dl mZ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 eZdgZG dd deZdS )    )AbstractEventLoopensure_futureFutureiscoroutinewait)AnyCallablecastDictOptionalSetTuple)EventEmitterAsyncIOEventEmitterc                
       s   e Zd ZdZddedee ddf fddZdeded	e	d
e	de
f
 fddZdeded	ee	df d
eee	f ddf
ddZdeddfddZdeddfddZedede
f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selfloopreturnc                    s    t t|   || _t | _d S N)superr   __init___loopset_waiting)r   r   	__class__ U/var/www/html/brandlife/copilot/chat-env/lib/python3.10/site-packages/pyee/asyncio.pyr   )   s   zAsyncIOEventEmitter.__init__eventargskwargsc                    s   t  j|g|R i |S )a  Emit `event`, passing `*args` and `**kwargs` to each attached
        function or coroutine. Returns `True` if any functions are attached to
        `event`; otherwise returns `False`.

        Example:

        ```py
        ee.emit('data', '00101001')
        ```

        Assuming `data` is an attached function, this will call
        `data('00101001')'`.

        When executing coroutine handlers, their respective futures will be
        stored in a "waiting" state. These futures may be waited on or
        canceled with `wait_for_complete` and `cancel`, respectively; and
        their status may be checked via the `complete` property.
        N)r   emit)r   r   r   r   r   r   r   r    .   s   zAsyncIOEventEmitter.emitf.c              
      s   z	||i |}W n t y" } z d| W Y d }~d S d }~ww t|r= jr5ttt| jd}nttt|}nt|trHtt|}nd S dtdd f fdd}|	|  j
| d S )Nerror)r   r!   r   c                    s8    j |  |  rd S |  }|r d| d S d S )Nr"   )r   discard	cancelled	exceptionr    )r!   excr   r   r   callbacka   s   z/AsyncIOEventEmitter._emit_run.<locals>.callback)	Exceptionr    r   r   r   r	   r   
isinstancer   add_done_callbackr   add)r   r!   r   r   coror&   futr(   r   r'   r   	_emit_runH   s    


zAsyncIOEventEmitter._emit_runc                    s    | j rt| j I dH  dS dS )a  Waits for all pending tasks to complete. For example:

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

        # Triggers execution of async_handler
        ee.emit('data', '00101001')

        await ee.wait_for_complete()

        # async_handler has completed execution
        ```

        This is useful if you're attempting a graceful shutdown of your
        application and want to ensure all coroutines have completed execution
        beforehand.
        N)r   r   r'   r   r   r   wait_for_completen   s   z%AsyncIOEventEmitter.wait_for_completec                 C   s2   | j D ]}| s| s|  q| j   dS )a  Cancel all pending tasks. For example:

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

        # Triggers execution of async_handler
        ee.emit('data', '00101001')

        ee.cancel()

        # async_handler execution has been canceled
        ```

        This is useful if you're attempting to shut down your application and
        attempts at a graceful shutdown via `wait_for_complete` have failed.
        N)r   doner$   cancelclear)r   r.   r   r   r   r2      s
   
zAsyncIOEventEmitter.cancelc                 C   s   | j  S )a  When true, there are no pending tasks, and execution is complete.
        For example:

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

        # Triggers execution of async_handler
        ee.emit('data', '00101001')

        # async_handler is still running, so this prints False
        print(ee.complete)

        await ee.wait_for_complete()

        # async_handler has completed execution, so this prints True
        print(ee.complete)
        ```
        N)r   r'   r   r   r   complete   s   zAsyncIOEventEmitter.completer   )__name__
__module____qualname____doc__Selfr   r   r   strr   boolr    r   r   r
   r/   r0   r2   propertyr4   __classcell__r   r   r   r   r      s:     


&N)asyncior   r   r   r   r   typingr   r   r	   r
   r   r   r   	pyee.baser   r9   __all__r   r   r   r   r   <module>   s   $