o
    i                     @   s`   d dl Z d dlZd dlmZmZmZmZmZmZ ddl	m
Z
 edddZG dd	 d	ee ZdS )
    N)CallableDictSetOptionalGenericTypeVar   )loggerT_contraT)contravariantc                   @   sv   e Zd ZdddZdeddfddZddedee defd	d
Zddedee defddZ	dededdfddZ
dS )EventEmitterreturnNc                 C   s   t  | _dS )z<
        Initialize a new instance of EventEmitter.
        N)dict_events)self r   [/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/rtc/event_emitter.py__init__   s   zEventEmitter.__init__eventc              	   G   s   || j v ra| j |  }|D ]T}z8t|}|j }tdd |D }|r+||  ndd |D }t|}	tt||	}
|d|
 }||  W q t	yP     t
y`   td|  Y qw dS dS )a  
        Trigger all callbacks associated with the given event.

        Args:
            event (T): The event to emit.
            *args: Positional arguments to pass to the callbacks.

        Example:
            Basic usage of emit:

            ```python
            emitter = EventEmitter[str]()

            def greet(name):
                print(f"Hello, {name}!")

            emitter.on('greet', greet)
            emitter.emit('greet', 'Alice')  # Output: Hello, Alice!
            ```
        c                 s   s    | ]	}|j |jkV  qd S N)kindVAR_POSITIONAL.0pr   r   r   	<genexpr>-   s    z$EventEmitter.emit.<locals>.<genexpr>c                 S   s"   g | ]}|j |j|jfv r|qS r   )r   POSITIONAL_ONLYPOSITIONAL_OR_KEYWORDr   r   r   r   
<listcomp>1   s
    z%EventEmitter.emit.<locals>.<listcomp>Nzfailed to emit event )r   copyinspect	signature
parametersvaluesanylenmin	TypeError	Exceptionr	   	exception)r   r   args	callablescallbacksigparamshas_varargspositional_params
num_paramsnum_argscallback_argsr   r   r   emit   s0   



zEventEmitter.emitr,   c                    sB    dur fdd S dtdtffdd}|S )a  
        Register a callback to be called only once when the event is emitted.

        If a callback is provided, it registers the callback directly.
        If no callback is provided, it returns a decorator for use with function definitions.

        Args:
            event (T): The event to listen for.
            callback (Callable, optional): The callback to register. Defaults to None.

        Returns:
            Callable: The registered callback or a decorator if callback is None.

        Example:
            Using once with a direct callback:

            ```python
            emitter = EventEmitter[str]()

            def greet_once(name):
                print(f"Hello once, {name}!")

            emitter.once('greet', greet_once)
            emitter.emit('greet', 'Bob')    # Output: Hello once, Bob!
            emitter.emit('greet', 'Bob')    # No output, callback was removed after first call
            ```

            Using once as a decorator:

            ```python
            emitter = EventEmitter[str]()

            @emitter.once('greet')
            def greet_once(name):
                print(f"Hello once, {name}!")

            emitter.emit('greet', 'Bob')    # Output: Hello once, Bob!
            emitter.emit('greet', 'Bob')    # No output
            ```
        Nc                     s      | i | d S r   )off)r*   kwargsr,   r   once_callbackr   r   r   r8   k   s   z(EventEmitter.once.<locals>.once_callbackr,   r   c                         |  | S r   )oncer,   r   r   r   r   	decoratorr      z$EventEmitter.once.<locals>.decorator)onr   r   r   r,   r=   r   r7   r   r:   @   s
   )zEventEmitter.oncec                    s`   |dur"t |rtd jvrt j < j  | |S dtdtf fdd}|S )a`  
        Register a callback to be called whenever the event is emitted.

        If a callback is provided, it registers the callback directly.
        If no callback is provided, it returns a decorator for use with function definitions.

        Args:
            event (T): The event to listen for.
            callback (Callable, optional): The callback to register. Defaults to None.

        Returns:
            Callable: The registered callback or a decorator if callback is None.

        Example:
            Using on with a direct callback:

            ```python
            emitter = EventEmitter[str]()

            def greet(name):
                print(f"Hello, {name}!")

            emitter.on('greet', greet)
            emitter.emit('greet', 'Charlie')  # Output: Hello, Charlie!
            ```

            Using on as a decorator:

            ```python
            emitter = EventEmitter[str]()

            @emitter.on('greet')
            def greet(name):
                print(f"Hello, {name}!")

            emitter.emit('greet', 'Charlie')  # Output: Hello, Charlie!
            ```
        NzsCannot register an async callback with `.on()`. Use `asyncio.create_task` within your synchronous callback instead.r,   r   c                    r9   r   )r?   r;   r<   r   r   r=      r>   z"EventEmitter.on.<locals>.decorator)asyncioiscoroutinefunction
ValueErrorr   setaddr   r@   r   r<   r   r?   x   s   '

zEventEmitter.onc                 C   s"   || j v r| j | | dS dS )a  
        Unregister a callback from an event.

        Args:
            event (T): The event to stop listening to.
            callback (Callable): The callback to remove.

        Example:
            Removing a callback:

            ```python
            emitter = EventEmitter[str]()

            def greet(name):
                print(f"Hello, {name}!")

            emitter.on('greet', greet)
            emitter.off('greet', greet)
            emitter.emit('greet', 'Dave')  # No output, callback was removed
            ```
        N)r   discard)r   r   r,   r   r   r   r5      s   
zEventEmitter.off)r   Nr   )__name__
__module____qualname__r   r
   r4   r   r   r:   r?   r5   r   r   r   r   r   
   s    
/89r   )r    rA   typingr   r   r   r   r   r   logr	   r
   r   r   r   r   r   <module>   s     