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

useSelect: improve transition from async to sync mode #40680

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions packages/data/src/components/use-select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ export default function useSelect( mapSelect, deps ) {

const registry = useRegistry();
const isAsync = useAsyncMode();
// React can sometimes clear the `useMemo` cache.
// We use the cache-stable `useMemoOne` to avoid
// losing queues.
const queueContext = useMemoOne( () => ( { queue: true } ), [ registry ] );
const [ , forceRender ] = useReducer( ( s ) => s + 1, 0 );

const latestRegistry = useRef( registry );
const latestMapSelect = useRef();
Expand Down Expand Up @@ -147,11 +142,13 @@ export default function useSelect( mapSelect, deps ) {
mapOutput = latestMapOutput.current;
const hasReplacedRegistry = latestRegistry.current !== registry;
const hasReplacedMapSelect = latestMapSelect.current !== _mapSelect;
const hasLeftAsyncMode = latestIsAsync.current && ! isAsync;
const lastMapSelectFailed = !! latestMapOutputError.current;

if (
hasReplacedRegistry ||
hasReplacedMapSelect ||
hasLeftAsyncMode ||
lastMapSelectFailed
) {
try {
Expand All @@ -178,19 +175,16 @@ export default function useSelect( mapSelect, deps ) {

latestRegistry.current = registry;
latestMapSelect.current = _mapSelect;
latestIsAsync.current = isAsync;
latestMapOutput.current = mapOutput;
latestMapOutputError.current = undefined;

// This has to run after the other ref updates
// to avoid using stale values in the flushed
// callbacks or potentially overwriting a
// changed `latestMapOutput.current`.
if ( latestIsAsync.current !== isAsync ) {
latestIsAsync.current = isAsync;
renderQueue.flush( queueContext );
}
} );

// React can sometimes clear the `useMemo` cache.
// We use the cache-stable `useMemoOne` to avoid
// losing queues.
const queueContext = useMemoOne( () => ( { queue: true } ), [ registry ] );
const [ , forceRender ] = useReducer( ( s ) => s + 1, 0 );
const isMounted = useRef( false );

useIsomorphicLayoutEffect( () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/data/src/components/use-select/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,10 +884,10 @@ describe( 'useSelect', () => {
// Ensure the async update was flushed during the rerender.
expect( rendered.getByRole( 'status' ) ).toHaveTextContent( 1 );

// initial render + subscription check + flushed store update
// initial render + subscription check + rerender with isAsync=false
expect( selectSpy ).toHaveBeenCalledTimes( 3 );
// initial render + rerender with isAsync=false + store state update
expect( TestComponent ).toHaveBeenCalledTimes( 3 );
// initial render + rerender with isAsync=false
expect( TestComponent ).toHaveBeenCalledTimes( 2 );
Copy link
Contributor

Choose a reason for hiding this comment

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

This sounds like a potential performance improvement.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately this happens only in a very rare condition where there actually was a pending store update when the component was switching from async to sync mode.

} );

it( 'cancels scheduled updates when mapSelect function changes', async () => {
Expand Down