Skip to content

fix(client-state): let global_value and global_set run in call_script code - #7946

Open
benedikt-bartscher wants to merge 4 commits into
reflex-dev:mainfrom
benedikt-bartscher:fix/client-state-in-call-script
Open

benedikt-bartscher wants to merge 4 commits into
reflex-dev:mainfrom
benedikt-bartscher:fix/client-state-in-call-script

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Since #7870, code sent with rx.call_script runs through runScript, which can only see the exports of state.js. ClientStateVar.global_value and global_set compile to getClientState(...) / setClientState(...). state.js imports those two functions but doesn't export them, so both accessors throw ReferenceError in call_script code. The same goes for push() of a Var that reads another client state var, e.g. a.push(rx.cond(b.global_value == x, ..., ...)). Until now the only way around it was writing refs["__client_state"] by hand.

  • state.js now exports getClientState and setClientState, so both accessors work in script code.
  • push(Var) still writes through refs["__client_state"], but builds the call as a typed FunctionStringVar call, which keeps the imports of the Var it is given (formatting it with !s dropped them). A page that compiles the push into a trigger now imports getClientState itself, instead of relying on another component in the same module to import it.
  • Docs: replaced "rx.call_script is the one place these do not work" with an example.

Tests: a vitest test checks what script code can see, two Playwright tests cover call_script with global_set/global_value and a push reading another var, and a unit test pins the generated push code and its imports. Without the export, the Playwright tests fail with ReferenceError: setClientState is not defined; without the change to push, the unit test finds no imports on the generated function.

View guided diff

… code

Server-provided scripts run through runScript, which passes only the exports
of state.js (reflex-dev#7870). ClientStateVar.global_value and global_set compile to
getClientState and setClientState, which state.js imports but did not export,
so both raised ReferenceError in rx.call_script code, and so did push() of a
Var that reads another client state var's global_value.

state.js now re-exports both functions. push() of a Var writes through
global_set, so a page that compiles the push into one of its triggers also
imports what the expression calls.
@benedikt-bartscher
benedikt-bartscher requested review from a team and Alek99 as code owners November 20, 2026 08:14

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 8 files

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable new issues were found.

Summary

Makes named client state available in rx.call_script and preserves imports when push receives a Var.

  • Client-state accessors work in rx.call_script code.
  • A pushed Var keeps the imports needed to read another client-state value.

No actionable new issues were found. Tests were inspected, not run.

Reviews (5) · Last reviewed commit: "fix(client-state): build push's script a..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 21 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing benedikt-bartscher:fix/client-state-in-call-script (68e94a2) with main (df7f157)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment on lines +681 to +683
# it on the client instead, through `global_set`, whose import comes
# along when a page compiles the push into one of its triggers.
return run_script(self.global_set.to(FunctionVar).call(value))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop this rewrite and keep the refs path from main. The new state.js export alone fixes the bug.

Evidence: I ran main plus only export { getClientState, setClientState } in state.js, in dev and in a prod build. A backend push(other.global_value.to(str) + "!") sets the value, and the store access stays refs["__client_state"].set(...). rx.call_script with global_set and with global_value also works.

The page-trigger case does not need this change. On main, on_click=echoed.push(shared.global_value.to(str) + "?") already works in dev and prod, because _client_state_store_ref brings the refs import.

refs is the entry point that #6936 documents for script code. Please restore _client_state_store_ref and the refs assertion in test_push_var_is_evaluated_on_the_client. The new other.global_value input in that test can stay.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ad87682: push writes through refs["__client_state"] again, _client_state_store_ref is back, and the unit test asserts the exact refs code, with the other.global_value input.

One part of the rewrite fixed a real bug, so I kept that part. Main formats the Vars with !s, which drops their var data, so the function push builds carries no imports at all (_get_all_var_data() is None). The trigger case works in test_client_state.py only because every component of that app compiles into one module (app_components/clientstateapp/clientstateapp.jsx), where other components already import getClientState. On a page where the trigger is the only reader, main plus the export fails:

shared = rx.client_state("", name="shared")
echoed = rx.client_state("", name="echoed")


def index():
    return rx.box(
        rx.input(value=shared.value, on_change=shared.set),
        rx.button("go", on_click=echoed.push(shared.global_value.to(str) + "?")),
        rx.text(echoed.value),
    )

Clicking the button logs ReferenceError: getClientState is not defined. Formatting the Vars without !s keeps their imports (refs and getClientState/setClientState), and the page works. The unit test now asserts those imports; against main's push it fails with assert None is not None.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up in 68e94a2: push now builds the call as FunctionStringVar(refs["__client_state"].set).call(name, value) instead of formatting the Vars into an f-string. Formatting registers each Var in the module-global _global_vars, which is never cleared, and push runs in backend handlers: 1000 pushes of distinct Vars left 1002 entries. The typed call produces the same script with the same imports and adds nothing to the registry.

from reflex_base.components.client_state_context import get_client_state_app_wraps
from reflex_base.constants import Dirs
from reflex_base.constants.state import CAMEL_CASE_CLIENT_STATE_MARKER, CLIENT_STATE_REF
from reflex_base.constants.state import CAMEL_CASE_CLIENT_STATE_MARKER

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the push rewrite, no Python code reads CLIENT_STATE_REF any more. Its comment in reflex_base/constants/state.py still calls it "the one Python-side definition". If you drop the rewrite (see the comment on push), this import comes back and the constant has a reader again. If you keep the rewrite, remove the constant or update its comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import is back along with _client_state_store_ref (ad87682), so the constant has its reader again and the comment stays true.

The state.js export alone lets rx.call_script and a backend push reach
getClientState, so push goes back to writing through refs["__client_state"],
the entry point reflex-dev#6936 documents.

push formatted its Vars with !s, which drops their var data, so the function
it builds carried no imports. A page that compiles push(other.global_value...)
into a trigger then worked only when another component in the same module
happened to import getClientState; with the trigger as the page's only reader
the click raised "ReferenceError: getClientState is not defined". Formatting
the Vars without !s keeps their imports.
Formatting Vars into an f-string registers each one in the module-global
_global_vars, which is never cleared. That is harmless at compile time, but
push runs in backend event handlers, so every push of a new Var kept it alive
for the life of the process (1000 pushes of distinct Vars left 1002 entries).

push now calls the store's set as a FunctionStringVar, which merges the
imports of its arguments without the registry: same script, same imports,
no growth. The unit test compares the whole event and the function's var data
exactly, and uses Event.from_event_type instead of the deprecated fix_events.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants