Skip to content
Source
Sign inSign up
TanStack/db

[fix] synced data not appearing in derived collection during pending optimistic mutation (issue #1017) #1167

Closedclaude[bot](GitHub profile, opens in a new tab)wants to merge 1 commit intoai/issue-1017-repro fromai/issue-1017-fixopened

Summary

Fixes the issue where synced data from a source collection does not appear in a derived (live query) collection while there is a pending optimistic mutation.

Root Cause

When a sync transaction commits while there's a persisting user transaction (optimistic mutation), commitPendingTransactions() was designed to delay processing the sync transaction. However, this also prevented change events from being emitted to subscribers, making the synced data invisible to live query collections.

The issue manifests in applications that use optimistic updates with live queries - synced data arriving during a pending mutation becomes invisible until the mutation completes.

The Fix

Add an else branch to commitPendingTransactions() that emits change events for committed sync operations without modifying syncedData. This approach:

  1. Preserves existing behavior: The source collection's state (syncedData) remains unchanged during the persisting transaction, which is the expected behavior per existing tests
  2. Enables live query visibility: Subscribers receive events about the new synced data, making it visible immediately in derived collections
  3. Avoids conflicts: Skips emitting events for keys that have pending optimistic mutations (optimisticUpserts/optimisticDeletes)

When the persisting transaction completes, the normal reconciliation flow processes the queued sync transactions and updates syncedData.

How to Test

pnpm --filter "@tanstack/db" test -- tests/query/live-query-collection.test.ts -t "synced data visibility"

The test creates:

  1. A source collection with controllable sync
  2. A derived collection via createLiveQueryCollection
  3. An optimistic action that keeps a mutation pending
  4. Syncs new data while the mutation is pending
  5. Asserts both items are visible in the derived collection

🤖 Generated with Claude Code

    1. ⚠️ No Changeset found

      Latest commit: 815bf4fb79f411de75dfd31b3011464d36afa534

      Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

      This PR includes no changesets

      When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

      Click here to learn what changesets are, and how to add one.

      Click here if you're a maintainer who wants to add a changeset to this PR

    2. More templates

      @tanstack/angular-db

      npm i https://pkg.pr.new/@tanstack/angular-db@1167

      @tanstack/db

      npm i https://pkg.pr.new/@tanstack/db@1167

      @tanstack/db-ivm

      npm i https://pkg.pr.new/@tanstack/db-ivm@1167

      @tanstack/electric-db-collection

      npm i https://pkg.pr.new/@tanstack/electric-db-collection@1167

      @tanstack/offline-transactions

      npm i https://pkg.pr.new/@tanstack/offline-transactions@1167

      @tanstack/powersync-db-collection

      npm i https://pkg.pr.new/@tanstack/powersync-db-collection@1167

      @tanstack/query-db-collection

      npm i https://pkg.pr.new/@tanstack/query-db-collection@1167

      @tanstack/react-db

      npm i https://pkg.pr.new/@tanstack/react-db@1167

      @tanstack/rxdb-db-collection

      npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@1167

      @tanstack/solid-db

      npm i https://pkg.pr.new/@tanstack/solid-db@1167

      @tanstack/svelte-db

      npm i https://pkg.pr.new/@tanstack/svelte-db@1167

      @tanstack/trailbase-db-collection

      npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@1167

      @tanstack/vue-db

      npm i https://pkg.pr.new/@tanstack/vue-db@1167

      commit: 815bf4f

    3. Size Change: +159 B (+0.18%)

      Total Size: 90.9 kB

      FilenameSizeChange
      ./packages/db/dist/esm/collection/state.js3.65 kB+159 B (+4.56%)🔍
      ℹ️ View Unchanged
      FilenameSize
      ./packages/db/dist/esm/collection/change-events.js1.39 kB
      ./packages/db/dist/esm/collection/changes.js1.19 kB
      ./packages/db/dist/esm/collection/events.js388 B
      ./packages/db/dist/esm/collection/index.js3.32 kB
      ./packages/db/dist/esm/collection/indexes.js1.1 kB
      ./packages/db/dist/esm/collection/lifecycle.js1.67 kB
      ./packages/db/dist/esm/collection/mutations.js2.34 kB
      ./packages/db/dist/esm/collection/subscription.js3.62 kB
      ./packages/db/dist/esm/collection/sync.js2.41 kB
      ./packages/db/dist/esm/deferred.js207 B
      ./packages/db/dist/esm/errors.js4.7 kB
      ./packages/db/dist/esm/event-emitter.js748 B
      ./packages/db/dist/esm/index.js2.69 kB
      ./packages/db/dist/esm/indexes/auto-index.js742 B
      ./packages/db/dist/esm/indexes/base-index.js766 B
      ./packages/db/dist/esm/indexes/btree-index.js1.93 kB
      ./packages/db/dist/esm/indexes/lazy-index.js1.1 kB
      ./packages/db/dist/esm/indexes/reverse-index.js513 B
      ./packages/db/dist/esm/local-only.js837 B
      ./packages/db/dist/esm/local-storage.js2.1 kB
      ./packages/db/dist/esm/optimistic-action.js359 B
      ./packages/db/dist/esm/paced-mutations.js496 B
      ./packages/db/dist/esm/proxy.js3.75 kB
      ./packages/db/dist/esm/query/builder/functions.js733 B
      ./packages/db/dist/esm/query/builder/index.js4.08 kB
      ./packages/db/dist/esm/query/builder/ref-proxy.js1.05 kB
      ./packages/db/dist/esm/query/compiler/evaluators.js1.42 kB
      ./packages/db/dist/esm/query/compiler/expressions.js430 B
      ./packages/db/dist/esm/query/compiler/group-by.js1.81 kB
      ./packages/db/dist/esm/query/compiler/index.js1.96 kB
      ./packages/db/dist/esm/query/compiler/joins.js2 kB
      ./packages/db/dist/esm/query/compiler/order-by.js1.45 kB
      ./packages/db/dist/esm/query/compiler/select.js1.06 kB
      ./packages/db/dist/esm/query/expression-helpers.js1.43 kB
      ./packages/db/dist/esm/query/ir.js673 B
      ./packages/db/dist/esm/query/live-query-collection.js360 B
      ./packages/db/dist/esm/query/live/collection-config-builder.js5.42 kB
      ./packages/db/dist/esm/query/live/collection-registry.js264 B
      ./packages/db/dist/esm/query/live/collection-subscriber.js1.93 kB
      ./packages/db/dist/esm/query/live/internal.js145 B
      ./packages/db/dist/esm/query/optimizer.js2.56 kB
      ./packages/db/dist/esm/query/predicate-utils.js2.97 kB
      ./packages/db/dist/esm/query/subset-dedupe.js921 B
      ./packages/db/dist/esm/scheduler.js1.3 kB
      ./packages/db/dist/esm/SortedMap.js1.3 kB
      ./packages/db/dist/esm/strategies/debounceStrategy.js247 B
      ./packages/db/dist/esm/strategies/queueStrategy.js428 B
      ./packages/db/dist/esm/strategies/throttleStrategy.js246 B
      ./packages/db/dist/esm/transactions.js2.9 kB
      ./packages/db/dist/esm/utils.js924 B
      ./packages/db/dist/esm/utils/browser-polyfills.js304 B
      ./packages/db/dist/esm/utils/btree.js5.61 kB
      ./packages/db/dist/esm/utils/comparison.js852 B
      ./packages/db/dist/esm/utils/cursor.js457 B
      ./packages/db/dist/esm/utils/index-optimization.js1.51 kB
      ./packages/db/dist/esm/utils/type-guards.js157 B
      compressed-size-action::db-package-size
    4. Size Change: 0 B

      Total Size: 3.7 kB

      ℹ️ View Unchanged
      FilenameSize
      ./packages/react-db/dist/esm/index.js225 B
      ./packages/react-db/dist/esm/useLiveInfiniteQuery.js1.17 kB
      ./packages/react-db/dist/esm/useLiveQuery.js1.34 kB
      ./packages/react-db/dist/esm/useLiveSuspenseQuery.js559 B
      ./packages/react-db/dist/esm/usePacedMutations.js401 B
      compressed-size-action::react-db-package-size
    5. I'm closing this PR because we want to solve the issue differently. This solution tries to maintain the current semantics but we discussed that we may change the semantics such that we don't delay incoming synced operations:

      collections should always apply synced changes — they should always reflect the state of the backend — but live queries can have the ability to opt out of updates — this would be a new option for a query and if you opt out you'd get returned an object that updates when the query updates and a function to apply those to the query.

      cc @KyleAMathews @samwillis

Sign in to join this conversation.