-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84dcf74
commit 22d61f0
Showing
7 changed files
with
496 additions
and
194 deletions.
There are no files selected for viewing
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,70 @@ | ||
import { useState, useLayoutEffect, useEffect } from 'preact/hooks'; | ||
import { is } from './util'; | ||
|
||
/** | ||
* This is taken from https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L84 | ||
* on a high level this cuts out the warnings, ... and attempts a smaller implementation | ||
* @typedef {{ _value: any; _getSnapshot: () => any }} Store | ||
*/ | ||
export function useSyncExternalStore(subscribe, getSnapshot) { | ||
const value = getSnapshot(); | ||
|
||
/** | ||
* @typedef {{ _instance: Store }} StoreRef | ||
* @type {[StoreRef, (store: StoreRef) => void]} | ||
*/ | ||
const [{ _instance }, forceUpdate] = useState({ | ||
_instance: { _value: value, _getSnapshot: getSnapshot } | ||
}); | ||
|
||
useLayoutEffect(() => { | ||
_instance._value = value; | ||
_instance._getSnapshot = getSnapshot; | ||
|
||
if (didSnapshotChange(_instance)) { | ||
forceUpdate({ _instance }); | ||
} | ||
}, [subscribe, value, getSnapshot]); | ||
|
||
useEffect(() => { | ||
if (didSnapshotChange(_instance)) { | ||
forceUpdate({ _instance }); | ||
} | ||
|
||
return subscribe(() => { | ||
if (didSnapshotChange(_instance)) { | ||
forceUpdate({ _instance }); | ||
} | ||
}); | ||
}, [subscribe]); | ||
|
||
return value; | ||
} | ||
|
||
/** @type {(inst: Store) => boolean} */ | ||
function didSnapshotChange(inst) { | ||
const latestGetSnapshot = inst._getSnapshot; | ||
const prevValue = inst._value; | ||
try { | ||
const nextValue = latestGetSnapshot(); | ||
return !is(prevValue, nextValue); | ||
} catch (error) { | ||
return true; | ||
} | ||
} | ||
|
||
export function startTransition(cb) { | ||
cb(); | ||
} | ||
|
||
export function useDeferredValue(val) { | ||
return val; | ||
} | ||
|
||
export function useTransition() { | ||
return [false, startTransition]; | ||
} | ||
|
||
// TODO: in theory this should be done after a VNode is diffed as we want to insert | ||
// styles/... before it attaches | ||
export const useInsertionEffect = useLayoutEffect; |
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
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.