o
    %h                     @   s   d dl 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 eG dd dZG dd dZe Zd	ed
eegef fddZdeded
efddZe
de	dZded
efddZdS )    )	dataclass)wraps)AnyCallableIteratorListTypeTypeVar)EventEmitterc                   @   s   e Zd ZU eed< eed< dS )HandlereventmethodN)__name__
__module____qualname__str__annotations__r    r   r   </var/www/html/myenv/lib/python3.10/site-packages/pyee/cls.pyr      s   
 r   c                   @   s:   e Zd ZdddZdddZdee fddZd	d
 ZdS )HandlersreturnNc                 C   
   g | _ d S N	_handlersselfr   r   r   __init__      
zHandlers.__init__c                 C   s   | j | d S r   )r   append)r   handlerr   r   r   r      s   zHandlers.appendc                 C   s
   t | jS r   )iterr   r   r   r   r   __iter__   r   zHandlers.__iter__c                 C   r   r   r   r   r   r   r   reset   r   zHandlers.reset)r   N)	r   r   r   r   r   r   r   r"   r#   r   r   r   r   r      s
    

r   r   r   c                    s   dt dt f fdd}|S )zr
    Register an event handler on an evented class. See the `evented` class
    decorator for a full example.
    r   r   c                    s   t t | d | S )N)r   r   )r   r   r   )r   r   r   r   	decorator%   s   zon.<locals>.decorator)r   )r   r%   r   r$   r   on   s   r&   r   r   c                    s    t  dtf fdd}|S )Nr   c                     s    g| R i |S r   r   )argskwargsr   r   r   r   bound-   s   z_bind.<locals>.bound)r   r   )r   r   r*   r   r)   r   _bind,   s   r+   Cls)r*   clsc              	      sJ   t t t  | jt| jdtdtdtddf fdd}|| _| S )aQ  
    Configure an evented class.

    Evented classes are classes which use an EventEmitter to call instance
    methods during runtime. To achieve this without this helper, you would
    instantiate an `EventEmitter` in the `__init__` method and then call
    `event_emitter.on` for every method on `self`.

    This decorator and the `on` function help make things look a little nicer
    by defining the event handler on the method in the class and then adding
    the `__init__` hook in a wrapper:

    ```py
    from pyee.cls import evented, on

    @evented
    class Evented:
        @on("event")
        def event_handler(self, *args, **kwargs):
            print(self, args, kwargs)

    evented_obj = Evented()

    evented_obj.event_emitter.emit(
        "event", "hello world", numbers=[1, 2, 3]
    )
    ```

    The `__init__` wrapper will create a `self.event_emitter: EventEmitter`
    automatically but you can also define your own event_emitter inside your
    class's unwrapped `__init__` method. For example, to use this
    decorator with a `TwistedEventEmitter`::

    ```py
    @evented
    class Evented:
        def __init__(self):
            self.event_emitter = TwistedEventEmitter()

        @on("event")
        async def event_handler(self, *args, **kwargs):
            await self.some_async_action(*args, **kwargs)
    ```
    r   r'   r(   r   Nc                    sN   | g|R i | t | dst | _ D ]}| j|jt| |j qd S )Nevent_emitter)hasattrr
   r.   r&   r   r+   r   )r   r'   r(   hhandlersog_initr   r   initi   s   
zevented.<locals>.init)listr   r#   r   r   r   )r-   r4   r   r1   r   evented7   s   -"r6   N)dataclassesr   	functoolsr   typingr   r   r   r   r   r	   pyeer
   r   r   r   r   r&   r+   r,   r6   r   r   r   r   <module>   s     