Skip to content

Commit e7610ef

Browse files
authored
Fix demo (#4551)
1 parent 84dcf74 commit e7610ef

File tree

7 files changed

+496
-194
lines changed

7 files changed

+496
-194
lines changed

compat/src/hooks.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { useState, useLayoutEffect, useEffect } from 'preact/hooks';
2+
import { is } from './util';
3+
4+
/**
5+
* This is taken from https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L84
6+
* on a high level this cuts out the warnings, ... and attempts a smaller implementation
7+
* @typedef {{ _value: any; _getSnapshot: () => any }} Store
8+
*/
9+
export function useSyncExternalStore(subscribe, getSnapshot) {
10+
const value = getSnapshot();
11+
12+
/**
13+
* @typedef {{ _instance: Store }} StoreRef
14+
* @type {[StoreRef, (store: StoreRef) => void]}
15+
*/
16+
const [{ _instance }, forceUpdate] = useState({
17+
_instance: { _value: value, _getSnapshot: getSnapshot }
18+
});
19+
20+
useLayoutEffect(() => {
21+
_instance._value = value;
22+
_instance._getSnapshot = getSnapshot;
23+
24+
if (didSnapshotChange(_instance)) {
25+
forceUpdate({ _instance });
26+
}
27+
}, [subscribe, value, getSnapshot]);
28+
29+
useEffect(() => {
30+
if (didSnapshotChange(_instance)) {
31+
forceUpdate({ _instance });
32+
}
33+
34+
return subscribe(() => {
35+
if (didSnapshotChange(_instance)) {
36+
forceUpdate({ _instance });
37+
}
38+
});
39+
}, [subscribe]);
40+
41+
return value;
42+
}
43+
44+
/** @type {(inst: Store) => boolean} */
45+
function didSnapshotChange(inst) {
46+
const latestGetSnapshot = inst._getSnapshot;
47+
const prevValue = inst._value;
48+
try {
49+
const nextValue = latestGetSnapshot();
50+
return !is(prevValue, nextValue);
51+
} catch (error) {
52+
return true;
53+
}
54+
}
55+
56+
export function startTransition(cb) {
57+
cb();
58+
}
59+
60+
export function useDeferredValue(val) {
61+
return val;
62+
}
63+
64+
export function useTransition() {
65+
return [false, startTransition];
66+
}
67+
68+
// TODO: in theory this should be done after a VNode is diffed as we want to insert
69+
// styles/... before it attaches
70+
export const useInsertionEffect = useLayoutEffect;

compat/src/index.js

Lines changed: 12 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ import {
2020
useContext,
2121
useDebugValue
2222
} from 'preact/hooks';
23+
import {
24+
useInsertionEffect,
25+
startTransition,
26+
useDeferredValue,
27+
useSyncExternalStore,
28+
useTransition
29+
} from './hooks';
2330
import { PureComponent } from './PureComponent';
2431
import { memo } from './memo';
2532
import { forwardRef } from './forwardRef';
2633
import { Children } from './Children';
2734
import { Suspense, lazy } from './suspense';
2835
import { SuspenseList } from './suspense-list';
2936
import { createPortal } from './portals';
30-
import { is } from './util';
3137
import {
3238
hydrate,
3339
render,
@@ -143,77 +149,9 @@ const flushSync = (callback, arg) => callback(arg);
143149
*/
144150
const StrictMode = Fragment;
145151

146-
export function startTransition(cb) {
147-
cb();
148-
}
149-
150-
export function useDeferredValue(val) {
151-
return val;
152-
}
153-
154-
export function useTransition() {
155-
return [false, startTransition];
156-
}
157-
158-
// TODO: in theory this should be done after a VNode is diffed as we want to insert
159-
// styles/... before it attaches
160-
export const useInsertionEffect = useLayoutEffect;
161-
162152
// compat to react-is
163153
export const isElement = isValidElement;
164154

165-
/**
166-
* This is taken from https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L84
167-
* on a high level this cuts out the warnings, ... and attempts a smaller implementation
168-
* @typedef {{ _value: any; _getSnapshot: () => any }} Store
169-
*/
170-
export function useSyncExternalStore(subscribe, getSnapshot) {
171-
const value = getSnapshot();
172-
173-
/**
174-
* @typedef {{ _instance: Store }} StoreRef
175-
* @type {[StoreRef, (store: StoreRef) => void]}
176-
*/
177-
const [{ _instance }, forceUpdate] = useState({
178-
_instance: { _value: value, _getSnapshot: getSnapshot }
179-
});
180-
181-
useLayoutEffect(() => {
182-
_instance._value = value;
183-
_instance._getSnapshot = getSnapshot;
184-
185-
if (didSnapshotChange(_instance)) {
186-
forceUpdate({ _instance });
187-
}
188-
}, [subscribe, value, getSnapshot]);
189-
190-
useEffect(() => {
191-
if (didSnapshotChange(_instance)) {
192-
forceUpdate({ _instance });
193-
}
194-
195-
return subscribe(() => {
196-
if (didSnapshotChange(_instance)) {
197-
forceUpdate({ _instance });
198-
}
199-
});
200-
}, [subscribe]);
201-
202-
return value;
203-
}
204-
205-
/** @type {(inst: Store) => boolean} */
206-
function didSnapshotChange(inst) {
207-
const latestGetSnapshot = inst._getSnapshot;
208-
const prevValue = inst._value;
209-
try {
210-
const nextValue = latestGetSnapshot();
211-
return !is(prevValue, nextValue);
212-
} catch (error) {
213-
return true;
214-
}
215-
}
216-
217155
export * from 'preact/hooks';
218156
export {
219157
version,
@@ -237,6 +175,11 @@ export {
237175
memo,
238176
forwardRef,
239177
flushSync,
178+
useInsertionEffect,
179+
startTransition,
180+
useDeferredValue,
181+
useSyncExternalStore,
182+
useTransition,
240183
// eslint-disable-next-line camelcase
241184
unstable_batchedUpdates,
242185
StrictMode,

demo/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import NestedSuspenseBug from './nested-suspense';
2626
import Contenteditable from './contenteditable';
2727
import { MobXDemo } from './mobx';
2828
import Zustand from './zustand';
29-
import ReduxToolkit from './redux_toolkit';
29+
import ReduxToolkit from './redux-toolkit';
3030

3131
let isBenchmark = /(\/spiral|\/pythagoras|[#&]bench)/g.test(
3232
window.location.href

0 commit comments

Comments
 (0)