o
    tf                     @  s   d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	m
Z
mZmZmZmZmZ e	r.d dlZddlmZ ddlmZ eee
d	f ge
f Zeeef ZG d
d deZddddZdS )    )annotations)Queue)Threadcurrent_threadmain_thread)TYPE_CHECKINGAnyCallableClassVarDefaultDictLiteralTupleN   )EmitLoopError)WeakCallback.c                   @  sF   e Zd ZU dZeeZded< 	ddddZddddZ	dddZ
dS )QueuedCallbacka  WeakCallback that queues the callback to be called on a different thread.

    (...rather than invoking it immediately.)

    Parameters
    ----------
    wrapped : WeakCallback
        The actual callback to be invoked.
    thread : Thread | Literal["main", "current"] | None
        The thread on which to invoke the callback.  If not provided, the main
        thread will be used.
    z=ClassVar[collections.defaultdict[Thread, Queue[CbArgsTuple]]]_GLOBAL_QUEUENwrappedr   thread*Thread | Literal['main', 'current'] | NonereturnNonec                 C  s~   || _ |j| _|j| _|j| _|j| _|d u s|dkrt }n|dkr't }nt|ts6t	dt
|j || _|j| _d S )Nmaincurrentz(`thread` must be a Thread instance, not )_wrapped_keyZ	_max_args_aliveZ_on_ref_errorr   r   
isinstancer   	TypeErrortype__name__	_cbthreadpriority)selfr   r    r$   W/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/psygnal/_queue.py__init__$   s   
zQueuedCallback.__init__r$   argstuplec                 C  s:   t  | ju r| j| d S tj| j | jj|f d S N)r   r!   r   cbr   r   put)r#   r'   r$   r$   r%   r*   >   s   zQueuedCallback.cbCallable | Nonec                 C  s
   | j  S r)   )r   dereference)r#   r$   r$   r%   r-   D   s   
zQueuedCallback.dereferencer)   )r   r   r   r   r   r   )r$   )r'   r(   r   r   )r   r,   )r    
__module____qualname____doc__r   r   r   __annotations__r&   r*   r-   r$   r$   r$   r%   r      s   
 
r   r   Thread | Noner   r   c              
   C  sr   | du rt  n| }tj| }| s7| \}}z|| W n ty0 } zt|d|d}~ww | rdS dS )a)  Trigger emissions of all callbacks queued in the current thread.

    Parameters
    ----------
    thread : Thread, optional
        The thread on which to invoke the callback.  If not provided, the main
        thread will be used.

    Raises
    ------
    EmitLoopError
        If an exception is raised while invoking a queued callback.
        This exception can be caught and optionally suppressed or handled by the caller,
        allowing the emission of other queued callbacks to continue even if one of them
        raises an exception.
    N)exc)r   r   r   emptyget	Exceptionr   )r   _threadqueuer*   r'   er$   r$   r%   emit_queuedH   s   
r:   r)   )r   r2   r   r   )
__future__r   r8   r   	threadingr   r   r   typingr   r   r	   r
   r   r   r   collections_exceptionsr   Z_weak_callbackr   ZCallbackr(   ZCbArgsTupler   r:   r$   r$   r$   r%   <module>   s    $6