o
    Zh                     @  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!   l/var/www/html/Persson_Maskin/env/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t d|| jv rtd| |o|| jvo| j|}t|trA|j}|	  t
|||j| j|j< | j|j |jS t|| j| j| j||d}tdt||j |j| jv rgt d|j dt
||| j|j< |jS )NzcOnly one of existing_session_id and session_id_override should be truthy. This should never happen.zESession 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 'z6' registered multiple times. This should never happen.)RuntimeErrorr   r   warningr   get
isinstancer   sessionregister_file_watchersr	   script_run_countiddeleter   r   r   r   debug)	r    r$   r%   r&   r(   r*   session_infoexisting_sessionr0   r!   r!   r"   connect_session>   sR   


	z'WebsocketSessionManager.connect_session
session_idc                 C  sb   || j v r%| j | }|j}|  |  | jtd ||jd | j |= | j s/| j	  d S d S )N)r$   r0   r2   )
r   r0   request_script_stopdisconnect_file_watchersr   saver   r2   r   clear)r    r9   active_session_infor0   r!   r!   r"   disconnect_session{   s    

z*WebsocketSessionManager.disconnect_sessionActiveSessionInfo | Nonec                 C  s   | j |S r   )r   r.   r    r9   r!   r!   r"   get_active_session_info   s   z/WebsocketSessionManager.get_active_session_infoboolc                 C  s
   || j v S r   )r   rA   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  sh   || j v r| j | }| j |= |j  | j s| j  d S | j|}|r2| j| |j  d S d S r   )r   r0   shutdownr   r=   r   r.   r4   )r    r9   r>   r6   r!   r!   r"   close_session   s   



z%WebsocketSessionManager.close_sessionSessionInfo | Nonec                 C  s$   |  |}|rtd|S | j|S )Nr   )rB   r   r   r.   )r    r9   r6   r!   r!   r"   get_session_info   s   

z(WebsocketSessionManager.get_session_infolist[SessionInfo]c                 C  s   t d|  | j  S )NrN   )r   rI   r   rF   rH   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+   )r9   r+   r   r   )r9   r+   r   r@   )r9   r+   r   rC   )r   rE   )r9   r+   r   rL   )r   rN   )__name__
__module____qualname____doc__r#   r8   r?   rB   rD   rI   rK   rM   rO   r!   r!   r!   r"   r   %   s    
	
=




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   rP   r   __annotations__r   r!   r!   r!   r"   <module>   s   