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

Detect subscriptions wrapped in startTransition #22271

Merged
merged 23 commits into from
Sep 8, 2021
Merged

Detect subscriptions wrapped in startTransition #22271

merged 23 commits into from
Sep 8, 2021

Conversation

salazarm
Copy link
Contributor

@salazarm salazarm commented Sep 8, 2021

Summary

n React 18+, we will recommend against userspace subscription implementations, and instead direct users to one of our built-in APIs like useState, useReducer, useContext, and useSyncExternalStore.

Subscriptions are only a problem when they are used to schedule concurrent updates, so we only need to warn if a subscription is wrapped in startTransition.

We can use a heuristic: if a single startTransition call updates more than n unique fibers, there's likely a subscription.

In dev, you can put each fiber into a set, then at the end of the startTransition call, count how many are in the set and fire a warning if it exceeds the limit.

Let's use console.warn instead of console.error since this may be noisy. Let's also only warn the first time we detect a subscription.

How did you test this change?

Added a test case inside a new file ReactStartTransition

@sizebot
Copy link

sizebot commented Sep 8, 2021

Comparing: 95d762e...10734d2

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 128.25 kB 128.25 kB = 40.92 kB 40.92 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 131.08 kB 131.08 kB = 41.85 kB 41.85 kB
facebook-www/ReactDOM-prod.classic.js = 407.05 kB 407.05 kB = 75.40 kB 75.40 kB
facebook-www/ReactDOM-prod.modern.js = 395.61 kB 395.61 kB = 73.68 kB 73.68 kB
facebook-www/ReactDOMForked-prod.classic.js = 407.05 kB 407.05 kB = 75.40 kB 75.40 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
facebook-www/ReactIs-dev.modern.js +1.10% 9.88 kB 9.99 kB +1.03% 2.63 kB 2.66 kB
facebook-www/ReactIs-dev.classic.js +1.10% 9.88 kB 9.99 kB +1.06% 2.63 kB 2.66 kB
oss-experimental/react/cjs/react-unstable-shared-subset.development.js +0.94% 70.39 kB 71.05 kB +0.99% 19.13 kB 19.32 kB
oss-stable-semver/react/cjs/react.development.js +0.79% 83.76 kB 84.42 kB +0.87% 22.42 kB 22.62 kB
oss-stable/react/cjs/react.development.js +0.79% 83.76 kB 84.42 kB +0.87% 22.42 kB 22.62 kB
oss-experimental/react/cjs/react.development.js +0.78% 84.46 kB 85.12 kB +0.87% 22.53 kB 22.73 kB
facebook-react-native/react/cjs/React-dev.js +0.75% 99.84 kB 100.59 kB +0.82% 24.14 kB 24.34 kB
facebook-www/React-dev.modern.js +0.74% 108.32 kB 109.12 kB +0.79% 26.46 kB 26.67 kB
facebook-www/React-dev.classic.js +0.73% 109.34 kB 110.14 kB +0.79% 26.66 kB 26.87 kB
oss-stable-semver/react/umd/react.development.js +0.64% 107.29 kB 107.97 kB +0.74% 27.40 kB 27.60 kB
oss-stable/react/umd/react.development.js +0.64% 107.29 kB 107.97 kB +0.74% 27.40 kB 27.60 kB
oss-experimental/react/umd/react.development.js +0.64% 108.01 kB 108.70 kB +0.75% 27.49 kB 27.70 kB
facebook-www/JSXDEVRuntime-dev.modern.js +0.26% 41.98 kB 42.09 kB +0.25% 11.72 kB 11.75 kB
facebook-www/JSXDEVRuntime-dev.classic.js +0.26% 41.98 kB 42.09 kB +0.25% 11.72 kB 11.75 kB
facebook-www/ReactTestUtils-dev.modern.js +0.21% 51.41 kB 51.52 kB +0.17% 14.38 kB 14.40 kB
facebook-www/ReactTestUtils-dev.classic.js +0.21% 51.41 kB 51.52 kB +0.17% 14.37 kB 14.40 kB

Generated by 🚫 dangerJS against 10734d2

Copy link
Member

@rickhanlonii rickhanlonii left a comment

Choose a reason for hiding this comment

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

Looks great!

packages/react/src/ReactStartTransition.js Show resolved Hide resolved
Copy link
Collaborator

@acdlite acdlite left a comment

Choose a reason for hiding this comment

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

Nice! Looks good to me

@salazarm salazarm merged commit a3fde23 into facebook:main Sep 8, 2021
facebook-github-bot pushed a commit to facebook/react-native that referenced this pull request Sep 22, 2021
Summary:
This sync includes the following changes:
- **[f4ac680c7](facebook/react@f4ac680c7 )**: Fixed broken build script --unsafe-partial flag ([#22324](facebook/react#22324)) //<Brian Vaughn>//
- **[67222f044](facebook/react@67222f044 )**: [Experiment] Warn if callback ref returns a function ([#22313](facebook/react#22313)) //<Dan Abramov>//
- **[263cfa6ec](facebook/react@263cfa6ec )**: [Experimental] Add useInsertionEffect ([#21913](facebook/react#21913)) //<Ricky>//
- **[806aaa2e2](facebook/react@806aaa2e2 )**: [useSES shim] Import prefixed native API ([#22310](facebook/react#22310)) //<Andrew Clark>//
- **[fd5e01c2e](facebook/react@fd5e01c2e )**: [useSES/extra] Reuse old selection if possible ([#22307](facebook/react#22307)) //<Andrew Clark>//
- **[33226fada](facebook/react@33226fada )**: Check for store mutations before commit ([#22290](facebook/react#22290)) //<Andrew Clark>//
- **[86c7ca70a](facebook/react@86c7ca70a )**: Fix link ([#22296](facebook/react#22296)) //<Konstantin Popov>//
- **[0fd195f29](facebook/react@0fd195f29 )**: update error message to include useLayoutEffect or useEffect on bad e… ([#22279](facebook/react#22279)) //<salazarm>//
- **[8f96c6b2a](facebook/react@8f96c6b2a )**: [Bugfix] Prevent infinite update loop caused by a synchronous update in a passive effect ([#22277](facebook/react#22277)) //<Andrew Clark>//
- **[4ce89a58d](facebook/react@4ce89a58d )**: Test bad useEffect return value with noop-renderer ([#22258](facebook/react#22258)) //<Sebastian Silbermann>//
- **[a3fde2358](facebook/react@a3fde2358 )**: Detect subscriptions wrapped in startTransition ([#22271](facebook/react#22271)) //<salazarm>//

Changelog:
[General][Changed] - React Native sync for revisions 95d762e...e8feb11

jest_e2e[run_all_tests]

Reviewed By: rickhanlonii

Differential Revision: D30966369

fbshipit-source-id: 6c88e591005deb1fd93493628ef4695add49186c
@salazarm salazarm deleted the detectSubscriptionsWrappedInStartTransition branch September 27, 2021 22:56
zhengjitf pushed a commit to zhengjitf/react that referenced this pull request Apr 15, 2022
* Detect subscriptions wrapped in startTransition
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.

6 participants