@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
25 lines (24 loc) • 989 B
JavaScript
import { useCallback, useRef, useSyncExternalStore } from "react";
//#region src/base-ui/FormKit/useStoreSelector.ts
const shallowEqual = (a, b) => {
if (Object.is(a, b)) return true;
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
const keysA = Object.keys(a);
if (keysA.length !== Object.keys(b).length) return false;
return keysA.every((key) => Object.is(a[key], b[key]));
};
const useStoreSelector = (engine, select) => {
const selectRef = useRef(select);
selectRef.current = select;
const cache = useRef(null);
const getSnapshot = useCallback(() => {
const next = selectRef.current(engine);
if (cache.current && shallowEqual(cache.current.value, next)) return cache.current.value;
cache.current = { value: next };
return next;
}, [engine]);
return useSyncExternalStore(engine.subscribe, getSnapshot, getSnapshot);
};
//#endregion
export { shallowEqual, useStoreSelector };
//# sourceMappingURL=useStoreSelector.mjs.map