
    /_i                        U d dl mZ d dl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rd dlmZ d dlmZ d dlmZ d d	lmZ  ee      Zd
ed<    G d de      Zy)    )annotations)TYPE_CHECKINGFinalcast)
get_logger)
AppSession)ActiveSessionInfoSessionClientSessionInfoSessionManagerSessionStorage)Callable)
ScriptData)ScriptCache)UploadedFileManagerr   _LOGGERc                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZddZddZddZ	dd	Z
dd
ZddZy)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.
    c                J    || _         || _        || _        || _        i | _        y N)_session_storage_uploaded_file_mgr_script_cache_message_enqueued_callback_active_session_info_by_id)selfsession_storageuploaded_file_managerscript_cachemessage_enqueued_callbacks        j/var/www/html/land_sniper/venv/lib/python3.12/site-packages/streamlit/runtime/websocket_session_manager.py__init__z WebsocketSessionManager.__init__1   s0     !0"7)*C' IK'    Nc                N   |r|rt        d      || j                  v rt        j                  d|       |xr+ || j                  vxr | j                  j                  |      }t        |t              r{|j                  }|j                          t        |||j                        | j                  |j                  <   | j                  j                  |j                         |j                  S t        || j                  | j                   | j"                  ||      }t        j%                  dt        |      |j                         |j                  | j                  v rt        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.)script_datar   r   r    	user_infosession_id_overridez1Created 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   clientr%   r&   existing_session_idr'   session_infoexisting_sessionr,   s	            r!   connect_sessionz'WebsocketSessionManager.connect_session@   s    #6, 
 $"A"AAOOW#   ?#4+J+JJ?%%))*=> 	 lK0+33335CT --DD++,<,?,?@
 !!(()9)<)<=#&&&#"&"9"9++&*&E&E 3
 	?FWZZ	
 ::888wzzl +, , 
 7HPW6X''

3zzr#   c                `   || j                   v ry| j                   |   }|j                  }|j                          |j                          | j                  j                  t        d ||j                               | j                   |= | j                   s| j                  j                          y y )N)r2   r,   r.   )
r   r,   request_script_stopdisconnect_file_watchersr   saver   r.   r   clear)r   
session_idactive_session_infor,   s       r!   disconnect_sessionz*WebsocketSessionManager.disconnect_session}   s    888"&"A"A*"M)11G''),,.!!&&#%8%I%I //
;..$$& /r#   c                8    | j                   j                  |      S r   )r   r*   r   r<   s     r!   get_active_session_infoz/WebsocketSessionManager.get_active_session_info   s    ..22:>>r#   c                    || j                   v S r   )r   r@   s     r!   is_active_sessionz)WebsocketSessionManager.is_active_session   s    T<<<<r#   c                H    t        | j                  j                               S r   )listr   valuesr   s    r!   list_active_sessionsz,WebsocketSessionManager.list_active_sessions   s    D33::<==r#   c                   || j                   v r]| j                   |   }| j                   |= |j                  j                          | j                   s| j                  j	                          y | j
                  j                  |      }|r6| j
                  j                  |       |j                  j                          y y r   )r   r,   shutdownr   r;   r   r*   r0   )r   r<   r=   r4   s       r!   close_sessionz%WebsocketSessionManager.close_session   s    888"&"A"A*"M//
;''00222""((*,,00<!!((4  ))+ r#   c                v    | j                  |      }|rt        d|      S | j                  j                  |      S )Nr   )rA   r   r   r*   )r   r<   r4   s      r!   get_session_infoz(WebsocketSessionManager.get_session_info   s9    33J?|44$$((44r#   c                l    t        d| j                               | j                  j                         z   S )Nlist[SessionInfo])r   rH   r   rE   rG   s    r!   list_sessionsz%WebsocketSessionManager.list_sessions   s2    $d&?&?&AB##((*+	
r#   )
r   r   r   r   r   r   r    zCallable[[], None] | NonereturnNone)NN)r2   r
   r%   r   r&   zdict[str, str | bool | None]r3   
str | Noner'   rS   rQ   str)r<   rT   rQ   rR   )r<   rT   rQ   zActiveSessionInfo | None)r<   rT   rQ   bool)rQ   zlist[ActiveSessionInfo])r<   rT   rQ   zSessionInfo | None)rQ   rO   )__name__
__module____qualname____doc__r"   r6   r>   rA   rC   rH   rK   rM   rP    r#   r!   r   r   '   s    K'K  3K "	K
 $=K 
K( +/*.;;  ; 0	;
 (; (; 
;z'*?=>, 5
r#   r   N)
__future__r   typingr   r   r   streamlit.loggerr   streamlit.runtime.app_sessionr   !streamlit.runtime.session_managerr	   r
   r   r   r   collections.abcr   streamlit.runtime.script_datar   +streamlit.runtime.scriptrunner.script_cacher   'streamlit.runtime.uploaded_file_managerr   rV   r   __annotations__r   rZ   r#   r!   <module>re      sN    # - - ' 4  (8GKH% %N
n N
r#   