Skip to content

Better structural sharing for complex types - #6311

Closed
aldebout wants to merge 24 commits into
trpc:mainfrom
aldebout:alexdb/better-structural-sharing
Closed

aldebout wants to merge 24 commits into
trpc:mainfrom
aldebout:alexdb/better-structural-sharing

Conversation

@aldebout

@aldebout aldebout commented Dec 11, 2024 •

Copy link
Copy Markdown

Closes #6306

🎯 Changes

This PR introduces a new function to handle structural sharing of non-JSON-serializable results in tanstack-query.
With this feature, API endpoints that returns Dates, Sets and Maps should now have more stable data object identity between renders.

βœ… Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

@vercel

vercel Bot commented Dec 11, 2024

Copy link
Copy Markdown

@aldebout is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

@aldebout aldebout changed the title Alexdb/better structural sharing Better structural sharing for complex objects Dec 11, 2024
@aldebout aldebout changed the title Better structural sharing for complex objects Better structural sharing for complex types Dec 11, 2024
@vercel

vercel Bot commented Dec 11, 2024 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
www βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Dec 20, 2024 11:05pm

@aldebout

Copy link
Copy Markdown
Author

Current state:

  • The first commit introduces the function and exports it, the second commit uses it by default making this a breaking change.
  • I would appreciate some help writing tests for the second part, I'm not familiar with testing hooks in components, we need to get the first result, invalidate, get the second result and compare both.
  • I'm not sure where we should reflect this in the docs?

@pkg-pr-new

pkg-pr-new Bot commented Dec 11, 2024 •

Copy link
Copy Markdown

Open in Stackblitz

npm i https://pkg.pr.new/trpc/trpc/@trpc/client@6311
npm i https://pkg.pr.new/trpc/trpc/@trpc/next@6311
npm i https://pkg.pr.new/trpc/trpc/@trpc/server@6311
npm i https://pkg.pr.new/trpc/trpc/@trpc/react-query@6311

commit: 1569e2a

KATT
KATT previously requested changes Dec 12, 2024

@KATT KATT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Needs

  • A test that leverages the structural sharing in a test that renders a react app
  • Removal of dependency
  • Making it opt-in somehow (I'm guessing this is something you pass to the react query proivder?)

Comment thread packages/react-query/package.json Resolved
@aldebout

Copy link
Copy Markdown
Author

Still missing:

  • Documentation

@KATT KATT left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

changed some variable names, fixed a bug, and left some questions/comments

Comment on lines +141 to +149
test('stable for sets - different order is still equal', async () => {
const set = new Set([1, 2, 3]);
const oldResult = parse(stringify({ a: set }));
const newResult = parse(stringify({ a: new Set([3, 2, 1]) }));

expect(defaultStructuralSharingFunction(oldResult, newResult)).toBe(
oldResult,
);
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am wondering if this is a bug or a feature? Is this how RQ handles arrays?

);
});

test('stable for maps - different keys are not equal', async () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure if order should matter or not here?

Comment on lines +168 to +173
test('sets - different sizes are not equal', async () => {
const prev = new Set([1]);
const next = new Set([1, 2]);

expect(defaultStructuralSharingFunction(prev, next)).toEqual(next);
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

added this test and fixed the bug

const equalCheck = (a: unknown, b: unknown) => {
return customEqualityFunction(a, b) || defaultIsEqual(a, b);
};
const structuralSharingFunction = (prev: any, next: any): any => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

renamed these to prev and next fyi

const map = prev instanceof Map && next instanceof Map;
const set = prev instanceof Set && next instanceof Set;

if (array || plainObject || map) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The code within here is.... horribly hard to follow

should we split it up into less DRY operations instead?

@KATT
KATT dismissed their stale review December 20, 2024 15:51

stale

@KATT

KATT commented Dec 20, 2024

Copy link
Copy Markdown
Member

please re-request review or ping me on discord when you have responded etc!

return a.source === b.source && a.flags === b.flags;
if (a.valueOf !== Object.prototype.valueOf)
return a.valueOf() === b.valueOf();
if (a.toString !== Object.prototype.toString)

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 may get evaluated if a and b are arrays. In this case, a.toString === Array.prototype.toString !== Object.prototype.toString.

Maybe moving the if(equalCheck(prev, next)) statement after the array and object detections could be more in line.

-- I've been debugging this for quite some time :)

@PodkopovP

Copy link
Copy Markdown

Anything happen with this one?

@Nick-Lucas Nick-Lucas closed this Nov 14, 2025
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 15, 2025

This branch was successfully deployed

5 active (3 outdated) deployments
Preview – www β€” 1569e2ac Deployed Dec 20, 2024 by vercel[bot]
Preview – trpc-next-app-dir β€” 1569e2ac Deployed Dec 20, 2024 by vercel[bot]
Preview – next-prisma-starter β€” d882ba31 Deployed Dec 20, 2024 by vercel[bot]
Preview – examples-todomvc β€” d882ba31 Deployed Dec 20, 2024 by vercel[bot]
Preview – og-image β€” d882ba31 Deployed Dec 20, 2024 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Better structural sharing in tanstack-query

5 participants