U
    /eb                     @  s   d Z ddlmZ ddlZeeZddlZddlm	Z	 ddl
mZmZmZmZmZ ddlmZmZ erddlmZ d	d
lmZmZ dZededef dZG dd dee ZdddddZdZG dd dZdS )z

    )annotationsN)wraps)TYPE_CHECKINGAnyCallableTypeVarcast)LiteralProtocol   )NextTickCallback   )CallbackDocument)UnlockedDocumentProxywithout_document_lockF.)boundc                   @  s   e Zd ZU ded< ded< dS )NoLockCallbackr   __call__zLiteral[True]nolockN)__name__
__module____qualname____annotations__ r   r   :/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/document/locking.pyr   9   s   
r   zNoLockCallback[F])funcreturnc                   s`   t  r*t dddd fdd}nt dddd fdd}ttt |}d|_|S )ap   Wrap a callback function to execute without first obtaining the
    document lock.

    Args:
        func (callable) : The function to wrap

    Returns:
        callable : a function wrapped to execute without a |Document| lock.

    While inside an unlocked callback, it is completely *unsafe* to modify
    ``curdoc()``. The value of ``curdoc()`` inside the callback will be a
    specially wrapped version of |Document| that only allows safe operations,
    which are:

    * :func:`~bokeh.document.Document.add_next_tick_callback`
    * :func:`~bokeh.document.Document.remove_next_tick_callback`

    Only these may be used safely without taking the document lock. To make
    other changes to the document, you must add a next tick callback and make
    your changes to ``curdoc()`` from that second callback.

    Attempts to otherwise access or change the Document will result in an
    exception being raised.

    ``func`` can be a synchronous function, an async function, or a function
    decorated with ``asyncio.coroutine``. The returned function will be an
    async function if ``func`` is any of the latter two.

    r   None)argskwr   c                    s    | |I d H  d S Nr   r    r!   r   r   r   _wrapper\   s    z'without_document_lock.<locals>._wrapperc                    s    | | d S r"   r   r#   r$   r   r   r%   `   s    T)asyncioiscoroutinefunctionr   r   r   r   r   )r   r%   wrapperr   r$   r   r   =   s    
r   zOnly 'add_next_tick_callback' may be used safely without taking the document lock; to make other changes to the document, add a next tick callback and make your changes from that callback.c                   @  sP   e Zd ZdZdddddZddd	d
dZdddddZdddddZdS )r   z Wrap a Document object so that only methods that can safely be used
    from unlocked callbacks or threads are exposed. Attempts to otherwise
    access or change the Document results in an exception.

    r   r   )docr   c                 C  s
   || _ dS z


        N)_doc)selfr)   r   r   r   __init__w   s    zUnlockedDocumentProxy.__init__strr   )attrr   c                 C  s   t tdS r*   )AttributeErrorUNSAFE_DOC_ATTR_USAGE_MSG)r,   r/   r   r   r   __getattr__}   s    z!UnlockedDocumentProxy.__getattr__r   r   )callbackr   c                 C  s   | j |S )zW Add a "next tick" callback.

        Args:
            callback (callable) :

        )r+   add_next_tick_callbackr,   r3   r   r   r   r4      s    z,UnlockedDocumentProxy.add_next_tick_callbackc                 C  s   | j | dS )zZ Remove a "next tick" callback.

        Args:
            callback (callable) :

        N)r+   remove_next_tick_callbackr5   r   r   r   r6      s    z/UnlockedDocumentProxy.remove_next_tick_callbackN)r   r   r   __doc__r-   r2   r4   r6   r   r   r   r   r   p   s
   	r   )r7   
__future__r   logging	getLoggerr   logr&   	functoolsr   typingr   r   r   r   r   Ztyping_extensionsr	   r
   Zserver.callbacksr   documentr   r   __all__r   r   r   r1   r   r   r   r   r   <module>   s"   
		-