浏览代码

Merge branch 'main' into release/reflex-0.7.9

Khaleel Al-Adhami 1 月之前
父节点
当前提交
c185bbe7ca
共有 5 个文件被更改,包括 35 次插入27 次删除
  1. 3 3
      reflex/.templates/web/utils/state.js
  2. 21 23
      reflex/app.py
  3. 8 0
      reflex/page.py
  4. 2 1
      reflex/reflex.py
  5. 1 0
      tests/units/test_app.py

+ 3 - 3
reflex/.templates/web/utils/state.js

@@ -243,13 +243,13 @@ export const applyEvent = async (event, socket) => {
   if (event.name == "_set_focus") {
     const ref =
       event.payload.ref in refs ? refs[event.payload.ref] : event.payload.ref;
-    const focus = ref?.current?.focus;
-    if (focus === undefined) {
+    const current = ref?.current;
+    if (current === undefined || current?.focus === undefined) {
       console.error(
         `No element found for ref ${event.payload.ref} in _set_focus`,
       );
     } else {
-      focus();
+      current.focus();
     }
     return false;
   }

+ 21 - 23
reflex/app.py

@@ -489,7 +489,7 @@ class App(MiddlewareMixin, LifespanMixin):
 
         # Set up the API.
         self._api = Starlette(lifespan=self._run_lifespan_tasks)
-        self._add_cors()
+        App._add_cors(self._api)
         self._add_default_endpoints()
 
         for clz in App.__mro__:
@@ -613,19 +613,6 @@ class App(MiddlewareMixin, LifespanMixin):
         Returns:
             The backend api.
         """
-        if self._cached_fastapi_app is not None:
-            asgi_app = self._cached_fastapi_app
-
-            if not asgi_app or not self._api:
-                raise ValueError("The app has not been initialized.")
-
-            asgi_app.mount("", self._api)
-        else:
-            asgi_app = self._api
-
-            if not asgi_app:
-                raise ValueError("The app has not been initialized.")
-
         # For py3.9 compatibility when redis is used, we MUST add any decorator pages
         # before compiling the app in a thread to avoid event loop error (REF-2172).
         self._apply_decorated_pages()
@@ -637,9 +624,17 @@ class App(MiddlewareMixin, LifespanMixin):
             # Force background compile errors to print eagerly
             lambda f: f.result()
         )
-        # Wait for the compile to finish in prod mode to ensure all optional endpoints are mounted.
-        if is_prod_mode():
-            compile_future.result()
+        # Wait for the compile to finish to ensure all optional endpoints are mounted.
+        compile_future.result()
+
+        if not self._api:
+            raise ValueError("The app has not been initialized.")
+        if self._cached_fastapi_app is not None:
+            asgi_app = self._cached_fastapi_app
+            asgi_app.mount("", self._api)
+            App._add_cors(asgi_app)
+        else:
+            asgi_app = self._api
 
         if self.api_transformer is not None:
             api_transformers: Sequence[Starlette | Callable[[ASGIApp], ASGIApp]] = (
@@ -651,6 +646,7 @@ class App(MiddlewareMixin, LifespanMixin):
             for api_transformer in api_transformers:
                 if isinstance(api_transformer, Starlette):
                     # Mount the api to the fastapi app.
+                    App._add_cors(api_transformer)
                     api_transformer.mount("", asgi_app)
                     asgi_app = api_transformer
                 else:
@@ -709,11 +705,14 @@ class App(MiddlewareMixin, LifespanMixin):
         if environment.REFLEX_ADD_ALL_ROUTES_ENDPOINT.get():
             self.add_all_routes_endpoint()
 
-    def _add_cors(self):
-        """Add CORS middleware to the app."""
-        if not self._api:
-            return
-        self._api.add_middleware(
+    @staticmethod
+    def _add_cors(api: Starlette):
+        """Add CORS middleware to the app.
+
+        Args:
+            api: The Starlette app to add CORS middleware to.
+        """
+        api.add_middleware(
             cors.CORSMiddleware,
             allow_credentials=True,
             allow_methods=["*"],
@@ -1118,7 +1117,6 @@ class App(MiddlewareMixin, LifespanMixin):
         # Add the @rx.page decorated pages to collect on_load events.
         for render, kwargs in DECORATED_PAGES[app_name]:
             self.add_page(render, **kwargs)
-        DECORATED_PAGES[app_name].clear()
 
     def _validate_var_dependencies(self, state: type[BaseState] | None = None) -> None:
         """Validate the dependencies of the vars in the app.

+ 8 - 0
reflex/page.py

@@ -8,6 +8,7 @@ from typing import Any
 
 from reflex.config import get_config
 from reflex.event import EventType
+from reflex.utils import console
 
 DECORATED_PAGES: dict[str, list] = defaultdict(list)
 
@@ -76,6 +77,13 @@ def get_decorated_pages(omit_implicit_routes: bool = True) -> list[dict[str, Any
     Returns:
         The decorated pages.
     """
+    console.deprecate(
+        "get_decorated_pages",
+        reason="This function is deprecated and will be removed in a future version.",
+        deprecation_version="0.7.9",
+        removal_version="0.8.0",
+        dedupe=True,
+    )
     return sorted(
         [
             page_data

+ 2 - 1
reflex/reflex.py

@@ -354,6 +354,7 @@ def run(
 @click.option(
     "--zip/--no-zip",
     default=True,
+    is_flag=True,
     help="Whether to zip the backend and frontend exports.",
 )
 @click.option(
@@ -569,7 +570,7 @@ def makemigrations(message: str | None):
     help="The hostname of the frontend.",
 )
 @click.option(
-    "--interactive",
+    "--interactive/--no-interactive",
     is_flag=True,
     default=True,
     help="Whether to list configuration options and ask for confirmation.",

+ 1 - 0
tests/units/test_app.py

@@ -1502,6 +1502,7 @@ def test_raise_on_state():
 def test_call_app():
     """Test that the app can be called."""
     app = App()
+    app._compile = unittest.mock.Mock()
     api = app()
     assert isinstance(api, Starlette)