Skip to content

Commit 5707fba

Browse files
committed
refactor init
1 parent 32b367c commit 5707fba

File tree

1 file changed

+23
-17
lines changed
  • src/py/reactpy/reactpy/backend

1 file changed

+23
-17
lines changed

src/py/reactpy/reactpy/backend/asgi.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from reactpy.core.layout import Layout
2525
from reactpy.core.serve import serve_layout
2626
from reactpy.core.types import ComponentConstructor, VdomDict
27+
from concurrent.futures import Future
2728

2829
_logger = logging.getLogger(__name__)
2930
_backhaul_loop = asyncio.new_event_loop()
@@ -51,35 +52,40 @@ def __init__(
5152
backhaul_thread: bool = True,
5253
block_size: int = 8192,
5354
) -> None:
54-
self.component = (
55+
# Convert kwargs to class attributes
56+
self.dispatch_path = re.compile(dispatcher_path)
57+
self.js_modules_path = re.compile(js_modules_path) if js_modules_path else None
58+
self.static_path = re.compile(static_path) if static_path else None
59+
self.static_dir = static_dir
60+
self.head = vdom_head_elements_to_html(head)
61+
self.backhaul_thread = backhaul_thread
62+
self.block_size = block_size
63+
64+
# Internal attributes (not using the same name as a kwarg)
65+
self.component: re.Pattern = (
5566
app_or_component
5667
if isinstance(app_or_component, ComponentConstructor)
5768
else None
5869
)
59-
self.user_app = (
70+
self.user_app: re.Pattern = (
6071
guarantee_single_callable(app_or_component)
6172
if not self.component and asyncio.iscoroutinefunction(app_or_component)
6273
else None
6374
)
64-
if not self.component and not self.user_app:
65-
raise TypeError(
66-
"The first argument to ReactPy(...) must be a component or an ASGI application."
67-
)
68-
self.dispatch_path = re.compile(dispatcher_path)
69-
self.js_modules_path = re.compile(js_modules_path) if js_modules_path else None
70-
self.static_path = re.compile(static_path) if static_path else None
71-
self.static_dir = static_dir
72-
self.all_paths = re.compile(
75+
self.all_paths: re.Pattern = re.compile(
7376
"|".join(
7477
path for path in [dispatcher_path, js_modules_path, static_path] if path
7578
)
7679
)
77-
self.head = vdom_head_elements_to_html(head)
78-
self._cached_index_html = ""
79-
self.connected = False
80-
self.backhaul_thread = backhaul_thread
81-
self.dispatcher = None
82-
self.block_size = block_size
80+
self.dispatcher: Future | asyncio.Task | None = None
81+
self._cached_index_html: str = ""
82+
self.connected: bool = False
83+
84+
# Validate the arguments
85+
if not self.component and not self.user_app:
86+
raise TypeError(
87+
"The first argument to ReactPy(...) must be a component or an ASGI application."
88+
)
8389
if self.backhaul_thread and not _backhaul_thread.is_alive():
8490
_backhaul_thread.start()
8591

0 commit comments

Comments
 (0)