
    AHj                       d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlmZmZ ddlZddlmZ erddlmZ  e	d      Ze
eegee   f   eegef   f   Z G d	 d
e      Zej0                  j3                  d       G d dee                Zy)z
Signals are a simple pub-sub mechanism.

DOMNodes can subscribe to a signal, which will invoke a callback when the signal is published.

    )annotations)TYPE_CHECKINGAny	AwaitableCallableGenericTypeVarUnion)WeakKeyDictionaryrefN)log)DOMNodeSignalTc                      e Zd ZdZy)SignalErrorzRaised for Signal errors.N)__name__
__module____qualname____doc__     F/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/signal.pyr   r      s    #r   r   T)angularc                  \    e Zd ZdZd	dZd
dZedd       Z	 d	 	 	 	 	 	 	 ddZddZ	ddZ
y)SignalzfA signal that a widget may subscribe to, in order to invoke callbacks when an associated event occurs.c                P    t        |      | _        || _        t               | _        y)zInitialize a signal.

        Args:
            owner: The owner of this signal.
            name: An identifier for debugging purposes.
        N)r   _owner_namer   _subscriptions)selfownernames      r   __init__zSignal.__init__#   s&     %j
  	r   c              #     K   d| j                   f d| j                  f dt        | j                  j	                               f y w)Nr!   r"   subscriptions)r!   r   listr   keysr    s    r   __rich_repr__zSignal.__rich_repr__0   sC     tzz!!djj  tD$7$7$<$<$>???s   A	Ac                "    | j                         S )z9The owner of this Signal, or `None` if there is no owner.)r   r(   s    r   r!   zSignal.owner5   s     {{}r   c                    j                   st        d d      |rdfd}ndfd}| j                  j                  g       }|j	                  |       y)a&  Subscribe a node to this signal.

        When the signal is published, the callback will be invoked.

        Args:
            node: Node to subscribe.
            callback: A callback function which takes a single argument and returns anything (return type ignored).
            immediate: Invoke the callback immediately on publish if `True`, otherwise post it to the DOM node to be
                called once existing messages have been processed.

        Raises:
            SignalError: Raised when subscribing a non-mounted widget.
        z3Node must be running to subscribe to a signal (has z been mounted)?c                     |        y)z Invoke the callback immediately.Nr   )datacallbacks    r   signal_callbackz)Signal.subscribe.<locals>.signal_callbackU   s
    r   c                *    j                  |        y)z>Post the callback to the node, to call at the next opertunity.N)	call_next)r-   r.   nodes    r   r/   z)Signal.subscribe.<locals>.signal_callback[   s    x.r   Nr-   r   returnNone)
is_runningr   r   
setdefaultappend)r    r2   r.   	immediater/   	callbackss    ``   r   	subscribezSignal.subscribe:   s\    ( EdV?[  / ''224<	)r   c                <    | j                   j                  |d       y)zcUnsubscribe a node from this signal.

        Args:
            node: Node to unsubscribe,
        N)r   pop)r    r2   s     r   unsubscribezSignal.unsubscribeb   s     	d+r   c                   | j                   }|y|j                  r|j                  ry|j                  D ]  }|j                  r y t        | j                  j                               D ]U  \  }}|j                  r|j                  r|j                  r| j                  j                  |       F|D ]  }	  ||        W y# t        $ r(}t        j                  d| d| d|        Y d}~=d}~ww xY w)zPublish the signal (invoke subscribed callbacks).

        Args:
            data: An argument to pass to the callbacks.

        Nzerror publishing signal to z ignored (callback=z); )r!   is_attached_pruningancestors_with_selfr6   r&   r   itemsr=   	Exceptionr   error)r    r-   r!   ancestor_noder2   r:   r.   rE   s           r   publishzSignal.publishj   s     

=  ENN"66 	M ++	  $D$7$7$=$=$?@ 	OD)OO(8(8T]]##''- !* H 	 % 		9$?RS[R\\_`e_fg s   7C	C5C00C5N)r!   r   r"   strr4   r5   )r4   zrich.repr.Result)r4   zDOMNode | None)F)r2   r   r.   zSignalCallbackType[SignalT]r9   boolr4   r5   )r2   r   r4   r5   r3   )r   r   r   r   r#   r)   propertyr!   r;   r>   rG   r   r   r   r   r      s_    p @
    	&*&* .&* 	&*
 
&*P,r   r   )r   
__future__r   typingr   r   r   r   r   r	   r
   weakrefr   r   	rich.reprrichtextualr   textual.domr   r   SignalCallbackTyperD   r   reprautor   r   r   r   <module>rU      s    # S S S *  #
)
gY	#&'7)S.)AA 
$) $ gWW g gr   