12
12
from tornado .platform .asyncio import AsyncIOMainLoop
13
13
from tornado .web import Application , RedirectHandler , RequestHandler , StaticFileHandler
14
14
from tornado .websocket import WebSocketHandler
15
+ from tornado .wsgi import WSGIContainer
15
16
from typing_extensions import TypedDict
16
17
17
18
from idom .config import IDOM_WEB_MODULES_DIR
18
- from idom .core .hooks import use_context
19
+ from idom .core .hooks import Context , create_context , use_context
19
20
from idom .core .layout import Layout , LayoutEvent
20
21
from idom .core .serve import VdomJsonPatch , serve_json_patch
21
22
from idom .core .types import ComponentConstructor
22
23
23
- from ._conn import Connection
24
24
from .utils import CLIENT_BUILD_DIR
25
25
26
26
27
+ RequestContext : type [Context [HTTPServerRequest | None ]] = create_context (
28
+ None , "RequestContext"
29
+ )
30
+
31
+
27
32
def configure (
28
33
app : Application ,
29
34
component : ComponentConstructor ,
@@ -72,12 +77,17 @@ async def serve_development_app(
72
77
await server .close_all_connections ()
73
78
74
79
75
- def use_connection () -> HTTPServerRequest :
80
+ def use_request () -> HTTPServerRequest :
76
81
"""Get the current ``HTTPServerRequest``"""
77
- value = use_context (Connection )
78
- if value is None :
79
- raise RuntimeError ("No established connection." )
80
- return value
82
+ request = use_context (RequestContext )
83
+ if request is None :
84
+ raise RuntimeError ("No request. Are you running with a Tornado server?" )
85
+ return request
86
+
87
+
88
+ def use_scope () -> dict [str , Any ]:
89
+ """Get the current WSGI environment dictionary"""
90
+ return WSGIContainer .environ (use_request ())
81
91
82
92
83
93
class Options (TypedDict , total = False ):
@@ -160,7 +170,6 @@ def initialize(self, component_constructor: ComponentConstructor) -> None:
160
170
161
171
async def open (self , * args : str , ** kwargs : str ) -> None :
162
172
message_queue : "AsyncQueue[str]" = AsyncQueue ()
163
- query_params = {k : v [0 ].decode () for k , v in self .request .arguments .items ()}
164
173
165
174
async def send (value : VdomJsonPatch ) -> None :
166
175
await self .write_message (json .dumps (value ))
@@ -171,7 +180,9 @@ async def recv() -> LayoutEvent:
171
180
self ._message_queue = message_queue
172
181
self ._dispatch_future = asyncio .ensure_future (
173
182
serve_json_patch (
174
- Layout (Connection (self ._component_constructor (), value = self .request )),
183
+ Layout (
184
+ RequestContext (self ._component_constructor (), value = self .request )
185
+ ),
175
186
send ,
176
187
recv ,
177
188
)
0 commit comments