Skip to content

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

Merged
TkDodo merged 4 commits into
TanStack:mainfrom
alex-js-ltd:fix/svelte-query-usemutationstate-array-shrink
Aug 24, 2026
Merged

fix(svelte-query): shrink useMutationState results#11153
TkDodo merged 4 commits into
TanStack:mainfrom
alex-js-ltd:fix/svelte-query-usemutationstate-array-shrink

Conversation

@alex-js-ltd

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

Copy link
Copy Markdown
Contributor

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 keep mutation state results accurate as statuses change, including transitions away from pending.
  • Tests

    • Added coverage confirming that mutations are removed when they no longer match a pending-status filter.
    • Verified filtered mutation results remain synchronized after status updates.

@coderabbitai

coderabbitai Bot 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: 1b8fbfd9-caa3-4939-b1c3-d51f0570b358

📥 Commits

Reviewing files that changed from the base of the PR and between 16b6671 and 9b20de9.

📒 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
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/svelte-query/tests/useMutationState/useMutationState.svelte.test.ts
  • packages/svelte-query/src/useMutationState.svelte.ts
  • .changeset/svelte-query-usemutationstate-filter-shrink.md

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


📝 Walkthrough

Walkthrough

useMutationState now removes mutations that no longer match its filters. A regression test covers completed pending mutations, 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

Merge Risk: ⚪ Minimal · up to 9b20d

This localized fix makes the returned mutation-state array correctly shrink when entries stop matching, while preserving the existing array reference. No actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main fix: shrinking stale useMutationState results in Svelte Query.
Description check ✅ Passed The description explains the bug, root cause, fix, regression test, checklist, and release impact.
Linked Issues check ✅ Passed The changes directly address issue #11152 by removing stale filtered mutations while preserving array identity and adding regression coverage.
Out of Scope Changes check ✅ Passed All code, test, and changeset updates directly support the linked issue and stated pull request objectives.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
✨ 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.

@Michael-Obele

Copy link
Copy Markdown

Yeah. This checks out. Thanks for the commit!

@Michael-Obele Michael-Obele left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. Thanks

@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@nx-cloud

nx-cloud Bot commented Aug 24, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 9b20de9

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 4m 26s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 7s View ↗

☁️ Nx Cloud last updated this comment at 2026-08-24 12:39:16 UTC

@pkg-pr-new

pkg-pr-new Bot commented Aug 24, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@11153

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@11153

@tanstack/lit-query

npm i https://pkg.pr.new/@tanstack/lit-query@11153

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@11153

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@11153

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@11153

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@11153

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@11153

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@11153

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@11153

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@11153

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@11153

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@11153

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@11153

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@11153

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@11153

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@11153

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@11153

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@11153

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@11153

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@11153

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@11153

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@11153

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@11153

commit: 9b20de9

@TkDodo
TkDodo merged commit 250050d into TanStack:main Aug 24, 2026
9 checks passed
@github-actions github-actions Bot mentioned this pull request Aug 24, 2026
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)

3 participants