Repository navigation
fix(client-state): let global_value and global_set run in call_script code - #7946
benedikt-bartscher wants to merge 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
|
| # 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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.