-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
perf(hooks): Optimize useSyncExternalStoreWithSelector selector calls #32040
Open
Biki-das
wants to merge
6
commits into
facebook:main
Choose a base branch
from
Biki-das:useSync-optimise
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+289
−17
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d4bec8
perf(hooks): Optimize useSyncExternalStoreWithSelector selector calls
Biki-das aeab8bc
removed eslint directives
Biki-das dd01f2d
remove unnecessary async call
Biki-das c4c9e87
removed unused variables
Biki-das dbc1822
removed declare variables
Biki-das 85ef2ab
revert directives
Biki-das File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
206 changes: 206 additions & 0 deletions
206
packages/use-sync-external-store/src/__tests__/useSyncExternalStoreWithSelector-test.js
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,206 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and 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 useSyncExternalStoreWithSelector; | ||
let React; | ||
let ReactDOM; | ||
let ReactDOMClient; | ||
let act; | ||
|
||
describe('useSyncExternalStoreWithSelector', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
|
||
if (gate(flags => flags.enableUseSyncExternalStoreShim)) { | ||
// Remove useSyncExternalStore from the React imports so that we use the | ||
// shim instead. Also removing startTransition, since we use that to | ||
// detect outdated 18 alphas that don't yet include useSyncExternalStore. | ||
// | ||
// Longer term, we'll probably test this branch using an actual build | ||
// of React 17. | ||
jest.mock('react', () => { | ||
const { | ||
startTransition: _, | ||
useSyncExternalStore: __, | ||
...otherExports | ||
} = jest.requireActual('react'); | ||
return otherExports; | ||
}); | ||
} | ||
|
||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
ReactDOMClient = require('react-dom/client'); | ||
const internalAct = require('internal-test-utils').act; | ||
|
||
// The internal act implementation doesn't batch updates by default, since | ||
// it's mostly used to test concurrent mode. But since these tests run | ||
// in both concurrent and legacy mode, I'm adding batching here. | ||
act = cb => internalAct(() => ReactDOM.unstable_batchedUpdates(cb)); | ||
|
||
if (gate(flags => flags.source)) { | ||
// The `shim/with-selector` module composes the main | ||
// `use-sync-external-store` entrypoint. In the compiled artifacts, this | ||
// is resolved to the `shim` implementation by our build config, but when | ||
// running the tests against the source files, we need to tell Jest how to | ||
// resolve it. Because this is a source module, this mock has no affect on | ||
// the build tests. | ||
jest.mock('use-sync-external-store/src/useSyncExternalStore', () => | ||
jest.requireActual('use-sync-external-store/shim'), | ||
); | ||
} | ||
useSyncExternalStoreWithSelector = | ||
require('use-sync-external-store/shim/with-selector').useSyncExternalStoreWithSelector; | ||
}); | ||
|
||
function createRoot(container) { | ||
// This wrapper function exists so we can test both legacy roots and | ||
// concurrent roots. | ||
if (gate(flags => !flags.enableUseSyncExternalStoreShim)) { | ||
// The native implementation only exists in 18+, so we test using | ||
// concurrent mode. | ||
return ReactDOMClient.createRoot(container); | ||
} else { | ||
// For legacy mode, use ReactDOM.createRoot instead of ReactDOM.render | ||
const root = ReactDOMClient.createRoot(container); | ||
return { | ||
render(children) { | ||
root.render(children); | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
function createExternalStore(initialState) { | ||
const listeners = new Set(); | ||
let currentState = initialState; | ||
return { | ||
set(text) { | ||
currentState = text; | ||
ReactDOM.unstable_batchedUpdates(() => { | ||
listeners.forEach(listener => listener()); | ||
}); | ||
}, | ||
subscribe(listener) { | ||
listeners.add(listener); | ||
return () => listeners.delete(listener); | ||
}, | ||
getState() { | ||
return currentState; | ||
}, | ||
getSubscriberCount() { | ||
return listeners.size; | ||
}, | ||
}; | ||
} | ||
|
||
test('should call selector on change accessible segment', async () => { | ||
const store = createExternalStore({a: '1', b: '2'}); | ||
|
||
const selectorFn = jest.fn(); | ||
const selector = state => { | ||
selectorFn(); | ||
return state.a; | ||
}; | ||
|
||
function App() { | ||
const data = useSyncExternalStoreWithSelector( | ||
store.subscribe, | ||
store.getState, | ||
null, | ||
selector, | ||
); | ||
return <>{data}</>; | ||
} | ||
|
||
const container = document.createElement('div'); | ||
const root = createRoot(container); | ||
await act(() => { | ||
root.render(<App />); | ||
}); | ||
|
||
expect(selectorFn).toHaveBeenCalledTimes(1); | ||
|
||
await act(() => { | ||
store.set({a: '2', b: '2'}); | ||
}); | ||
|
||
expect(selectorFn).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
test('should not call selector if nothing changed', async () => { | ||
const store = createExternalStore({a: '1', b: '2'}); | ||
|
||
const selectorFn = jest.fn(); | ||
const selector = state => { | ||
selectorFn(); | ||
return state.a; | ||
}; | ||
|
||
function App() { | ||
const data = useSyncExternalStoreWithSelector( | ||
store.subscribe, | ||
store.getState, | ||
null, | ||
selector, | ||
); | ||
return <>{data}</>; | ||
} | ||
|
||
const container = document.createElement('div'); | ||
const root = createRoot(container); | ||
await act(() => { | ||
root.render(<App />); | ||
}); | ||
|
||
expect(selectorFn).toHaveBeenCalledTimes(1); | ||
|
||
await act(() => { | ||
store.set({a: '1', b: '2'}); | ||
}); | ||
|
||
expect(selectorFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('should not call selector on change not accessible segment', async () => { | ||
const store = createExternalStore({a: '1', b: '2'}); | ||
|
||
const selectorFn = jest.fn(); | ||
const selector = state => { | ||
selectorFn(); | ||
return state.a; | ||
}; | ||
|
||
function App() { | ||
const data = useSyncExternalStoreWithSelector( | ||
store.subscribe, | ||
store.getState, | ||
null, | ||
selector, | ||
); | ||
return <>{data}</>; | ||
} | ||
|
||
const container = document.createElement('div'); | ||
const root = createRoot(container); | ||
await act(() => { | ||
root.render(<App />); | ||
}); | ||
|
||
expect(selectorFn).toHaveBeenCalledTimes(1); | ||
|
||
await act(() => { | ||
store.set({a: '1', b: '3'}); | ||
}); | ||
|
||
expect(selectorFn).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@markerikson you were curious for the case when a a primitive value is passed, this lines will handle the same.
For Primitive Values