2929from idom .core .layout import LayoutEvent , LayoutUpdate
3030from idom .core .serve import serve_json_patch
3131from idom .core .types import ComponentType , RootComponentConstructor
32+ from idom .server .types import Location
3233from idom .utils import Ref
3334
3435from .utils import safe_client_build_dir_path , safe_web_modules_dir_path
@@ -110,6 +111,23 @@ def run_server() -> None:
110111 raise RuntimeError ("Failed to shutdown server." )
111112
112113
114+ def use_location () -> Location :
115+ """Get the current route as a string"""
116+ conn = use_connection ()
117+ search = conn .request .query_string .decode ()
118+ return Location (pathname = "/" + conn .path , search = "?" + search if search else "" )
119+
120+
121+ def use_scope () -> dict [str , Any ]:
122+ """Get the current WSGI environment"""
123+ return use_request ().environ
124+
125+
126+ def use_request () -> Request :
127+ """Get the current ``Request``"""
128+ return use_connection ().request
129+
130+
113131def use_connection () -> Connection :
114132 """Get the current :class:`Connection`"""
115133 connection = use_context (ConnectionContext )
@@ -120,14 +138,18 @@ def use_connection() -> Connection:
120138 return connection
121139
122140
123- def use_request () -> Request :
124- """Get the current ``Request``"""
125- return use_connection (). request
141+ @ dataclass
142+ class Connection :
143+ """A simple wrapper for holding connection information"""
126144
145+ request : Request
146+ """The current request object"""
127147
128- def use_scope () -> dict [str , Any ]:
129- """Get the current WSGI environment"""
130- return use_request ().environ
148+ websocket : WebSocket
149+ """A handle to the current websocket"""
150+
151+ path : str
152+ """The current path being served"""
131153
132154
133155@dataclass
@@ -181,13 +203,13 @@ def send(value: Any) -> None:
181203 def recv () -> LayoutEvent :
182204 return LayoutEvent (** json .loads (ws .receive ()))
183205
184- dispatch_in_thread (ws , path , constructor (), send , recv )
206+ _dispatch_in_thread (ws , path , constructor (), send , recv )
185207
186208 sock .route ("/_api/stream" , endpoint = "without_path" )(model_stream )
187209 sock .route ("/<path:path>/_api/stream" , endpoint = "with_path" )(model_stream )
188210
189211
190- def dispatch_in_thread (
212+ def _dispatch_in_thread (
191213 websocket : WebSocket ,
192214 path : str ,
193215 component : ComponentType ,
@@ -260,20 +282,6 @@ def run_send() -> None:
260282 )
261283
262284
263- @dataclass
264- class Connection :
265- """A simple wrapper for holding connection information"""
266-
267- request : Request
268- """The current request object"""
269-
270- websocket : WebSocket
271- """A handle to the current websocket"""
272-
273- path : str
274- """The current path being served"""
275-
276-
277285class _DispatcherThreadInfo (NamedTuple ):
278286 dispatch_loop : asyncio .AbstractEventLoop
279287 dispatch_future : "asyncio.Future[Any]"
0 commit comments