Skip to content

fix(svelte-query): shrink useMutationState results - #11153

Open
alex-js-ltd wants to merge 3 commits into
TanStack:mainfrom
alex-js-ltd:fix/svelte-query-usemutationstate-array-shrink
Open

fix(svelte-query): shrink useMutationState results#11153
alex-js-ltd wants to merge 3 commits into
TanStack:mainfrom
alex-js-ltd:fix/svelte-query-usemutationstate-array-shrink

Conversation

@alex-js-ltd

@alex-js-ltdalex-js-ltd commented Aug 8, 2026

Copy link
Copy Markdown

Changes

Fixes#11152.

useMutationState() in @tanstack/svelte-query only ever grew its returned
array. The cache-subscription callback updated the underlying $state array
with Object.assign(result, nextResult), but Array.prototype.length is
non-enumerable, so Object.assign never truncates result when the new
result set is shorter. Once a mutation stopped matching filters (settled,
was removed/GC'd, or a narrower filter was applied), its stale entry stayed
in the array forever.

Fix: replace the Object.assign with result.splice(0, result.length, ...nextResult),
which replaces the array's contents in place — including truncating it —
while preserving the $state array's reference identity (required by the rune).

Includes a regression test that starts two mutations matching
filters: { status: 'pending' }, lets both settle, and asserts the returned
array goes back to [] instead of keeping the two stale "pending" entries.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes

    • Fixed useMutationState so mutations are removed from filtered results when they no longer match the selected criteria.
    • Improved reactive updates to ensure mutation state results stay accurate as statuses change.
  • Tests

    • Added coverage for removing mutations that no longer match a pending-status filter.

@coderabbitai

coderabbitaiBot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: eef56c9e-f49e-4db7-82ff-5d61602392ad

📥 Commits

Reviewing files that changed from the base of the PR and between 46d7f02 and 9f71f4f.

📒 Files selected for processing (3)
  • .changeset/svelte-query-usemutationstate-filter-shrink.md
  • packages/svelte-query/src/useMutationState.svelte.ts
  • packages/svelte-query/tests/useMutationState/useMutationState.svelte.test.ts

📝 Walkthrough

Walkthrough

useMutationState now removes mutations that no longer match its filters. A regression test covers pending mutations after completion, and a patch changeset documents the fix.

Changes

Mutation filter result updates

Layer / File(s)Summary
Replace stale filtered results
packages/svelte-query/src/useMutationState.svelte.ts, packages/svelte-query/tests/useMutationState/useMutationState.svelte.test.ts, .changeset/svelte-query-usemutationstate-filter-shrink.md
The reactive result array now uses splice to match the current filtered results. The test verifies that completed pending mutations produce an empty result. A patch changeset records the fix.

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

Possibly related PRs

  • TanStack/query#11123: Both PRs modify useMutationState tests, but this PR fixes stale filtered results.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check nameStatusExplanation
Title check✅ PassedThe title clearly describes the primary fix: shrinking filtered useMutationState results.
Description check✅ PassedThe description explains the bug, fix, regression test, checklist completion, and release impact with a changeset.
Linked Issues check✅ PassedThe implementation, regression test, and changeset satisfy the requirements in issue #11152.
Out of Scope Changes check✅ PassedAll changes directly support fixing filtered useMutationState results and adding regression coverage.
Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useMutationState never removes mutations that no longer match the filter (svelte-query)

1 participant

@alex-js-ltd