Skip to content

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

Merged
FarhanAliRaza merged 4 commits into
reflex-dev:mainfrom
benedikt-bartscher:fix/client-state-in-call-script
Nov 23, 2026
Merged

FarhanAliRaza merged 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. ↩

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.
@FarhanAliRaza FarhanAliRaza added the bug Something isn't working label Nov 23, 2026 — with ChatGPT Codex Connector
@FarhanAliRaza
FarhanAliRaza merged commit bd108d8 into reflex-dev:main Nov 23, 2026
174 checks passed
FarhanAliRaza added a commit to FarhanAliRaza/reflex that referenced this pull request Nov 24, 2026
reflex-dev#7966 dropped the FunctionVar import while reflex-dev#7946 still used it here, so
ruff reports an undefined name on main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants