Skip to content

fix(ui): send a follow-up form state refresh when a change races a pending answer - #19844

Open
theguysudo wants to merge 1 commit into
payloadcms:mainfrom
theguysudo:fix/form-state-debounce-race
Open

theguysudo wants to merge 1 commit into
payloadcms:mainfrom
theguysudo:fix/form-state-debounce-race

Conversation

@theguysudo

Copy link
Copy Markdown

What?

A field change made while a form-state refresh answer is pending is now still sent in a follow-up refresh. Previously such a change could silently never reach the server, leaving admin.condition fields hidden and array rows added during the race stuck in their loading state until the next edit.

Why?

Form queues a refresh 250 ms after the last change, but only when formState differs from prevFormStateRef. When a refresh answer arrives, MERGE_SERVER_STATE sets prevStateRef.current to the merged state — and that state already includes local changes made while the request was pending. If the answer arrives within 250 ms after such a change, the debounced effect later compares the merged state with itself, finds no difference, and never queues the refresh for that change.

How?

In refreshFormState (packages/ui/src/forms/Form/useFormState.ts), keep a reference to contextRef.current.fields as it was when the request was built (fieldsAtRequest). On a refresh answer, only pass prevStateRef to MERGE_SERVER_STATE if contextRef.current.fields is still that reference. Otherwise leave prevFormStateRef unchanged, so the debounced effect sees the difference and queues one more refresh carrying the unsent change. Save (afterSave), validate, reset and autosave answers are untouched — the condition only applies to refresh answers.

Verification

Added packages/ui/src/forms/Form/Form.spec.tsx (browser-mode component test, mocked getFormState server function):

  1. Holds the first refresh answer pending, makes a second change (unit: 'km') while it is pending, then resolves the answer within the 250 ms debounce window. Before this fix the test fails ("expected to be called 2 times, but got 1 times" — the change is never sent); after the fix a second refresh is sent carrying unit: 'km', and no further refresh is queued once it settles.
  2. Guards the no-race case: with no local change during the request, applying the answer settles the form without queueing another refresh.
pnpm vitest run --project components packages/ui/src/forms/Form/Form.spec.tsx
# before fix: 1 failed | 1 passed (2)
# after fix:  2 passed (2)  (verified stable across 3 consecutive runs)

pnpm vitest run --project components
# 18 test files, 149 tests passed

pnpm exec tsc --noEmit -p packages/ui/tsconfig.json
# same error set as before the change (all pre-existing, only line numbers shifted); no new type errors

Fixes #19842

Transparency note: this fix was developed with AI assistance (an AI coding agent driven by the repo owner). The race was reproduced locally and tests were run before submission.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The form-state refresh captures the fields present when the request starts. If fields change before the response arrives, the merge leaves the previous baseline unchanged. Tests cover changed and unchanged fields during a pending request.

Changes: Form refresh handling

Layer / File(s) Summary
Refresh baseline and race tests
packages/ui/src/forms/Form/useFormState.ts, packages/ui/src/forms/Form/Form.spec.tsx
Refresh requests capture the fields at request time. The merge updates the previous baseline only when the fields have not changed. Tests verify that changes during a pending refresh trigger a follow-up request, while no change does not.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • payloadcms/payload#19384: Extracts form-state updates into useFormState, whose hook structure this fix updates.
  • payloadcms/payload#19343: Moves the form-state request lifecycle into useFormState, including the refresh lifecycle changed here.
  • payloadcms/payload#19203: Consolidates refreshes and other form-state requests into a queue that handles pending refresh requests.

Suggested reviewers: alessiogr

Merge Risk: Minimal · up to d9bfb

