a
    ݌xda                     @   s   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 dZG dd dZe Zeeegef d	d
dZdd ZededZeedddZdS )    )	dataclasswraps)Callable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/Ranjet/env/lib/python3.9/site-packages/pyee/cls.pyr
      s   
r
   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
Handlersc                 C   s
   g | _ d S N	_handlersselfr   r   r   __init__   s    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__   s    zHandlers.__iter__c                 C   s
   g | _ d S r   r   r   r   r   r   reset   s    zHandlers.resetN)r   r   r   r   r   r   r   r   r   r   r   r      s   r   )r   returnc                    s   t t d fdd}|S )zt
    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#   c                    s   t   fdd}|S )Nc                     s    g| R i |S r   r   )argskwargsr   r   r   r   bound-   s    z_bind.<locals>.boundr   )r   r   r'   r   r&   r   _bind,   s    r(   Cls)namer'   )clsr    c                    s8   t t t  | jt| j fdd}|| _| S )a  
    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::

        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``::

        @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                    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handlersZog_initr   r   inite   s
    
zevented.<locals>.init)listr   r   r   r   )r+   r1   r   r/   r   evented7   s    )r3   N)dataclassesr   	functoolsr   typingr   r   r   r   Zpyeer	   r
   r   r   r   r#   r(   r)   r3   r   r   r   r   <module>   s   