@@ -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' ;
2330import { PureComponent } from './PureComponent' ;
2431import { memo } from './memo' ;
2532import { forwardRef } from './forwardRef' ;
2633import { Children } from './Children' ;
2734import { Suspense , lazy } from './suspense' ;
2835import { SuspenseList } from './suspense-list' ;
2936import { createPortal } from './portals' ;
30- import { is } from './util' ;
3137import {
3238 hydrate ,
3339 render ,
@@ -143,77 +149,9 @@ const flushSync = (callback, arg) => callback(arg);
143149 */
144150const 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
163153export 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-
217155export * from 'preact/hooks' ;
218156export {
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 ,
0 commit comments