|
24 | 24 | from reactpy.core.layout import Layout |
25 | 25 | from reactpy.core.serve import serve_layout |
26 | 26 | from reactpy.core.types import ComponentConstructor, VdomDict |
| 27 | +from concurrent.futures import Future |
27 | 28 |
|
28 | 29 | _logger = logging.getLogger(__name__) |
29 | 30 | _backhaul_loop = asyncio.new_event_loop() |
@@ -51,35 +52,40 @@ def __init__( |
51 | 52 | backhaul_thread: bool = True, |
52 | 53 | block_size: int = 8192, |
53 | 54 | ) -> 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 = ( |
55 | 66 | app_or_component |
56 | 67 | if isinstance(app_or_component, ComponentConstructor) |
57 | 68 | else None |
58 | 69 | ) |
59 | | - self.user_app = ( |
| 70 | + self.user_app: re.Pattern = ( |
60 | 71 | guarantee_single_callable(app_or_component) |
61 | 72 | if not self.component and asyncio.iscoroutinefunction(app_or_component) |
62 | 73 | else None |
63 | 74 | ) |
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( |
73 | 76 | "|".join( |
74 | 77 | path for path in [dispatcher_path, js_modules_path, static_path] if path |
75 | 78 | ) |
76 | 79 | ) |
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 | + ) |
83 | 89 | if self.backhaul_thread and not _backhaul_thread.is_alive(): |
84 | 90 | _backhaul_thread.start() |
85 | 91 |
|
|
0 commit comments