Skip to content

fix(svelte-query): useMutationState array never shrank when mutations stopped matching filter - #11254

Open
okxint wants to merge 1 commit into
TanStack:mainfrom
okxint:fix/svelte-query-use-mutation-state-shrink
Open

fix(svelte-query): useMutationState array never shrank when mutations stopped matching filter#11254
okxint wants to merge 1 commit into
TanStack:mainfrom
okxint:fix/svelte-query-use-mutation-state-shrink

Conversation

@okxint

@okxint okxint commented Aug 22, 2026

Copy link
Copy Markdown

Summary

useMutationState() in @tanstack/svelte-query never removed entries from its returned array. Once a mutation stopped matching filters (settled, was garbage-collected, or mutationCache.clear() was called), the stale entry stayed in the array forever.

Root cause

The cache-subscription callback updates the reactive $state array with:

Object.assign(result, nextResult)

Object.assign copies enumerable own properties — the numeric indices of nextResult — but never adjusts Array.length. When nextResult is shorter than result, the trailing stale entries are never touched and result.length is never updated.

This bug was introduced in the Svelte 5 runes rewrite (#9694) and has been present since.

Fix

- Object.assign(result, nextResult)
+ result.splice(0, result.length, ...nextResult)

splice keeps the $state array reference intact (required by Svelte 5 runes — reassigning result = nextResult would lose reactivity) while correctly adjusting length, replacing all elements, and removing stale trailing entries.

Test

Added regression test: two mutations started concurrently both match filters: { status: 'pending' }. After both settle, the result array must shrink to []. Previously this assertion failed — the array stayed ["pending", "pending"] indefinitely.

All 7 existing tests continue to pass.

Fixes #11152

Summary by CodeRabbit

  • Bug Fixes

    • Fixed mutation state tracking so results are removed when mutations no longer match the selected filter.
    • Ensured filtered mutation lists accurately shrink after mutations finish.
  • Tests

    • Added coverage verifying pending mutation entries are cleared once mutations settle.

… stopped matching filter

Object.assign copies enumerable own properties but never sets Array.length.
When nextResult was shorter than result (fewer mutations matched the filter),
trailing stale entries were never removed — the array only ever grew.

Fix: replace Object.assign with result.splice(0, result.length, ...nextResult).
splice keeps the reactive $state array reference intact (required by Svelte 5
runes) while correctly adjusting length, adding, and removing elements.

Adds a regression test: two pending mutations, both settle, assert the
filtered result shrinks to [].

Fixes TanStack#11152

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 22, 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: b76c5445-8b9c-4670-99a4-2953482de0fd

📥 Commits

Reviewing files that changed from the base of the PR and between 2215bb0 and 9002ee6.

📒 Files selected for processing (3)
  • .changeset/fix-svelte-use-mutation-state-shrink.md
  • packages/svelte-query/src/useMutationState.svelte.ts
  • packages/svelte-query/tests/useMutationState/useMutationState.svelte.test.ts

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


📝 Walkthrough

Walkthrough

useMutationState now updates its reactive array with splice, which removes mutations that no longer match the filter. A regression test verifies the array changes from two pending mutations to an empty array after settlement. A Changesets file declares a patch release.

Changes

Mutation state shrink fix

Layer / File(s) Summary
Synchronize filtered mutation results
packages/svelte-query/src/useMutationState.svelte.ts, packages/svelte-query/tests/useMutationState/useMutationState.svelte.test.ts
The result array now uses splice to replace its contents. The test verifies that settled mutations are removed from a pending-only result.
Record the patch release
.changeset/fix-svelte-use-mutation-state-shrink.md
Adds patch release metadata for @tanstack/svelte-query.

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

Merge Risk: ⚪ Minimal · up to 9002e

This localized change corrects stale mutation-state entries and adds regression coverage; no actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.) Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the Svelte Query bug and the array-shrinking fix.
Description check ✅ Passed The description clearly explains the bug, root cause, fix, regression test, and linked issue, with only template headings and checklist items omitted.
Linked Issues check ✅ Passed The source fix and regression test directly satisfy issue #11152 by removing stale filtered mutations while preserving reactive array identity.
Out of Scope Changes check ✅ Passed The changeset, source fix, and regression test all directly support issue #11152 and contain no unrelated changes.
✨ 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 free to 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