a
    Ò�éi’	  ã                   @   sf   d dl mZmZmZ d dlmZ d dl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 )é    )ÚExecutorÚFutureÚThreadPoolExecutor)ÚTracebackType)ÚAnyÚCallableÚDictÚOptionalÚTupleÚType)ÚEventEmitterÚExecutorEventEmitterc                       sŠ   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deddœdd„Zd dœdd„Zee eeee dœdd„Z‡  ZS )r   a  An event emitter class which runs handlers in a `concurrent.futures`
    executor.

    By default, this class creates a default `ThreadPoolExecutor`, but
    a custom executor may also be passed in explicitly to, for instance,
    use a `ProcessPoolExecutor` instead.

    This class runs all emitted events on the configured executor. Errors
    captured by the resulting Future are automatically emitted on the
    `error` event. This is unlike the EventEmitter, which have no error
    handling.

    The underlying executor may be shut down by calling the `shutdown`
    method. Alternately you can treat the event emitter as a context manager:

    ```py
    with ExecutorEventEmitter() as ee:
        # Underlying executor open

        @ee.on('data')
        def handler(data):
            print(data)

        ee.emit('event')

    # Underlying executor closed
    ```

    Since the function call is scheduled on an executor, emit is always
    non-blocking.

    No effort is made to ensure thread safety, beyond using an executor.
    N)Úexecutorc                    s&   t t| ƒ ¡  |r|| _ntƒ | _d S ©N)Úsuperr   Ú__init__Ú	_executorr   )Úselfr   ©Ú	__class__© ú=/home/Claro/venv/lib/python3.9/site-packages/pyee/executor.pyr   /   s    zExecutorEventEmitter.__init__.)ÚfÚargsÚkwargsc                    s8   ˆ j j|g|¢R i |¤Ž}|jtd dœ‡ fdd„ƒ}d S )N)r   Úreturnc                    s0   |   ¡ }t|tƒr ˆ  d|¡ n|d ur,|‚d S )NÚerror)Ú	exceptionÚ
isinstanceÚ	ExceptionÚemit)r   Úexc©r   r   r   Ú	_callback>   s
    
z1ExecutorEventEmitter._emit_run.<locals>._callback)r   ZsubmitZadd_done_callbackr   )r   r   r   r   Úfuturer#   r   r"   r   Ú	_emit_run6   s    zExecutorEventEmitter._emit_runT)Úwaitr   c                 C   s   | j j|d� dS )z)Call `shutdown` on the internal executor.)r&   N)r   Úshutdown)r   r&   r   r   r   r'   F   s    zExecutorEventEmitter.shutdown)r   c                 C   s   | S r   r   r"   r   r   r   Ú	__enter__K   s    zExecutorEventEmitter.__enter__)ÚtypeÚvalueÚ	tracebackr   c                 C   s   |   ¡  d S r   )r'   )r   r)   r*   r+   r   r   r   Ú__exit__N   s    zExecutorEventEmitter.__exit__)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r
   r   r   Ústrr%   Úboolr'   r(   r   r   r   r,   Ú__classcell__r   r   r   r   r      s   "	

ü
þN)Úconcurrent.futuresr   r   r   Útypesr   Útypingr   r   r   r	   r
   r   Z	pyee.baser   Ú__all__r   r   r   r   r   Ú<module>   s
    