
    q	h                     j    S SK r S SKrS SKJrJrJrJrJrJr  SSK	J
r
  \" SSS9r " S S	\\   5      rg)
    N)CallableDictSetOptionalGenericTypeVar   )loggerT_contraT)contravariantc                       \ rS rSrSS jrS\SS4S jrSS\S\\   S\4S jjr	SS\S\\   S\4S	 jjr
S\S\SS4S
 jrSrg)EventEmitter
   returnNc                 "    [        5       U l        g)z,
Initialize a new instance of EventEmitter.
N)dict_events)selfs    K/var/www/html/env/lib/python3.13/site-packages/livekit/rtc/event_emitter.py__init__EventEmitter.__init__   s     7;f    eventc                 L   XR                   ;   a  U R                   U   R                  5       nU H  n [        R                  " U5      nUR                  R                  5       n[        S U 5       5      nU(       a  U" U6   MT  U Vs/ s H-  nUR                  UR                  UR                  4;   d  M+  UPM/     n	n[        U	5      n
[        [        U5      U
5      nUSU nU" U6   M     ggs  snf ! [         a    e [         a    [        R                  " SU 35         M  f = f)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   R   #    U  H  oR                   UR                  :H  v   M     g 7fN)kindVAR_POSITIONAL).0ps     r   	<genexpr>$EventEmitter.emit.<locals>.<genexpr>-   s     %Q&Qff0@0@&@&s   %'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   emitEventEmitter.emit   s   * LL U+002I%F!++H5C ^^224F"%%Q&%Q"QK" $ &,-%+ vv!*;*;Q=T=T)UU %+ * -
 &)):%;
#&s4y*#=(,Yh -0% & !- !   F$$'<UG%DEFs0   AC2C2*C-5C-;,C2-C22-D#"D#r2   c                 t   ^ ^^^ Tb  UUUU 4S jmT R                  TT5      $ S[        S[        4UU 4S jjnU$ )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                  :   > TR                  TT5        T" U 0 UD6  g r   )off)r0   kwargsr2   r   once_callbackr   s     r   r@   (EventEmitter.once.<locals>.once_callbackk   s    .$)&)r   r2   r   c                 ,   > TR                  TU 5        U $ r   )oncer2   r   r   s    r   	decorator$EventEmitter.once.<locals>.decoratorr   s    		%*r   )onr   )r   r   r2   rE   r@   s   ``` @r   rC   EventEmitter.once@   sI    R * * 775-00 H       r   c                   ^ ^ Ubm  [         R                  " U5      (       a  [        S5      eTT R                  ;  a  [	        5       T R                  T'   T R                  T   R                  U5        U$ S[        S[        4UU 4S jjnU$ )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.r2   r   c                 ,   > TR                  TU 5        U $ r   )rG   rD   s    r   rE   "EventEmitter.on.<locals>.decorator   s    x(r   )asyncioiscoroutinefunction
ValueErrorr   setaddr   )r   r   r2   rE   s   ``  r   rG   EventEmitter.onx   s    N **844  J  DLL(&)eU#LL##H-O H       r   c                 `    XR                   ;   a  U R                   U   R                  U5        gg)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   r2   s      r   r>   EventEmitter.off   s*    , LL LL''1 !r   )r   )r   Nr   )__name__
__module____qualname____firstlineno__r   r   r:   r   r   rC   rG   r>   __static_attributes__ 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$   rL   typingr   r   r   r   r   r   logr
   r   r   rZ   r   r   <module>r]      s3      B B :T2~278$ ~2r   