Skip to content
Source
Sign inSign up
TanStack/db

fix: handle Temporal objects correctly in proxy deepClone and deepEqual #434

MergedKyleAMathewsmerged 6 commits intomain fromrefs/pull/434/headmerged by KyleAMathews

KyleAMathewscommented

Summary

  • Fixes an issue where Temporal objects (like Temporal.ZonedDateTime) were being converted to empty objects during proxy tracking operations
  • Adds proper support for all Temporal API objects in the proxy's deepClone and deepEqual functions
  • Prevents unnecessary proxy creation for immutable Temporal objects

Problem

When using Temporal objects with TanStack DB's proxy system, the deepClone function was attempting to clone Temporal objects as regular objects by copying their enumerable properties. Since Temporal objects store their data internally (not as enumerable properties), this resulted in empty objects being produced.

Solution

This PR adds special handling for Temporal objects:

  1. In deepClone: Detects Temporal objects by their Symbol.toStringTag and returns them directly (since they're immutable)
  2. In deepEqual: Adds proper equality checking using Temporal's built-in equals() method where available
  3. In proxy get handler: Prevents creating proxies for Temporal objects since they're immutable

Test plan

  • Added comprehensive test coverage for Temporal.ZonedDateTime, Temporal.PlainDate, and Temporal.Duration
  • All existing proxy tests pass
  • Package builds successfully

🤖 Generated with Claude Code

    1. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 Changeset detected

      Latest commit: 1aed4aa1773c0f785413d590986e0fdd5a455d78

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 8 packages
      NameType
      @tanstack/dbPatch
      @tanstack/electric-db-collectionPatch
      @tanstack/query-db-collectionPatch
      @tanstack/react-dbPatch
      @tanstack/solid-dbPatch
      @tanstack/svelte-dbPatch
      @tanstack/trailbase-db-collectionPatch
      @tanstack/vue-dbPatch

      Not sure what this means? Click here to learn what changesets are.

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

    2. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented
      More templates

      @tanstack/db

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

      @tanstack/db-ivm

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

      @tanstack/electric-db-collection

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

      @tanstack/query-db-collection

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

      @tanstack/react-db

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

      @tanstack/solid-db

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

      @tanstack/svelte-db

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

      @tanstack/trailbase-db-collection

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

      @tanstack/vue-db

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

      commit: 1aed4aa

    3. github-actions[bot](GitHub profile, opens in a new tab)commented

      Size Change: +74 B (+0.11%)

      Total Size: 64.6 kB

      FilenameSizeChange
      ./packages/db/dist/esm/collection.js10.4 kB-128 B (-1.21%)
      ./packages/db/dist/esm/proxy.js3.87 kB-322 B (-7.69%)✅
      ./packages/db/dist/esm/utils.js943 B+524 B (+125.06%)🆘
      ℹ️ View Unchanged
      FilenameSize
      ./packages/db/dist/esm/change-events.js1.13 kB
      ./packages/db/dist/esm/deferred.js230 B
      ./packages/db/dist/esm/errors.js3 kB
      ./packages/db/dist/esm/index.js1.52 kB
      ./packages/db/dist/esm/indexes/auto-index.js745 B
      ./packages/db/dist/esm/indexes/base-index.js605 B
      ./packages/db/dist/esm/indexes/btree-index.js1.74 kB
      ./packages/db/dist/esm/indexes/lazy-index.js1.25 kB
      ./packages/db/dist/esm/local-only.js827 B
      ./packages/db/dist/esm/local-storage.js2.03 kB
      ./packages/db/dist/esm/optimistic-action.js294 B
      ./packages/db/dist/esm/query/builder/functions.js575 B
      ./packages/db/dist/esm/query/builder/index.js3.79 kB
      ./packages/db/dist/esm/query/builder/ref-proxy.js890 B
      ./packages/db/dist/esm/query/compiler/evaluators.js1.48 kB
      ./packages/db/dist/esm/query/compiler/expressions.js631 B
      ./packages/db/dist/esm/query/compiler/group-by.js2.06 kB
      ./packages/db/dist/esm/query/compiler/index.js2.15 kB
      ./packages/db/dist/esm/query/compiler/joins.js2.36 kB
      ./packages/db/dist/esm/query/compiler/order-by.js1.17 kB
      ./packages/db/dist/esm/query/compiler/select.js655 B
      ./packages/db/dist/esm/query/ir.js466 B
      ./packages/db/dist/esm/query/live-query-collection.js333 B
      ./packages/db/dist/esm/query/live/collection-config-builder.js2.56 kB
      ./packages/db/dist/esm/query/live/collection-subscriber.js2.4 kB
      ./packages/db/dist/esm/query/optimizer.js2.63 kB
      ./packages/db/dist/esm/SortedMap.js1.24 kB
      ./packages/db/dist/esm/transactions.js2.29 kB
      ./packages/db/dist/esm/utils/btree.js6.02 kB
      ./packages/db/dist/esm/utils/comparison.js718 B
      ./packages/db/dist/esm/utils/index-optimization.js1.62 kB
      compressed-size-action::db-package-size
    4. github-actions[bot](GitHub profile, opens in a new tab)commented

      Size Change: 0 B

      Total Size: 1.16 kB

      ℹ️ View Unchanged
      FilenameSize
      ./packages/react-db/dist/esm/index.js152 B
      ./packages/react-db/dist/esm/useLiveQuery.js1.01 kB
      compressed-size-action::react-db-package-size
    5. This works beautifully. Thanks v much for jumping on this so quickly 🏎️

    6. Actually, I spoke too soon 😓 Unless I'm doing something wrong, I'm seeing some different odd behaviour now, and I put together this little reproduction.

      If you click the button after the page loads to mutate the data in the store, you will see that the data that's returned from the live query gets out of sync with the data retrieved directly from the store, even though a refetch is happening.

    7. Ok interesting — thanks for the further reproduction! Me or someone else will look into this next week.

    8. Thanks, I appreciate it. Looks like the code in collection.ts to check deep equality is the culprit – it will need some logic similar to what you added in proxy.ts. I just copy-pasted it across and the problem goes away.

      https://github.com/TanStack/db/blob/25c43cdae02c586fcad2a1b661ab620643093bcf/packages/db/src/collection.ts#L1712-L1733

    9. Ah nice!

    10. I took a stab at fixing this, and here's a commit you can cherry-pick onto this branch (if you agree with it!) that makes the issues I was seeing go away: https://github.com/obeattie/tanstack-db/commit/c7cf39cced6316c802f70619be3dd9391db31aeb

      I noticed there are a bunch of different functions for checking deep equality, so I combined them in that commit.

    11. I had a look at this PR and left some comments that need fixing. Nothing major, just refactorings to avoid code duplication. Still need to look into @obeattie's commit.

      @@ -133,3 +133,17 @@
      133133 return clone as unknown as T
      134134 }
      135135
      136+ // Handle Temporal objects
      137+ // Check if it's a Temporal object by checking for the Temporal brand
      138+ if (typeof (obj as any)[Symbol.toStringTag] === `string`) {
      139+ const tag = (obj as any)[Symbol.toStringTag]
      140+ const temporalTypes = [
      141+ `Temporal.PlainDate`,
      142+ `Temporal.PlainTime`,
      143+ `Temporal.PlainDateTime`,
      144+ `Temporal.ZonedDateTime`,
      145+ `Temporal.Instant`,
      146+ `Temporal.PlainYearMonth`,
      147+ `Temporal.PlainMonthDay`,
      148+ `Temporal.Duration`,
      149+ `Temporal.TimeZone`,
      kevin-dp(GitHub profile, opens in a new tab)commented

      These 2 temporals (TimeZone and Calendar) don't seem to exist according to the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal

      @@ -133,3 +133,6 @@
      133133 return clone as unknown as T
      134134 }
      135135
      136+ // Handle Temporal objects
      137+ // Check if it's a Temporal object by checking for the Temporal brand
      138+ if (typeof (obj as any)[Symbol.toStringTag] === `string`) {
      kevin-dp(GitHub profile, opens in a new tab)commented

      We should extract the check to a helper function isTemporalObject that does this logic of checking the toStringTag. We could also use a plain instanceof check unless these temporals may come from a different realm (not sure?).

      @@ -244,3 +268,9 @@
      244268 return true
      245269 }
      246270
      271+ // Handle Temporal objects
      272+ // Check if both are Temporal objects of the same type
      273+ const aTag = (a as any)[Symbol.toStringTag]
      274+ const bTag = (b as any)[Symbol.toStringTag]
      275+
      276+ if (typeof aTag === `string` && typeof bTag === `string`) {
      kevin-dp(GitHub profile, opens in a new tab)commented

      Let's not duplicate this temporal checking logic. We should use a helper function.

      @@ -246,0 +275,21 @@
      275+
      276+ if (typeof aTag === `string` && typeof bTag === `string`) {
      277+ const temporalTypes = [
      278+ `Temporal.PlainDate`,
      279+ `Temporal.PlainTime`,
      280+ `Temporal.PlainDateTime`,
      281+ `Temporal.ZonedDateTime`,
      282+ `Temporal.Instant`,
      283+ `Temporal.PlainYearMonth`,
      284+ `Temporal.PlainMonthDay`,
      285+ `Temporal.Duration`,
      286+ `Temporal.TimeZone`,
      287+ `Temporal.Calendar`,
      288+ ]
      289+
      290+ const aIsTemporal = temporalTypes.includes(aTag)
      291+ const bIsTemporal = temporalTypes.includes(bTag)
      292+
      293+ if (aIsTemporal && bIsTemporal) {
      294+ // If they're different Temporal types, they're not equal
      295+ if (aTag !== bTag) return false
      kevin-dp(GitHub profile, opens in a new tab)commented

      Let's also make a helper function to get the tag of a temporal

      @@ -246,0 +283,21 @@
      283+ `Temporal.PlainYearMonth`,
      284+ `Temporal.PlainMonthDay`,
      285+ `Temporal.Duration`,
      286+ `Temporal.TimeZone`,
      287+ `Temporal.Calendar`,
      288+ ]
      289+
      290+ const aIsTemporal = temporalTypes.includes(aTag)
      291+ const bIsTemporal = temporalTypes.includes(bTag)
      292+
      293+ if (aIsTemporal && bIsTemporal) {
      294+ // If they're different Temporal types, they're not equal
      295+ if (aTag !== bTag) return false
      296+
      297+ // Use Temporal's built-in equals method if available
      298+ if (typeof (a as any).equals === `function`) {
      299+ return (a as any).equals(b)
      300+ }
      301+
      302+ // For Duration, use toString comparison as it doesn't have equals
      303+ if (aTag === `Temporal.Duration`) {
      kevin-dp(GitHub profile, opens in a new tab)commented

      Since the default is to fallback to toString comparison (L308) there's no need for this if test.

      kevin-dp(GitHub profile, opens in a new tab)commented

      Actually, i wouldn't fallback to string comparison here but rather use the static compare method: Temporal.Duration.compare(a, b) === 0

      kevin-dp(GitHub profile, opens in a new tab)replied on packages/db/src/proxy.ts line 303

      Actually, i wouldn't fallback to string comparison here but rather use the static compare method: Temporal.Duration.compare(a, b) === 0

      @@ -741,4 +806,5 @@
      741806 return value.bind(ptarget)
      742807 }
      743808
      744- // If the value is an object, create a proxy for it
      809+ // Check if it's a Temporal object - don't proxy them as they're immutable
      810+ const isTemporalObject =
      kevin-dp(GitHub profile, opens in a new tab)commented

      Use a helper function instead of duplicating this code.

    12. kevin-dp(GitHub profile, opens in a new tab)commented

      @obeattie Your commit is spot on! As far as i can tell, you replaced the different versions of deepEquals spread across the code base by a single deepEquals version that handles Temporals properly and that fixes your issue, right?

      Thanks for jumping in and fixing this 👍

    13. @obeattie I cherry-picked your commit and i further cleaned this PR. Your stackblitz example now works correctly. Before i merge this I'd like to add a unit test though that reproduces the original bug to avoid future regressions.

    14. Thanks for picking this up @kevin-dp!

      As far as i can tell, you replaced the different versions of deepEquals spread across the code base by a single deepEquals version that handles Temporals properly and that fixes your issue, right?

      Yes that's exactly right. In the longer term, and as I think is touched upon in #428's discussion, I think it might be good to allow users to provide their own comparators for different types. If that functionality existed I would probably have just used that, though it's also easy to make a case that Dates and Temporal objects ought to work out of the box.

      Appreciate you both for fixing this so quickly!

    15. I added a unit test that reproduces the bug when ran before @obeattie's commit and passes after that commit. All good on my side to merge it. I'll wait untill this afternoon to give @KyleAMathews a chance to have a look at it.

    16. kevin-dp(GitHub profile, opens in a new tab) approved these changes

    17. Looks great! I found another bug around live queries not updating when I modified a (regular) date which this I tested this PR on and it also fixed so glad you cleaned up our sprawling deep equals mess @obeattie :-D Let's get this out there

    18. KyleAMathews merged thisd64b4a8

    19. KyleAMathews closed this

Sign in to join this conversation.