-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix ClientStateVar late mount default sync #6824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harsh21234i
wants to merge
3
commits into
reflex-dev:main
Choose a base branch
from
harsh21234i:fix/client-state-late-mount-default-sync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixed global `ClientStateVar` values getting stuck in late-mounted components when the shared value is pushed back to the default. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| """Integration tests for experimental client state vars.""" | ||
|
|
||
| from collections.abc import Generator | ||
|
|
||
| import pytest | ||
| from playwright.sync_api import Page, expect | ||
|
|
||
| from reflex.testing import AppHarness | ||
|
|
||
|
|
||
| def ClientStateLateMountApp(): | ||
| """App reproducing late-mounted global ClientStateVar synchronization.""" | ||
| import asyncio | ||
|
|
||
| import reflex as rx | ||
| from reflex.experimental import ClientStateVar | ||
|
|
||
| flag = ClientStateVar.create("flag", default="") | ||
|
|
||
| class State(rx.State): | ||
| mounted: bool = False | ||
|
|
||
| @rx.event(background=True) | ||
| async def go(self): | ||
| async with self: | ||
| self.mounted = False | ||
| yield flag.push("busy") | ||
| await asyncio.sleep(0.2) | ||
| async with self: | ||
| self.mounted = True | ||
| await asyncio.sleep(0.2) | ||
| yield flag.push("") | ||
|
|
||
| def index() -> rx.Component: | ||
| return rx.el.div( | ||
| rx.el.button("go", on_click=State.go, id="go"), | ||
| rx.el.div(flag.value, id="always"), | ||
| rx.cond(State.mounted, rx.el.div(flag.value, id="late")), | ||
| ) | ||
|
|
||
| app = rx.App() | ||
| app.add_page(index, route="/") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def client_state_late_mount_app( | ||
| tmp_path_factory: pytest.TempPathFactory, | ||
| ) -> Generator[AppHarness, None, None]: | ||
| """Run the client state late-mount repro app. | ||
|
|
||
| Args: | ||
| tmp_path_factory: Pytest fixture for creating temporary directories. | ||
|
|
||
| Yields: | ||
| The running harness. | ||
| """ | ||
| with AppHarness.create( | ||
| root=tmp_path_factory.mktemp("client_state_late_mount_app"), | ||
| app_source=ClientStateLateMountApp, | ||
| ) as harness: | ||
| yield harness | ||
|
|
||
|
|
||
| def test_late_mounted_global_client_state_rerenders_on_default_push( | ||
| client_state_late_mount_app: AppHarness, page: Page | ||
| ) -> None: | ||
| """A late-mounted consumer should update when the shared value returns to default. | ||
|
|
||
| Args: | ||
| client_state_late_mount_app: Running app harness. | ||
| page: Playwright page. | ||
| """ | ||
| assert client_state_late_mount_app.frontend_url is not None | ||
| page.goto(client_state_late_mount_app.frontend_url) | ||
|
|
||
| expect(page.locator("#always")).to_have_text("") | ||
| expect(page.locator("#late")).to_have_count(0) | ||
|
|
||
| page.click("#go") | ||
|
|
||
| expect(page.locator("#always")).to_have_text("busy") | ||
| expect(page.locator("#late")).to_have_text("busy") | ||
| expect(page.locator("#always")).to_have_text("") | ||
| expect(page.locator("#late")).to_have_text("") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """Tests for experimental client state vars.""" | ||
|
|
||
| from reflex.experimental.client_state import ClientStateVar | ||
|
|
||
|
|
||
| def _hooks(client_state_var: ClientStateVar) -> tuple[str, ...]: | ||
| """Get all hook strings for a client state var. | ||
|
|
||
| Returns: | ||
| The normalized hook strings. | ||
| """ | ||
| var_data = client_state_var._get_all_var_data() | ||
| assert var_data is not None | ||
| return var_data.hooks | ||
|
|
||
|
|
||
| def test_global_client_state_initializes_use_state_from_shared_ref(): | ||
| """Global client state should keep late-mounted components in sync.""" | ||
| flag = ClientStateVar.create("flag", default="") | ||
|
|
||
| hooks = _hooks(flag) | ||
|
|
||
| assert ( | ||
| """const [flag, setFlag] = useState(() => refs['_client_state_flag'] !== undefined ? refs['_client_state_flag'] : "")""" | ||
| in hooks | ||
| ) | ||
| assert """const [flag, setFlag] = useState("")""" not in hooks | ||
|
|
||
|
|
||
| def test_global_client_state_preserves_null_shared_ref(): | ||
| """Global client state should not replace explicit null with the default.""" | ||
| flag = ClientStateVar.create("flag", default="idle") | ||
|
|
||
| assert ( | ||
| """const [flag, setFlag] = useState(() => refs['_client_state_flag'] !== undefined ? refs['_client_state_flag'] : "idle")""" | ||
| in _hooks(flag) | ||
| ) | ||
|
|
||
|
|
||
| def test_global_client_state_without_default_uses_undefined_fallback(): | ||
| """Global client state without a default should still read the shared ref.""" | ||
| flag = ClientStateVar.create("flag") | ||
|
|
||
| assert ( | ||
| "const [flag, setFlag] = useState(() => refs['_client_state_flag'] !== undefined ? refs['_client_state_flag'] : undefined)" | ||
| in _hooks(flag) | ||
| ) | ||
|
|
||
|
|
||
| def test_local_client_state_keeps_plain_default_initializer(): | ||
| """Non-global client state should not read from the shared refs mirror.""" | ||
| flag = ClientStateVar.create("flag", default="", global_ref=False) | ||
|
|
||
| hooks = _hooks(flag) | ||
|
|
||
| assert """const [flag, setFlag] = useState("")""" in hooks | ||
| assert not any("refs['_client_state_flag']" in hook for hook in hooks) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.