29
29
if TYPE_CHECKING :
30
30
from django .contrib .auth .models import AbstractUser
31
31
32
+ from reactpy_django import models
33
+
32
34
_logger = logging .getLogger (__name__ )
33
35
BACKHAUL_LOOP = asyncio .new_event_loop ()
34
36
@@ -47,9 +49,18 @@ def start_backhaul_loop():
47
49
class ReactpyAsyncWebsocketConsumer (AsyncJsonWebsocketConsumer ):
48
50
"""Communicates with the browser to perform actions on-demand."""
49
51
52
+ def __init__ (self , * args , ** kwargs ):
53
+ super ().__init__ (* args , ** kwargs )
54
+
55
+ # New WebsocketConsumer attributes created by ReactPy
56
+ self .dispatcher : Future | asyncio .Task
57
+ self .threaded : bool
58
+ self .recv_queue : asyncio .Queue
59
+ self .dotted_path : str
60
+ self .component_session : "models.ComponentSession" | None = None
61
+
50
62
async def connect (self ) -> None :
51
63
"""The browser has connected."""
52
- from reactpy_django import models
53
64
from reactpy_django .config import (
54
65
REACTPY_AUTH_BACKEND ,
55
66
REACTPY_AUTO_RELOGIN ,
@@ -79,9 +90,7 @@ async def connect(self) -> None:
79
90
)
80
91
81
92
# Start the component dispatcher
82
- self .dispatcher : Future | asyncio .Task
83
93
self .threaded = REACTPY_BACKHAUL_THREAD
84
- self .component_session : models .ComponentSession | None = None
85
94
if self .threaded :
86
95
if not BACKHAUL_THREAD .is_alive ():
87
96
await asyncio .to_thread (
@@ -149,14 +158,14 @@ async def run_dispatcher(self):
149
158
)
150
159
151
160
scope = self .scope
152
- self .dotted_path = dotted_path = scope ["url_route" ]["kwargs" ]["dotted_path" ]
161
+ self .dotted_path = scope ["url_route" ]["kwargs" ]["dotted_path" ]
153
162
uuid = scope ["url_route" ]["kwargs" ].get ("uuid" )
154
163
has_args = scope ["url_route" ]["kwargs" ].get ("has_args" )
155
164
scope ["reactpy" ] = {"id" : str (uuid )}
156
165
query_string = parse_qs (scope ["query_string" ].decode (), strict_parsing = True )
157
166
http_pathname = query_string .get ("http_pathname" , ["" ])[0 ]
158
167
http_search = query_string .get ("http_search" , ["" ])[0 ]
159
- self .recv_queue : asyncio . Queue = asyncio .Queue ()
168
+ self .recv_queue = asyncio .Queue ()
160
169
connection = Connection ( # For `use_connection`
161
170
scope = scope ,
162
171
location = Location (pathname = http_pathname , search = http_search ),
@@ -168,11 +177,11 @@ async def run_dispatcher(self):
168
177
169
178
# Verify the component has already been registered
170
179
try :
171
- root_component_constructor = REACTPY_REGISTERED_COMPONENTS [dotted_path ]
180
+ root_component_constructor = REACTPY_REGISTERED_COMPONENTS [self . dotted_path ]
172
181
except KeyError :
173
182
await asyncio .to_thread (
174
183
_logger .warning ,
175
- f"Attempt to access invalid ReactPy component: { dotted_path !r} " ,
184
+ f"Attempt to access invalid ReactPy component: { self . dotted_path !r} " ,
176
185
)
177
186
return
178
187
@@ -194,7 +203,7 @@ async def run_dispatcher(self):
194
203
except models .ComponentSession .DoesNotExist :
195
204
await asyncio .to_thread (
196
205
_logger .warning ,
197
- f"Component session for '{ dotted_path } :{ uuid } ' not found. The "
206
+ f"Component session for '{ self . dotted_path } :{ uuid } ' not found. The "
198
207
"session may have already expired beyond REACTPY_SESSION_MAX_AGE. "
199
208
"If you are using a custom `host`, you may have forgotten to provide "
200
209
"args/kwargs." ,
0 commit comments