Repository navigation
Await the results of browser scripts from background handlers - #7955
FarhanAliRaza wants to merge 6 commits into
Conversation
A background event handler can now `await rx.call_script(...)`,
`rx.call_function(...)` or `rx.run_script(...)` and get the script's
result back, instead of passing a callback that the browser evals.
- Event gains an optional `rid`, sent only when the backend awaits the
event, so ordinary events serialize as before. Clients cannot set it.
- The browser runs the script and answers on a new `result` socket event
with `{rid, json}` or `{rid, error}`. It does not await the script in
the event queue, so a script waiting on the user holds up nothing.
- BrowserCalls keeps the pending calls per token. A result settles a
call only for the token it was sent to. A disconnect starts a grace
period (browser_call_grace_period); a reconnect within it keeps the
calls, since socket.io buffers the result emit.
- Awaiting under the state lock (a foreground handler, `async with
self`) raises EventHandlerTypeError, as does passing a callback too.
- Timeouts default to browser_call_timeout, overridable with timeout=.
- Errors raise BrowserScriptError, BrowserResultSerializationError,
BrowserDisconnectedError or TimeoutError.
Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
add_args binds extra arguments to the event's signature parameters, so the new keyword-only timeout absorbed one. The signature the event reports now leaves it out. runEventScript declares the late-bound addEvents alias itself, as every function that runs user code must. Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
[Medium impact] The PR appears safe to merge, with no new actionable findings. SummaryBackground handlers can await browser scripts and receive their values or errors, with configurable timeouts and reconnect grace periods.
Reviews (4) · Last reviewed commit: "Import the exceptions at the top of the ..." · Reviewed by Greptile |
String() throws on an object without a prototype, so a script rejecting with one sent no result and its call waited for the timeout. Error fields now fall back to Object.prototype.toString. The new tests also import the exceptions at the top of their modules. Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
Merging this PR will regress 1 benchmark2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
The rid field on Event cost every Event one more frozen field write (CodSpeed: -7% on from_event_type). Only _call_script and _call_function ever carry a request id, so it now goes in their payload and no other event pays for it; the Event serializer and the _EVENT_FIELDS change are gone with the field. The script event signature is also computed once instead of on every call, which made console_log 8% slower. Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
|
CodSpeed on 53e4424 reports
The two regressions earlier runs found ( Generated by Claude Code |
All Submissions:
Type of change
Description
Part of ENG-9127. Instead of passing a
callbackthat the browser evals, the browser now sends a script's return value back to the backend, so a background handler canawaitit:Works for
rx.call_script,rx.call_functionandrx.run_script. A script returning a promise is awaited in the browser, and the handler gets the resolved value.Rules
async with self). Awaiting in a foreground handler or under the lock raisesEventHandlerTypeError, because every other event of the tab would wait for the browser. Passingcallback=and awaiting raises too.browser_call_timeoutconfig (30s by default), overridable per call withtimeout=(None= no limit). RaisesTimeoutError.browser_call_grace_period(5s). socket.io buffers the browser's result emit, so a tab reconnecting in time still delivers it. Otherwise the call raisesBrowserDisconnectedError, which is also what you get when calling a tab that is not connected.BrowserScriptError(with the JSname,messageandstack). A value JSON cannot carry raisesBrowserResultSerializationError. That includes DOM nodes andwindow, which would otherwise arrive as{}, and BigInt.Wire
_call_script/_call_functionevent carries a request id in its payload (rid). No other event changes shape or cost: theEventdataclass is untouched.resultsocket event with{rid, json}or{rid, error: {name, message, stack, kind?}}.BrowserCalls(one per event processor) resolves a call only for the token it was sent to, so a client cannot answer another tab's call.timeout) is computed once instead of on every call, so creating a script event is cheaper than before.Out of scope / follow-ups
BrowserDisconnectedError. This is documented.callback=, and the browser awaiting the backend (env.call).New Feature Submission:
Changes To Core Features:
Tests run locally:
tests/units/reflex_cli/v2, which fails identically onmainin this environment.reflex-frontendlint and typecheck.test_call_script.py(2 new end-to-end tests).pyright reflex tests, ruff and pre-commit.Docs: "Awaiting Results" in
docs/api-reference/browser_javascript.md, and both settings indocs/advanced_onboarding/configuration.md.https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf