Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace JSON.stringify with replaceDeepEqual in structural sharing integrity check #8030

Merged
merged 7 commits into from
Sep 9, 2024

Conversation

jxom
Copy link
Contributor

@jxom jxom commented Sep 8, 2024

This PR is a follow up on #7966 to:

  • not surface console errors for values that are referentially stable (e.g. BigInt)
  • detect circular references in replaceDeepEqual
  • catch errors from replaceDeepEqual in replaceData, and surface them in the console + propagated to the query.
  • replace JSON.stringify with replaceDeepEqual in structural sharing integrity check

Copy link

nx-cloud bot commented Sep 8, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit cd2c14c. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

Comment on lines 255 to 258
if (seen.has(a)) {
throw new Error('circular reference detected.')
}
seen.add(a)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these lines were added

if (a === b) {
return a
}
const seen = new WeakSet()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this line was added

Copy link

pkg-pr-new bot commented Sep 8, 2024

Open in Stackblitz

More templates

@tanstack/eslint-plugin-query

pnpm add https://pkg.pr.new/@tanstack/eslint-plugin-query@8030

@tanstack/angular-query-experimental

pnpm add https://pkg.pr.new/@tanstack/angular-query-experimental@8030

@tanstack/query-async-storage-persister

pnpm add https://pkg.pr.new/@tanstack/query-async-storage-persister@8030

@tanstack/query-broadcast-client-experimental

pnpm add https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@8030

@tanstack/query-core

pnpm add https://pkg.pr.new/@tanstack/query-core@8030

@tanstack/query-devtools

pnpm add https://pkg.pr.new/@tanstack/query-devtools@8030

@tanstack/query-persist-client-core

pnpm add https://pkg.pr.new/@tanstack/query-persist-client-core@8030

@tanstack/query-sync-storage-persister

pnpm add https://pkg.pr.new/@tanstack/query-sync-storage-persister@8030

@tanstack/react-query

pnpm add https://pkg.pr.new/@tanstack/react-query@8030

@tanstack/react-query-devtools

pnpm add https://pkg.pr.new/@tanstack/react-query-devtools@8030

@tanstack/react-query-next-experimental

pnpm add https://pkg.pr.new/@tanstack/react-query-next-experimental@8030

@tanstack/react-query-persist-client

pnpm add https://pkg.pr.new/@tanstack/react-query-persist-client@8030

@tanstack/solid-query

pnpm add https://pkg.pr.new/@tanstack/solid-query@8030

@tanstack/solid-query-devtools

pnpm add https://pkg.pr.new/@tanstack/solid-query-devtools@8030

@tanstack/solid-query-persist-client

pnpm add https://pkg.pr.new/@tanstack/solid-query-persist-client@8030

@tanstack/svelte-query

pnpm add https://pkg.pr.new/@tanstack/svelte-query@8030

@tanstack/svelte-query-devtools

pnpm add https://pkg.pr.new/@tanstack/svelte-query-devtools@8030

@tanstack/svelte-query-persist-client

pnpm add https://pkg.pr.new/@tanstack/svelte-query-persist-client@8030

@tanstack/vue-query

pnpm add https://pkg.pr.new/@tanstack/vue-query@8030

@tanstack/vue-query-devtools

pnpm add https://pkg.pr.new/@tanstack/vue-query-devtools@8030

@tanstack/angular-query-devtools-experimental

pnpm add https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@8030

commit: cd2c14c

Copy link

codecov bot commented Sep 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.85%. Comparing base (d58cf08) to head (cd2c14c).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           main    #8030       +/-   ##
=========================================
+ Coverage      0   61.85%   +61.85%     
=========================================
  Files         0      135      +135     
  Lines         0     4679     +4679     
  Branches      0     1306     +1306     
=========================================
+ Hits          0     2894     +2894     
- Misses        0     1543     +1543     
- Partials      0      242      +242     
Components Coverage Δ
@tanstack/angular-query-devtools-experimental ∅ <ø> (∅)
@tanstack/angular-query-experimental 86.58% <ø> (∅)
@tanstack/eslint-plugin-query ∅ <ø> (∅)
@tanstack/query-async-storage-persister 43.85% <ø> (∅)
@tanstack/query-broadcast-client-experimental ∅ <ø> (∅)
@tanstack/query-codemods ∅ <ø> (∅)
@tanstack/query-core 92.86% <100.00%> (∅)
@tanstack/query-devtools 4.86% <ø> (∅)
@tanstack/query-persist-client-core 57.73% <ø> (∅)
@tanstack/query-sync-storage-persister 82.50% <ø> (∅)
@tanstack/react-query 92.50% <ø> (∅)
@tanstack/react-query-devtools 10.00% <ø> (∅)
@tanstack/react-query-next-experimental ∅ <ø> (∅)
@tanstack/react-query-persist-client 100.00% <ø> (∅)
@tanstack/solid-query 78.20% <ø> (∅)
@tanstack/solid-query-devtools ∅ <ø> (∅)
@tanstack/solid-query-persist-client 100.00% <ø> (∅)
@tanstack/svelte-query 87.33% <ø> (∅)
@tanstack/svelte-query-devtools ∅ <ø> (∅)
@tanstack/svelte-query-persist-client 100.00% <ø> (∅)
@tanstack/vue-query 71.51% <ø> (∅)
@tanstack/vue-query-devtools ∅ <ø> (∅)

Copy link
Collaborator

@TkDodo TkDodo left a comment

Choose a reason for hiding this comment

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

I think we had a bit of a miscommunication. Ideally, we don't want any runtime changes for production builds - that's why the initial code was behind an env check.

So my idea was something like this:

if (process.env.NODE_ENV !== 'production') {
  try {
    return replaceEqualDeep(prevData, data)
  }
  catch(error) {
    console.error(...)
  }
}
return replaceEqualDeep(prevData, data)

This will remove the dev mode overhead of json stringify. If everything goes right, replaceEqualDeep will only be called once. If there is a circularity, the first call will log the error and the second call will then throw, so it will be caught in setData and the query will go to error state.

I think this would be the best of both worlds.

// Structurally share data between prev and new data if needed
return replaceEqualDeep(prevData, data)
} catch (error) {
console.error(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Realised this should be under a dev env guard. Will fix when I’m back at computer.

@jxom
Copy link
Contributor Author

jxom commented Sep 8, 2024

I think we had a bit of a miscommunication. Ideally, we don't want any runtime changes for production builds - that's why the initial code was behind an env check.

So my idea was something like this:

if (process.env.NODE_ENV !== 'production') {
  try {
    return replaceEqualDeep(prevData, data)
  }
  catch(error) {
    console.error(...)
  }
}
return replaceEqualDeep(prevData, data)

This will remove the dev mode overhead of json stringify. If everything goes right, replaceEqualDeep will only be called once. If there is a circularity, the first call will log the error and the second call will then throw, so it will be caught in setData and the query will go to error state.

I think this would be the best of both worlds.

Got it. Can do that!

@jxom jxom changed the title feat: throw when replaceDeepEqual contains circular references feat: replace JSON.stringify with replaceEqualDeep in structural sharing integrity check Sep 8, 2024
@jxom
Copy link
Contributor Author

jxom commented Sep 8, 2024

@TkDodo - lmk what you think

@jxom jxom changed the title feat: replace JSON.stringify with replaceEqualDeep in structural sharing integrity check feat: replace JSON.stringify with replaceDeepEqual in structural sharing integrity check Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants