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

fix: implementation of useSyncExternalStore #291

Merged
merged 3 commits into from
Jan 11, 2023
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
2 changes: 1 addition & 1 deletion benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@types/benchmark": "^2.1.2",
"@types/canvas-confetti": "^1.6.0",
"@types/lodash": "^4.14.191",
"@types/node": "^18.11.17",
"@types/node": "^18.11.18",
"@types/virtual-dom": "^2.1.1",
"@vitejs/plugin-legacy": "^2.3.1",
"benchmark": "^2.1.4",
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,23 @@
"homepage": "https://github.com/aidenybai/million",
"prettier": "@vercel/style-guide/prettier",
"devDependencies": {
"@babel/core": "^7.20.7",
"@types/node": "^18.11.17",
"@typescript-eslint/eslint-plugin": "^5.47.0",
"@typescript-eslint/parser": "^5.47.0",
"@babel/core": "^7.20.12",
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"@vercel/style-guide": "^3.0.0",
"bumpp": "^8.2.1",
"c8": "^7.12.0",
"esbuild": "^0.14.54",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.5.0",
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.6.0",
"export-size": "^0.5.2",
"fsxx": "^0.1.0",
"husky": "^8.0.2",
"husky": "^8.0.3",
"jsdom": "^20.0.3",
"np": "^7.6.2",
"np": "^7.6.3",
"ora": "^6.1.2",
"prettier": "^2.8.1",
"prettier": "^2.8.2",
"recast": "^0.21.5",
"simple-git": "^3.15.1",
"source-map": "^0.7.4",
Expand Down
61 changes: 45 additions & 16 deletions packages/react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// - https://github.com/WebReflection/augmentor/blob/master/esm/index.js
// - https://github.com/WebReflection/umap/blob/master/esm/index.js

/* eslint-disable no-self-compare */
/* eslint-disable no-console */
/* eslint-disable eqeqeq */
/* eslint-disable no-sequences */
Expand Down Expand Up @@ -243,26 +244,50 @@ export const useDeferredValue = (value: any) => {
export const useInsertionEffect = useLayoutEffect;

// useSyncExternalStore
// This implementation is grokked straight from Preact (ref: https://github.com/preactjs/preact/blob/6b92b1fab41599e6da4f96d65b07fdbe0b6ff2fc/compat/src/index.js#L136-L154)
// This implementation is grokked straight from Preact (ref: https://github.com/preactjs/preact/blob/528a7760c1cdec7515d514c71875860528e6dc12/compat/src/index.js#L142-L171)
// which is in turn ported from React's shim (ref: https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js)
// (c) 2015-present Jason Miller
//
// Disclaimer: This shim breaks many of the rules of React, and only works
// because updates are always synchronous. Million.js doesn't have concurrent
// rendering (Both preact and Million.js performs synchronous rendering just
// like React pre-18), and it is the only reason why react's shim also works for
// Million.js (and preact).
export const useSyncExternalStore = (subscribe, getSnapshot) => {
const [state, setState] = useState(getSnapshot);

// Read the current snapshot from the store on every render. This breaks the
// normal rules of React, and only works because store updates are
// always synchronous.
const value = getSnapshot();

useLayoutEffect(() => {
if (value !== state) {
setState(() => value);
}
}, [subscribe, value, getSnapshot]);

useEffect(() => {
return subscribe(() => {
setState(() => getSnapshot());
});
}, [subscribe, getSnapshot]);

return state;
// Because updates are synchronous, we force a re-render whenever the
// subscribed state changes by updating an some arbitrary useState hook. Then,
// during render, we call getSnapshot to read the current value.
const [{ instance }, forceUpdate] = useState({
instance: { value, getSnapshot }
});

useLayoutEffect(() => {
instance.value = value;
instance.getSnapshot = getSnapshot;

if (!is(value, getSnapshot())) {
forceUpdate({ instance });
}
}, [subscribe, value, getSnapshot]);

useEffect(() => {
if (!is(instance.value, instance.getSnapshot())) {
forceUpdate({ instance });
}

return subscribe(() => {
if (!is(instance.value, instance.getSnapshot())) {
forceUpdate({ instance });
}
});
}, [subscribe]);

return value;
}

// useImperativeHandle
Expand All @@ -277,6 +302,10 @@ function different(value, i) {
return value !== this[i];
}

function is(x, y) {
return (x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y);
}

// useDelta
export const useDelta = () => {
const delta: DeltaTypes[] & {
Expand Down
Loading