Fix running a newly selected example with stale editor code - #313
Conversation
Clicking an example and Run in the same tick used React state from the previous render, so example 8 could still execute the default screenshot snippet. Keep the latest code in a ref, read it via getCode() on Run, and wait for the examples accordion to open before clicking Run in e2e. Co-authored-by: Max Schmitt <max@schmitt.mx>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 18d8fc8f9a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| }); | ||
| }); | ||
| setResponse(await runCode(code, codeLanguage, turnstileToken)) | ||
| setResponse(await runCode(getCode(), codeLanguage, turnstileToken)) |
There was a problem hiding this comment.
Snapshot the code before awaiting Turnstile
When Turnstile completion is delayed and the user selects another example from the still-active examples panel after clicking Run, getCode() reads the mutated ref only after that selection, so this execution unexpectedly submits the later example rather than the code associated with the Run click. Capture getCode() before awaiting the Turnstile token, then pass that snapshot to runCode.
Useful? React with 👍 / 👎.
The setter-written ref is the product fix; the characterization test lives in a separate repro PR so this change stays the App/e2e path. Co-authored-by: Max Schmitt <max@schmitt.mx>
Co-authored-by: Max Schmitt <max@schmitt.mx>
Why
Selecting an example and clicking Run in the same tick sent the previous editor buffer to
/run. Example 8 (video) could still execute the default screenshot snippet, so e2e saw 0 videos and leftoverexample-chromium.png/example-webkit.png.React
setStateis async:onChangeupdates context, buthandleExecutionstill closed overcodefrom the last render. That is worse afterawaitTurnstile.The setter-written ref is not duplicated state — it is the mailbox React recommends for reading the latest value from an async handler. A
useEffectcopy is still stale in the same click. Characterization only: #316What
updateCodewriteslatestCodethensetCode.getCode()after the Turnstile promise..rs-panel-in, then Run.Does not change Turnstile or
/runstreaming.Test plan
npx playwright test src/repro/staleStateRepro.spec.tsxon Add a minimal repro for stale React state on select-and-run #316