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

React refactorings #8596

Merged
merged 65 commits into from
Aug 18, 2021
Merged

React refactorings #8596

merged 65 commits into from
Aug 18, 2021

Conversation

brainkim
Copy link
Contributor

@brainkim brainkim commented Aug 5, 2021

This PR eliminates the XData class-based abstractions which were used to implement the React hooks, in favor of hooks which are more “idiomatic” to React. The old class-based abstractions generated an immense amount of mental overhead, insofar as it attempted to abstract over React hooks by defining two methods execute() and afterExecute() which are called by the hook body and in a useEffect() hook respectively.

This PR should make the following issues easier to fix:

@brainkim brainkim changed the base branch from main to brian-testing-2 August 5, 2021 23:32
@brainkim brainkim force-pushed the brian-murder-querydata branch 3 times, most recently from 4b98507 to a7f60ba Compare August 6, 2021 13:59
@brainkim brainkim force-pushed the brian-murder-querydata branch 4 times, most recently from e7fc323 to 9d33727 Compare August 9, 2021 17:00
@brainkim brainkim force-pushed the brian-testing-2 branch 2 times, most recently from 3eaa6c1 to fbf42b6 Compare August 9, 2021 22:45
@brainkim brainkim force-pushed the brian-murder-querydata branch 2 times, most recently from 732bd3c to c2fa63f Compare August 10, 2021 15:05
@brainkim brainkim force-pushed the brian-murder-querydata branch 6 times, most recently from ef9c30a to baeb69c Compare August 12, 2021 16:58
@brainkim brainkim changed the base branch from brian-testing-2 to main August 13, 2021 14:33
@brainkim brainkim marked this pull request as ready for review August 13, 2021 17:25
@brainkim brainkim force-pushed the brian-murder-querydata branch 3 times, most recently from 3362d44 to 790d7f8 Compare August 16, 2021 14:53
Copy link
Member

@benjamn benjamn left a comment

Choose a reason for hiding this comment

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

This is really exciting!

Could you rebase against the release-3.5 branch, so we can put out a beta release with these changes?

Comment on lines +48 to +54
setResult({
loading: false,
data: void 0,
error: void 0,
variables: options?.variables,
});
setObservable(null);
Copy link
Member

Choose a reason for hiding this comment

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

Are these state updates automatically batched because they're in a useEffect callback?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’m not sure, but my guess is no. There are a lot of subtle timing changes which this PR inevitably produces as we refactor state calls.

Copy link
Member

Choose a reason for hiding this comment

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

@brainkim Maybe let's use unstable_batchedUpdates then, just in case? Or are you saying it's important to preserve the timing of the separate updates?

Copy link
Contributor Author

@brainkim brainkim Aug 17, 2021

Choose a reason for hiding this comment

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

@benjamn
It is impossible to preserve the exact timings of the old React hooks implementation, insofar as it used a useReducer/forceUpdate pattern, which, among other things, would in many circumstances, cause state changes to be happen directly in the render function, which is apparently a bad thing to do (we still do it in useQuery just so some specific edge cases don’t produce confusing unit test results). And I’m still not sure what the effect of “batching” the two state updates would have, given that the result and observable are two independently updating pieces of state.

I’m not familiar with unstable_batchedUpdates. Have you adopted it somewhere in this codebase?

  • What is availability of unstable_batchedUpdates across React versions?
  • Is it okay that it seems to be defined on ReactDOM, when none of the hooks require the ReactDOM package previously, especially given that some of our poor users are using React Native?
  • Is it a merely an optimization, or does it actually affect the logic of the hook?
  • Would it hamper preact support?

src/react/hooks/useQuery.ts Show resolved Hide resolved
src/react/hooks/useQuery.ts Show resolved Hide resolved
src/react/hooks/useQuery.ts Show resolved Hide resolved
src/core/ObservableQuery.ts Outdated Show resolved Hide resolved
src/react/hooks/useLazyQuery.ts Outdated Show resolved Hide resolved
src/react/hooks/useQuery.ts Outdated Show resolved Hide resolved
Comment on lines -42 to -44
if (__DEV__) {
// ensure we run an update after refreshing so that we can resubscribe
useAfterFastRefresh(forceUpdate);
Copy link
Member

Choose a reason for hiding this comment

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

Are we losing this functionality, or is it no longer needed after the refactoring?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’m not sure; I’ll have to look into it. We don’t call side-effects from the render function for the most part anymore, except for certain edge cases where I could not get the tests working.

@brainkim
Copy link
Contributor Author

brainkim commented Aug 18, 2021

@benjamn Can I get a review for this commit which would solve #8137

This last commit is failing some tests...

NEVER MIND I’M PUTTING IT IN A SEPARATE PR.

@brainkim brainkim merged commit a4dfee5 into release-3.5 Aug 18, 2021
@brainkim brainkim deleted the brian-murder-querydata branch August 18, 2021 22:29
benjamn added a commit that referenced this pull request Aug 30, 2021
I believe this captures the spirit of #8709, taking the big refactoring
from #8596 into account.
benjamn added a commit that referenced this pull request Sep 13, 2021
We temporarily relaxed the bundlesize limit to include only
`@apollo/client/core` during development of Apollo Client v3.4, in part
because `QueryData` and other React-related infrastructure were
unnecessarily verbose, as you can see from the jump in bundlesize from
24.7kB to 28.4kB because of this commit.

However, @brainkim refactored the React abstractions in #8596 🎉,
eliminating `QueryData` and related classes once and for all, so I now
feel much better about including `@apollo/client/react` in the
bundlesize calculation again.
benjamn added a commit that referenced this pull request Sep 13, 2021
Analogous to 2c0088f, but cherry-picked
from `main` to `release-3.5`.

Though this increases the bundlesize by +2.7kB (reflecting the weight of
`@apollo/client/react`), the same calculation on `main` gives 28.35kB,
whereas `release-3.5` now gives 27.62kB, reflecting the massive savings
from @brainkim's React refactorings in #8596.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
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.

3 participants