U
    õÕ/ez  ã                   @  sœ   d Z ddlmZ ddlZe e¡ZddlmZm	Z	m
Z
 er†ddlmZ ddlmZmZ ddlmZ d	d
lmZ d	dlmZ d	dlmZ dZG dd„ dƒZdS )z( Provides the ``ServerSession`` class.

é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚ	Awaitableé   )ÚDocumentPatchedEvent)ÚProtocolÚmessages)ÚMessageé   )ÚApplicationContext)ÚServerSession)Ú	WSHandler)ÚServerConnectionc                   @  s´   e Zd ZU dZded< dddddd	œd
d„Zeddœdd„ƒZeddœdd„ƒZddœdd„Z	dddœdd„Z
ddddœdd„Zdddœd d!„Zddœd"d#„Zeddœd$d%„ƒZd&S )'r   zG Wraps a websocket connection to a client.

    .. autoclasstoc::

    zServerSession | NoneÚ_sessionr   r   r   r   ÚNone)ÚprotocolÚsocketÚapplication_contextÚsessionÚreturnc                 C  s.   || _ || _|| _|| _| j | ¡ d| _d S )Nr   )Ú	_protocolÚ_socketÚ_application_contextr   Ú	subscribeÚ_ping_count)Úselfr   r   r   r   © r   ú;/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/server/connection.pyÚ__init__8   s    zServerConnection.__init__)r   c                 C  s   | j d k	st‚| j S ©N)r   ÚAssertionError©r   r   r   r   r   A   s    zServerConnection.sessionc                 C  s   | j S r    )r   r"   r   r   r   r   F   s    z$ServerConnection.application_contextc                 C  s    | j dk	r| j  | ¡ d| _ dS )zTAllow the session to be discarded and don't get change notifications from it anymoreN)r   Zunsubscriber"   r   r   r   Údetach_sessionJ   s    
zServerConnection.detach_sessionzMessage[Any]zmsg.ok)Úmessager   c                 C  s   | j  d|jd ¡S )NÚOKÚmsgid©r   ÚcreateÚheader)r   r$   r   r   r   ÚokP   s    zServerConnection.okÚstrz	msg.error)r$   Útextr   c                 C  s   | j  d|jd |¡S )NÚERRORr&   r'   )r   r$   r,   r   r   r   ÚerrorS   s    zServerConnection.errorr   zAwaitable[None])Úeventr   c                 C  s   | j  d|g¡}| j |¡S )zW Sends a PATCH-DOC message, returning a Future that's completed when it's written out. z	PATCH-DOC)r   r(   r   Úsend_message)r   r/   Úmsgr   r   r   Úsend_patch_documentV   s    z$ServerConnection.send_patch_documentc                 C  s*   | j  t| jƒ d¡¡ |  jd7  _d S )Nzutf-8r   )r   Zpingr+   r   Úencoder"   r   r   r   Ú	send_ping\   s    zServerConnection.send_pingc                 C  s   | j S r    )r   r"   r   r   r   r   `   s    zServerConnection.protocolN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   Úpropertyr   r   r#   r*   r.   r2   r4   r   r   r   r   r   r   /   s   
	r   )r8   Ú
__future__r   ÚloggingÚ	getLoggerr5   ÚlogÚtypingr   r   r   Zdocument.eventsr   r   r   r	   r1   Zprotocol.messager
   Zcontextsr   r   r   Zviews.wsr   Ú__all__r   r   r   r   r   Ú<module>   s   
