-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect subscriptions wrapped in startTransition (#22271)
* Detect subscriptions wrapped in startTransition
- Loading branch information
Showing
17 changed files
with
184 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
let ReactTestRenderer; | ||
let act; | ||
let useState; | ||
let useTransition; | ||
|
||
const SUSPICIOUS_NUMBER_OF_FIBERS_UPDATED = 10; | ||
|
||
describe('ReactStartTransition', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactTestRenderer = require('react-test-renderer'); | ||
act = require('jest-react').act; | ||
useState = React.useState; | ||
useTransition = React.useTransition; | ||
}); | ||
|
||
// @gate warnOnSubscriptionInsideStartTransition || !__DEV__ | ||
it('Warns if a suspicious number of fibers are updated inside startTransition', () => { | ||
const subs = new Set(); | ||
const useUserSpaceSubscription = () => { | ||
const setState = useState(0)[1]; | ||
subs.add(setState); | ||
}; | ||
|
||
let triggerHookTransition; | ||
|
||
const Component = ({level}) => { | ||
useUserSpaceSubscription(); | ||
if (level === 0) { | ||
triggerHookTransition = useTransition()[1]; | ||
} | ||
if (level < SUSPICIOUS_NUMBER_OF_FIBERS_UPDATED) { | ||
return <Component level={level + 1} />; | ||
} | ||
return null; | ||
}; | ||
|
||
act(() => { | ||
ReactTestRenderer.create(<Component level={0} />, { | ||
unstable_isConcurrent: true, | ||
}); | ||
}); | ||
|
||
expect(() => { | ||
act(() => { | ||
React.startTransition(() => { | ||
subs.forEach(setState => { | ||
setState(state => state + 1); | ||
}); | ||
}); | ||
}); | ||
}).toWarnDev( | ||
[ | ||
'Detected a large number of updates inside startTransition. ' + | ||
'If this is due to a subscription please re-write it to use React provided hooks. ' + | ||
'Otherwise concurrent mode guarantees are off the table.', | ||
], | ||
{withoutStack: true}, | ||
); | ||
|
||
expect(() => { | ||
act(() => { | ||
triggerHookTransition(() => { | ||
subs.forEach(setState => { | ||
setState(state => state + 1); | ||
}); | ||
}); | ||
}); | ||
}).toWarnDev( | ||
[ | ||
'Detected a large number of updates inside startTransition. ' + | ||
'If this is due to a subscription please re-write it to use React provided hooks. ' + | ||
'Otherwise concurrent mode guarantees are off the table.', | ||
], | ||
{withoutStack: true}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters