
    g%                         d dl mZ d dlmZ d dlmZmZmZmZ d dl	m
Z
 e G d d             Z G d d      Z e       Zd	ed
eegef   fdZd Z ede      Zded
efdZy)    )	dataclasswraps)CallableListTypeTypeVar)EventEmitterc                   "    e Zd ZU eed<   eed<   y)HandlereventmethodN)__name__
__module____qualname__str__annotations__r        =/var/www/openai/venv/lib/python3.12/site-packages/pyee/cls.pyr   r      s    Jr   r   c                   $    e Zd Zd Zd Zd Zd Zy)Handlersc                     g | _         y N	_handlersselfs    r   __init__zHandlers.__init__   s	    (*r   c                 :    | j                   j                  |       y r   )r   append)r   handlers     r   r!   zHandlers.append   s    g&r   c                 ,    t        | j                        S r   )iterr   r   s    r   __iter__zHandlers.__iter__   s    DNN##r   c                     g | _         y r   r   r   s    r   resetzHandlers.reset   s	    r   N)r   r   r   r   r!   r%   r'   r   r   r   r   r      s    +'$r   r   r   returnc                 ,     dt         dt         f fd}|S )zr
    Register an event handler on an evented class. See the `evented` class
    decorator for a full example.
    r   r(   c                 H    t         j                  t        |              | S )N)r   r   )r   r!   r   )r   r   s    r   	decoratorzon.<locals>.decorator%   s    uV<=r   )r   )r   r+   s   ` r   onr,      s    ( x  r   c                 2     t               fd       }|S )Nc                       g| i |S r   r   )argskwargsr   r   s     r   boundz_bind.<locals>.bound-   s    d,T,V,,r   r   )r   r   r1   s   `` r   _bindr2   ,   s     
6]- - Lr   Cls)r1   clsc                     t        t              t        j                          | j                  t	        | j                        f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)
    ```
    c                      | g|i | t        | d      st               | _        D ]<  }| j                  j                  |j                  t        | |j                               > y )Nevent_emitter)hasattrr
   r7   r,   r   r2   r   )r   r/   r0   hhandlersog_inits       r   initzevented.<locals>.initi   sX    &t&v&t_-!-DA!!!''5qxx+@A r   )listr   r'   r   r   )r4   r<   r:   r;   s     @@r   eventedr>   7   sM    Z #9oHOOG
3<<B B CLJr   N)dataclassesr   	functoolsr   typingr   r   r   r	   pyeer
   r   r   r   r   r,   r2   r3   r>   r   r   r   <module>rC      s    !  0 0    
  J	
c 
hz834 
 e4 = = =r   