
    qi                     p    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      Z G d d	ee         Zy)
    N)CallableDictSetOptionalGenericTypeVar   )loggerT_contraT)contravariantc                   p    e Zd Zd
dZdeddfdZddedee   defdZddedee   defdZ	dededdfd	Z
y)EventEmitterreturnNc                 "    t               | _        y)z<
        Initialize a new instance of EventEmitter.
        N)dict_events)selfs    K/opt/pipecat/venv/lib/python3.12/site-packages/livekit/rtc/event_emitter.py__init__zEventEmitter.__init__   s     7;f    eventc                 "   || j                   v r| j                   |   j                         }|D ]  }	 t        j                  |      }|j                  j                         }t        d |D              }|r ||  n]|D cg c](  }|j                  |j                  |j                  fv r|* }	}t        |	      }
t        t        |      |
      }|d| } ||   yyc c}w # t        $ r  t        $ r t        j                  d|        Y w xY w)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              3   N   K   | ]  }|j                   |j                  k(    y wN)kindVAR_POSITIONAL).0ps     r   	<genexpr>z$EventEmitter.emit.<locals>.<genexpr>-   s     %QQaff0@0@&@%Qs   #%Nzfailed to emit event )r   copyinspect	signature
parametersvaluesanyr   POSITIONAL_ONLYPOSITIONAL_OR_KEYWORDlenmin	TypeError	Exceptionr
   	exception)r   r   args	callablescallbacksigparamshas_varargsr   positional_params
num_paramsnum_argscallback_argss                r   emitzEventEmitter.emit   s&   * DLL U+002I% FF!++H5C ^^224F"%%Q&%Q"QK" $ &,- ! vv!*;*;Q=T=T)UU -) -
 &)):%;
#&s4y*#=(,Yh -0%F !- !   F$$'<UG%DEFs$   AC!?-C,,C!C!!*DDr/   c                 l      fd j                        S dt        dt        f f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
            ```
        c                  :    j                          | i | y r   )off)r-   kwargsr/   r   once_callbackr   s     r   r<   z(EventEmitter.once.<locals>.once_callbackk   s    .$)&)r   r/   r   c                 ,    j                  |        | S r   )oncer/   r   r   s    r   	decoratorz$EventEmitter.once.<locals>.decoratorr   s    		%*r   )onr   )r   r   r/   r@   r<   s   ``` @r   r>   zEventEmitter.once@   s?    R * 775-00 H     r   c                      |et        j                  |      rt        d       j                  vrt	                j                  <    j                     j                  |       |S dt        dt        f f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!
            ```
        zsCannot register an async callback with `.on()`. Use `asyncio.create_task` within your synchronous callback instead.r/   r   c                 ,    j                  |        | S r   )rA   r?   s    r   r@   z"EventEmitter.on.<locals>.decorator   s    x(r   )asyncioiscoroutinefunction
ValueErrorr   setaddr   )r   r   r/   r@   s   ``  r   rA   zEventEmitter.onx   s    N **84  J  DLL(&)eU#LL##H-O H     r   c                 ^    || j                   v r| j                   |   j                  |       yy)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/   s      r   r:   zEventEmitter.off   s,    , DLL LL''1 !r   )r   Nr   )__name__
__module____qualname__r   r   r7   r   r   r>   rA   r:    r   r   r   r   
   sx    =-F( -Fd -F^6( 6hx.@ 6H 6p7 7HX,> 7( 7r2 2X 2$ 2r   r   )r!   rD   typingr   r   r   r   r   r   logr
   r   r   rN   r   r   <module>rQ      s3      B B :T2~278$ ~2r   