mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-22 00:14:30 +00:00
55 lines
2.3 KiB
TypeScript
55 lines
2.3 KiB
TypeScript
// `use-sync-external-store` ships CommonJS that calls `require("react")`. React 19 has the hook
|
|
// itself, so the library build answers both entry points from React and bundles no CommonJS React.
|
|
import { useDebugValue, useMemo, useRef, useSyncExternalStore } from "react";
|
|
|
|
export { useSyncExternalStore };
|
|
|
|
export function useSyncExternalStoreWithSelector<Snapshot, Selection>(
|
|
subscribe: (onStoreChange: () => void) => () => void,
|
|
getSnapshot: () => Snapshot,
|
|
getServerSnapshot: undefined | null | (() => Snapshot),
|
|
selector: (snapshot: Snapshot) => Selection,
|
|
isEqual?: (a: Selection, b: Selection) => boolean,
|
|
): Selection {
|
|
const instance = useRef<{ hasValue: boolean; value: Selection | null }>(null);
|
|
if (instance.current === null) instance.current = { hasValue: false, value: null };
|
|
const inst = instance.current;
|
|
const [getSelection, getServerSelection] = useMemo(() => {
|
|
let hasMemo = false;
|
|
let memoizedSnapshot: Snapshot;
|
|
let memoizedSelection: Selection;
|
|
const memoizedSelector = (nextSnapshot: Snapshot): Selection => {
|
|
if (!hasMemo) {
|
|
hasMemo = true;
|
|
memoizedSnapshot = nextSnapshot;
|
|
const nextSelection = selector(nextSnapshot);
|
|
if (isEqual !== undefined && inst.hasValue) {
|
|
const current = inst.value as Selection;
|
|
if (isEqual(current, nextSelection)) return (memoizedSelection = current);
|
|
}
|
|
return (memoizedSelection = nextSelection);
|
|
}
|
|
const prevSnapshot = memoizedSnapshot;
|
|
const prevSelection = memoizedSelection;
|
|
if (Object.is(prevSnapshot, nextSnapshot)) return prevSelection;
|
|
const nextSelection = selector(nextSnapshot);
|
|
if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) {
|
|
memoizedSnapshot = nextSnapshot;
|
|
return prevSelection;
|
|
}
|
|
memoizedSnapshot = nextSnapshot;
|
|
return (memoizedSelection = nextSelection);
|
|
};
|
|
const maybeServer = getServerSnapshot ?? null;
|
|
return [
|
|
() => memoizedSelector(getSnapshot()),
|
|
maybeServer === null ? undefined : () => memoizedSelector(maybeServer()),
|
|
] as const;
|
|
}, [getSnapshot, getServerSnapshot, selector, isEqual, inst]);
|
|
const value = useSyncExternalStore(subscribe, getSelection, getServerSelection);
|
|
inst.hasValue = true;
|
|
inst.value = value;
|
|
useDebugValue(value);
|
|
return value;
|
|
}
|