UNPKG

@mui/x-data-grid

Version:

The Community plan edition of the Data Grid components (MUI X).

143 lines (140 loc) 5.04 kB
import * as React from 'react'; import { fastObjectShallowCompare } from '@mui/x-internals/fastObjectShallowCompare'; import { warnOnce } from '@mui/x-internals/warning'; import { useSyncExternalStore } from 'use-sync-external-store/shim'; import { useLazyRef } from "./useLazyRef.js"; function isOutputSelector(selector) { return selector.acceptsApiRef; } // TODO v8: Remove this function function applySelector(apiRef, selector) { if (isOutputSelector(selector)) { return selector(apiRef); } return selector(apiRef.current.state); } // TODO v8: Rename this function to `applySelector` function applySelectorV8(apiRef, selector, args, instanceId) { if (isOutputSelector(selector)) { return selector(apiRef, args); } return selector(apiRef.current.state, instanceId); } const defaultCompare = Object.is; export const objectShallowCompare = fastObjectShallowCompare; const arrayShallowCompare = (a, b) => { if (a === b) { return true; } return a.length === b.length && a.every((v, i) => v === b[i]); }; export const argsEqual = (prev, curr) => { let fn = Object.is; if (curr instanceof Array) { fn = arrayShallowCompare; } else if (curr instanceof Object) { fn = objectShallowCompare; } return fn(prev, curr); }; const createRefs = () => ({ state: null, equals: null, selector: null, args: undefined }); const EMPTY = []; const emptyGetSnapshot = () => null; // TODO v8: Remove this function export const useGridSelector = (apiRef, selector, equals = defaultCompare) => { if (process.env.NODE_ENV !== 'production') { if (!apiRef.current.state) { warnOnce(['MUI X: `useGridSelector` has been called before the initialization of the state.', 'This hook can only be used inside the context of the grid.']); } } const refs = useLazyRef(createRefs); const didInit = refs.current.selector !== null; const [state, setState] = React.useState( // We don't use an initialization function to avoid allocations didInit ? null : applySelector(apiRef, selector)); refs.current.state = state; refs.current.equals = equals; refs.current.selector = selector; const subscribe = React.useCallback(() => { if (refs.current.subscription) { return null; } refs.current.subscription = apiRef.current.store.subscribe(() => { const newState = applySelector(apiRef, refs.current.selector); if (!refs.current.equals(refs.current.state, newState)) { refs.current.state = newState; setState(newState); } }); return null; }, // eslint-disable-next-line react-hooks/exhaustive-deps EMPTY); const unsubscribe = React.useCallback(() => { return () => { if (refs.current.subscription) { refs.current.subscription(); refs.current.subscription = undefined; } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, EMPTY); useSyncExternalStore(unsubscribe, subscribe, emptyGetSnapshot); return state; }; // TODO v8: Rename this function to `useGridSelector` export const useGridSelectorV8 = (apiRef, selector, args = undefined, equals = defaultCompare) => { if (process.env.NODE_ENV !== 'production') { if (!apiRef.current.state) { warnOnce(['MUI X: `useGridSelector` has been called before the initialization of the state.', 'This hook can only be used inside the context of the grid.']); } } const refs = useLazyRef(createRefs); const didInit = refs.current.selector !== null; const [state, setState] = React.useState( // We don't use an initialization function to avoid allocations didInit ? null : applySelectorV8(apiRef, selector, args, apiRef.current.instanceId)); refs.current.state = state; refs.current.equals = equals; refs.current.selector = selector; const prevArgs = refs.current.args; refs.current.args = args; if (didInit && !argsEqual(prevArgs, args)) { const newState = applySelectorV8(apiRef, refs.current.selector, refs.current.args, apiRef.current.instanceId); if (!refs.current.equals(refs.current.state, newState)) { refs.current.state = newState; setState(newState); } } const subscribe = React.useCallback(() => { if (refs.current.subscription) { return null; } refs.current.subscription = apiRef.current.store.subscribe(() => { const newState = applySelectorV8(apiRef, refs.current.selector, refs.current.args, apiRef.current.instanceId); if (!refs.current.equals(refs.current.state, newState)) { refs.current.state = newState; setState(newState); } }); return null; }, // eslint-disable-next-line react-hooks/exhaustive-deps EMPTY); const unsubscribe = React.useCallback(() => { return () => { if (refs.current.subscription) { refs.current.subscription(); refs.current.subscription = undefined; } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, EMPTY); useSyncExternalStore(unsubscribe, subscribe, emptyGetSnapshot); return state; };