29
29
from idom .core .layout import LayoutEvent , LayoutUpdate
30
30
from idom .core .serve import serve_json_patch
31
31
from idom .core .types import ComponentType , RootComponentConstructor
32
+ from idom .server .types import Location
32
33
from idom .utils import Ref
33
34
34
35
from .utils import safe_client_build_dir_path , safe_web_modules_dir_path
@@ -110,6 +111,23 @@ def run_server() -> None:
110
111
raise RuntimeError ("Failed to shutdown server." )
111
112
112
113
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
+
113
131
def use_connection () -> Connection :
114
132
"""Get the current :class:`Connection`"""
115
133
connection = use_context (ConnectionContext )
@@ -120,14 +138,18 @@ def use_connection() -> Connection:
120
138
return connection
121
139
122
140
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"""
126
144
145
+ request : Request
146
+ """The current request object"""
127
147
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"""
131
153
132
154
133
155
@dataclass
@@ -181,13 +203,13 @@ def send(value: Any) -> None:
181
203
def recv () -> LayoutEvent :
182
204
return LayoutEvent (** json .loads (ws .receive ()))
183
205
184
- dispatch_in_thread (ws , path , constructor (), send , recv )
206
+ _dispatch_in_thread (ws , path , constructor (), send , recv )
185
207
186
208
sock .route ("/_api/stream" , endpoint = "without_path" )(model_stream )
187
209
sock .route ("/<path:path>/_api/stream" , endpoint = "with_path" )(model_stream )
188
210
189
211
190
- def dispatch_in_thread (
212
+ def _dispatch_in_thread (
191
213
websocket : WebSocket ,
192
214
path : str ,
193
215
component : ComponentType ,
@@ -260,20 +282,6 @@ def run_send() -> None:
260
282
)
261
283
262
284
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
-
277
285
class _DispatcherThreadInfo (NamedTuple ):
278
286
dispatch_loop : asyncio .AbstractEventLoop
279
287
dispatch_future : "asyncio.Future[Any]"
0 commit comments