Skip to content
Source
Sign inSign up
TanStack/query

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

Mergedalex-js-ltd(GitHub profile, opens in a new tab)merged 4 commits intomain fromrefs/pull/11153/headmerged by TkDodo View on GitHub

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.
    1. 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 16b6671374615466546e9ebb7e0025d2fb36fccb and 9b20de9fab48b95bc223a7d9ca3c84167d3f5fef.

      📒 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 nameStatusExplanation
      Title check✅ PassedThe title clearly summarizes the main fix: shrinking stale useMutationState results in Svelte Query.
      Description check✅ PassedThe description explains the bug, root cause, fix, regression test, checklist, and release impact.
      Linked Issues check✅ PassedThe changes directly address issue #11152 by removing stale filtered mutations while preserving array identity and adding regression coverage.
      Out of Scope Changes check✅ PassedAll code, test, and changeset updates directly support the linked issue and stated pull request objectives.
      Docstring Coverage✅ PassedDocstring 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.
    2. Yeah. This checks out. Thanks for the commit!

    3. 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.

    4. View your CI Pipeline Execution ↗ for commit 9b20de9fab48b95bc223a7d9ca3c84167d3f5fef

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded4m 26sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded7sView ↗

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

    5. 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

    6. TkDodo merged this250050d

    7. TkDodo closed this

Sign in to join this conversation.