-
Notifications
You must be signed in to change notification settings - Fork 48.9k
Initial shim of useSyncExternalStore #22211
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
Merged
acdlite
merged 1 commit into
facebook:main
from
acdlite:initial-use-sync-external-store-shim
Sep 2, 2021
+845
−7
Merged
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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,7 @@ | ||
'use strict'; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./cjs/use-sync-external-store-extra.production.min.js'); | ||
} else { | ||
module.exports = require('./cjs/use-sync-external-store-extra.development.js'); | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
"README.md", | ||
"build-info.json", | ||
"index.js", | ||
"extra.js", | ||
"cjs/" | ||
], | ||
"license": "MIT", | ||
|
621 changes: 621 additions & 0 deletions
621
packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
76 changes: 76 additions & 0 deletions
76
packages/use-sync-external-store/src/useSyncExternalStoreExtra.js
This file contains hidden or 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,76 @@ | ||
/** | ||
* 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. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import is from 'shared/objectIs'; | ||
import {useSyncExternalStore} from 'use-sync-external-store'; | ||
|
||
// Intentionally not using named imports because Rollup uses dynamic | ||
// dispatch for CommonJS interop named imports. | ||
const {useMemo, useDebugValue} = React; | ||
|
||
// Same as useSyncExternalStore, but supports selector and isEqual arguments. | ||
export function useSyncExternalStoreExtra<Snapshot, Selection>( | ||
subscribe: (() => void) => () => void, | ||
getSnapshot: () => Snapshot, | ||
selector: (snapshot: Snapshot) => Selection, | ||
isEqual?: (a: Selection, b: Selection) => boolean, | ||
): Selection { | ||
const getSnapshotWithMemoizedSelector = useMemo(() => { | ||
// Track the memoized state using closure variables that are local to this | ||
// memoized instance of a getSnapshot function. Intentionally not using a | ||
// useRef hook, because that state would be shared across all concurrent | ||
// copies of the hook/component. | ||
let hasMemo = false; | ||
let memoizedSnapshot; | ||
let memoizedSelection; | ||
return () => { | ||
const nextSnapshot = getSnapshot(); | ||
|
||
if (!hasMemo) { | ||
// The first time the hook is called, there is no memoized result. | ||
hasMemo = true; | ||
memoizedSnapshot = nextSnapshot; | ||
const nextSelection = selector(nextSnapshot); | ||
memoizedSelection = nextSelection; | ||
return nextSelection; | ||
} | ||
|
||
// We may be able to reuse the previous invocation's result. | ||
const prevSnapshot: Snapshot = (memoizedSnapshot: any); | ||
const prevSelection: Selection = (memoizedSelection: any); | ||
|
||
if (is(prevSnapshot, nextSnapshot)) { | ||
// The snapshot is the same as last time. Reuse the previous selection. | ||
return prevSelection; | ||
} | ||
|
||
// The snapshot has changed, so we need to compute a new selection. | ||
memoizedSnapshot = nextSnapshot; | ||
const nextSelection = selector(nextSnapshot); | ||
|
||
// If a custom isEqual function is provided, use that to check if the data | ||
// has changed. If it hasn't, return the previous selection. That signals | ||
// to React that the selections are conceptually equal, and we can bail | ||
// out of rendering. | ||
if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) { | ||
return prevSelection; | ||
} | ||
|
||
memoizedSelection = nextSelection; | ||
return nextSelection; | ||
}; | ||
}, [getSnapshot, selector, isEqual]); | ||
const value = useSyncExternalStore( | ||
subscribe, | ||
getSnapshotWithMemoizedSelector, | ||
); | ||
useDebugValue(value); | ||
return value; | ||
} |
This file contains hidden or 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
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.
This should move to
useEffect
.