Repository navigation
fix(client-state): let global_value and global_set run in call_script code - #7946
FarhanAliRaza merged 4 commits into
Conversation
… 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.
[Medium impact] The PR appears safe to merge; no actionable new issues were found. SummaryMakes named client state available in
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 |
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
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.
reflex-dev#7966 dropped the FunctionVar import while reflex-dev#7946 still used it here, so ruff reports an undefined name on main.
Since #7870, code sent with
rx.call_scriptruns throughrunScript, which can only see the exports ofstate.js.ClientStateVar.global_valueandglobal_setcompile togetClientState(...)/setClientState(...).state.jsimports those two functions but doesn't export them, so both accessors throwReferenceErrorincall_scriptcode. The same goes forpush()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 writingrefs["__client_state"]by hand.state.jsnow exportsgetClientStateandsetClientState, so both accessors work in script code.push(Var)still writes throughrefs["__client_state"], but builds the call as a typedFunctionStringVarcall, which keeps the imports of the Var it is given (formatting it with!sdropped them). A page that compiles the push into a trigger now importsgetClientStateitself, instead of relying on another component in the same module to import it.rx.call_scriptis 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_scriptwithglobal_set/global_valueand apushreading another var, and a unit test pins the generatedpushcode and its imports. Without the export, the Playwright tests fail withReferenceError: setClientState is not defined; without the change topush, the unit test finds no imports on the generated function.