o
    h                     @  s   U d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZmZ erAd dlmZ d dlmZ d dlmZ eeZd	ed
< G dd deZdS )    )annotations)TYPE_CHECKINGCallableFinalcast)
get_logger)
AppSession)ActiveSessionInfoSessionClientSessionInfoSessionManagerSessionStorage)
ScriptData)ScriptCache)UploadedFileManagerr   _LOGGERc                   @  sp   e Zd ZdZd/ddZ		d0d1ddZd2ddZd3dd Zd4d"d#Zd5d%d&Z	d2d'd(Z
d6d*d+Zd7d-d.ZdS )8WebsocketSessionManagera  A SessionManager used to manage sessions with lifecycles tied to those of a
    browser tab's websocket connection.

    WebsocketSessionManagers differentiate between "active" and "inactive" sessions.
    Active sessions are those with a currently active websocket connection. Inactive
    sessions are sessions without. Eventual cleanup of inactive sessions is a detail left
    to the specific SessionStorage that a WebsocketSessionManager is instantiated with.
    session_storager   uploaded_file_managerr   script_cacher   message_enqueued_callbackCallable[[], None] | NonereturnNonec                 C  s"   || _ || _|| _|| _i | _d S N)_session_storage_uploaded_file_mgr_script_cache_message_enqueued_callback_active_session_info_by_id)selfr   r   r   r    r!   _/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/websocket_session_manager.py__init__/   s
   
z WebsocketSessionManager.__init__Nclientr
   script_datar   	user_infodict[str, str | bool | None]existing_session_id
str | Nonesession_id_overridestrc           	      C  s   |r|rJ d|| j v rtd| |o|| j vo| j|}|r>|j}|  t|||j| j |j	< | j
|j	 |j	S t|| j| j| j||d}tdt	||j	 |j	| j vsdJ d|j	 dt||| j |j	< |j	S )NzHOnly one of existing_session_id and session_id_override should be truthyzESession with id %s is already connected! Connecting to a new session.)r%   r   r   r   r&   r*   z1Created new session for client %s. Session ID: %szsession.id 'z' registered multiple times!)r   r   warningr   getsessionregister_file_watchersr	   script_run_countiddeleter   r   r   r   debug)	r    r$   r%   r&   r(   r*   session_infoexisting_sessionr.   r!   r!   r"   connect_session>   sN   


	z'WebsocketSessionManager.connect_session
session_idc                 C  sR   || j v r'| j | }|j}|  |  | jtd ||jd | j |= d S d S )N)r$   r.   r0   )r   r.   request_script_stopdisconnect_file_watchersr   saver   r0   )r    r7   active_session_infor.   r!   r!   r"   disconnect_sessionw   s   

z*WebsocketSessionManager.disconnect_sessionActiveSessionInfo | Nonec                 C  s   | j |S r   )r   r-   r    r7   r!   r!   r"   get_active_session_info   s   z/WebsocketSessionManager.get_active_session_infoboolc                 C  s
   || j v S r   )r   r>   r!   r!   r"   is_active_session   s   
z)WebsocketSessionManager.is_active_sessionlist[ActiveSessionInfo]c                 C  s   t | j S r   )listr   valuesr    r!   r!   r"   list_active_sessions   s   z,WebsocketSessionManager.list_active_sessionsc                 C  sX   || j v r| j | }| j |= |j  d S | j|}|r*| j| |j  d S d S r   )r   r.   shutdownr   r-   r2   )r    r7   r;   r4   r!   r!   r"   close_session   s   


z%WebsocketSessionManager.close_sessionSessionInfo | Nonec                 C  s$   |  |}|rtd|S | j|S )Nr   )r?   r   r   r-   )r    r7   r4   r!   r!   r"   get_session_info   s   

z(WebsocketSessionManager.get_session_infolist[SessionInfo]c                 C  s   t d|  | j  S )NrK   )r   rF   r   rC   rE   r!   r!   r"   list_sessions   s   z%WebsocketSessionManager.list_sessions)
r   r   r   r   r   r   r   r   r   r   )NN)r$   r
   r%   r   r&   r'   r(   r)   r*   r)   r   r+   )r7   r+   r   r   )r7   r+   r   r=   )r7   r+   r   r@   )r   rB   )r7   r+   r   rI   )r   rK   )__name__
__module____qualname____doc__r#   r6   r<   r?   rA   rF   rH   rJ   rL   r!   r!   r!   r"   r   %   s    
	
9




r   N)
__future__r   typingr   r   r   r   streamlit.loggerr   streamlit.runtime.app_sessionr   !streamlit.runtime.session_managerr	   r
   r   r   r   streamlit.runtime.script_datar   +streamlit.runtime.scriptrunner.script_cacher   'streamlit.runtime.uploaded_file_managerr   rM   r   __annotations__r   r!   r!   r!   r"   <module>   s   