The change makes a form edit made while a refresh is pending get sent in a follow-up refresh. No concrete merge-blocking risk was found, and tests cover both the racing and no-race cases.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix and the trigger: a follow-up form-state refresh is sent when a change races a pending answer. It matches the main change in useFormState.ts and the new Form.spec.tsx tests. It …
Description check Passed The description states the bug, the cause, the fix in refreshFormState, and the scope limit to refresh answers. It also lists the added tests and the commands run. It matches the raw summary and the o…
Linked Issues check Passed Directly linked issue #19842 is closed. It is historical context only, so its requirement is background. The issue expects a field change made while a refresh is pending to be sent in a following refr…
Out of Scope Changes check Passed The change set has two files. packages/ui/src/forms/Form/useFormState.ts (about +9/-1 lines) implements the race fix for refresh answers only. packages/ui/src/forms/Form/Form.spec.tsx (about +19…
Full details: Title check

Explanation

The title names the fix and the trigger: a follow-up form-state refresh is sent when a change races a pending answer. It matches the main change in useFormState.ts and the new Form.spec.tsx tests. It is a single sentence of 14 words and follows the conventional-commit style.

Full details: Description check

Explanation

The description states the bug, the cause, the fix in refreshFormState, and the scope limit to refresh answers. It also lists the added tests and the commands run. It matches the raw summary and the objectives.

Full details: Linked Issues check

Explanation

Directly linked issue #19842 is closed. It is historical context only, so its requirement is background. The issue expects a field change made while a refresh is pending to be sent in a following refresh. The PR changes refreshFormState in packages/ui/src/forms/Form/useFormState.ts. It captures contextRef.current.fields when the request is built (fieldsAtRequest). On a refresh answer, it passes prevStateRef to MERGE_SERVER_STATE only when contextRef.current.fields is still that reference. Otherwise it leaves prevFormStateRef unchanged, so the debounced effect detects the unsent change and queues another refresh. This matches the issue's proposed fix. Save, validate, reset, and autosave paths are unchanged. Form.spec.tsx adds a race test (second refresh carries unit: 'km') and a no-race guard test. Limits: the repository inspection tool was unavailable for this assessment. The PASS rests on the PR description, the raw change summary, and the author's reported test results. The code and the test results were not read or re-run directly. The summaries agree with each other.

Full details: Out of Scope Changes check

Explanation

The change set has two files. packages/ui/src/forms/Form/useFormState.ts (about +9/-1 lines) implements the race fix for refresh answers only. packages/ui/src/forms/Form/Form.spec.tsx (about +198 lines) adds tests for the racing case and the no-race case. Both support the objective in #19842. The raw summary and PR description report no unrelated changes, no public declaration changes, and no changes to save, validate, reset, or autosave paths. Limit: the repository diff could not be read, so this result also relies on the summaries.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Library detection encountered an error. Review will continue without library-specific context.

The configuration directory could not be loaded. This review used the available YAML and UI settings; directory instructions and checks were not evaluated.

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 1eab969 and d9bfbe6.

Files selected for processing: 1

  • packages/ui/src/forms/Form/useFormState.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

…ms debounce

A change made while a form state refresh request was pending never reached
the server: the merged answer set prevFormStateRef to a state that already
included the unsent edits, so the debounced effect compared the merged state
with itself and skipped the follow-up refresh. Conditions dependent on the
change stayed hidden and array rows added meanwhile kept their loading state
until the next edit.

Only apply the answer to prevFormStateRef when the fields the request was
built from are unchanged; otherwise leave the baseline alone so the debounced
effect sees the difference and queues one more refresh. Save, validate, reset
and autosave answers are unaffected.
@theguysudo
theguysudo force-pushed the fix/form-state-debounce-race branch from 1eab969 to d9bfbe6 Compare November 23, 2026 19:50
@theguysudo

Copy link
Copy Markdown
Author

Rebased onto the latest main — main had moved 4 commits and refactored useFormState.ts (including the dispatchFields → dispatchFieldsWithoutIncrementingFormSequence change), which conflicted with this branch.

I re-applied the fix on top of the refactor and re-verified it:

  • packages/ui/src/forms/Form/useFormState.ts — same two-part change as before: capture fieldsAtRequest when the request is built, and only update the baseline (prevFormStateRef) when the fields have not changed since that request.
  • packages/ui/src/forms/Form/Form.spec.tsx — the same two tests (the racing change still queues a follow-up refresh; no change still does not).

pnpm vitest run --project components packages/ui/src/forms/Form/Form.spec.tsx → 2 passed on the rebased branch.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant