Skip to content

Commit

Permalink
Move caches to unstable_caches
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw committed May 3, 2022
1 parent e875169 commit 37dbfcc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function useGridApiInitialization<Api extends GridApiCommon>(
if (!apiRef.current) {
apiRef.current = {
unstable_eventManager: new EventManager(),
unstable_caches: {},
state: {} as Api['state'],
instanceId: globalId,
} as Api;
Expand Down
28 changes: 14 additions & 14 deletions packages/grid/x-data-grid/src/hooks/features/rows/useGridRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const getRowsStateFromCache = (
export const rowsStateInitializer: GridStateInitializer<
Pick<DataGridProcessedProps, 'rows' | 'rowCount' | 'getRowId' | 'loading'>
> = (state, props, apiRef) => {
apiRef.current.rowsCache = convertRowsPropToState({
apiRef.current.unstable_caches.rows = convertRowsPropToState({
rows: props.rows,
getRowId: props.getRowId,
prevCache: {
Expand All @@ -120,7 +120,7 @@ export const rowsStateInitializer: GridStateInitializer<
return {
...state,
rows: getRowsStateFromCache(
apiRef.current.rowsCache,
apiRef.current.unstable_caches.rows,
null,
apiRef,
props.rowCount,
Expand Down Expand Up @@ -176,7 +176,7 @@ export const useGridRows = (
apiRef.current.setState((state) => ({
...state,
rows: getRowsStateFromCache(
apiRef.current.rowsCache,
apiRef.current.unstable_caches.rows!,
gridRowTreeSelector(apiRef),
apiRef,
props.rowCount,
Expand All @@ -192,7 +192,7 @@ export const useGridRows = (
timeout.current = null;
}

apiRef.current.rowsCache = newCache;
apiRef.current.unstable_caches.rows = newCache;

if (!throttle) {
run();
Expand All @@ -219,7 +219,7 @@ export const useGridRows = (
throttledRowsChange(
convertRowsPropToState({
rows,
prevCache: apiRef.current.rowsCache,
prevCache: apiRef.current.unstable_caches.rows!,
getRowId: props.getRowId,
}),
true,
Expand Down Expand Up @@ -260,9 +260,9 @@ export const useGridRows = (
const deletedRowIds: GridRowId[] = [];

const newStateValue: GridRowInternalCacheValue = {
idRowsLookup: { ...apiRef.current.rowsCache.value.idRowsLookup },
idToIdLookup: { ...apiRef.current.rowsCache.value.idToIdLookup },
ids: [...apiRef.current.rowsCache.value.ids],
idRowsLookup: { ...apiRef.current.unstable_caches.rows!.value.idRowsLookup },
idToIdLookup: { ...apiRef.current.unstable_caches.rows!.value.idToIdLookup },
ids: [...apiRef.current.unstable_caches.rows!.value.ids],
};

uniqUpdates.forEach((partialRow, id) => {
Expand Down Expand Up @@ -290,7 +290,7 @@ export const useGridRows = (
}

const state: GridRowsInternalCache = {
...apiRef.current.rowsCache,
...apiRef.current.unstable_caches.rows!,
value: newStateValue,
};

Expand Down Expand Up @@ -410,7 +410,7 @@ export const useGridRows = (
[apiRef, logger],
);

const rowApi: Omit<GridRowApi, 'rowsCache'> = {
const rowApi: GridRowApi = {
getRow,
getRowModels,
getRowsCount,
Expand All @@ -431,7 +431,7 @@ export const useGridRows = (
logger.info(`Row grouping pre-processing have changed, regenerating the row tree`);

let rows: GridRowsProp | undefined;
if (apiRef.current.rowsCache.rowsBeforePartialUpdates === props.rows) {
if (apiRef.current.unstable_caches.rows!.rowsBeforePartialUpdates === props.rows) {
// The `props.rows` has not changed since the last row grouping
// We can keep the potential updates stored in `inputRowsAfterUpdates` on the new grouping
rows = undefined;
Expand All @@ -445,7 +445,7 @@ export const useGridRows = (
convertRowsPropToState({
rows,
getRowId: props.getRowId,
prevCache: apiRef.current.rowsCache,
prevCache: apiRef.current.unstable_caches.rows!,
}),
false,
);
Expand Down Expand Up @@ -500,7 +500,7 @@ export const useGridRows = (
}

// The new rows have already been applied (most likely in the `'rowGroupsPreProcessingChange'` listener)
if (apiRef.current.rowsCache.rowsBeforePartialUpdates === props.rows) {
if (apiRef.current.unstable_caches.rows!.rowsBeforePartialUpdates === props.rows) {
return;
}

Expand All @@ -509,7 +509,7 @@ export const useGridRows = (
convertRowsPropToState({
rows: props.rows,
getRowId: props.getRowId,
prevCache: apiRef.current.rowsCache,
prevCache: apiRef.current.unstable_caches.rows!,
}),
false,
);
Expand Down
11 changes: 11 additions & 0 deletions packages/grid/x-data-grid/src/models/api/gridCoreApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as React from 'react';
import { GridEventPublisher, GridEventListener, GridEventsStr } from '../events';
import { EventManager, EventListenerOptions } from '../../utils/EventManager';
import { GridRowsInternalCache } from '../../hooks/features/rows/gridRowsState';

// TODO: Export and make it augmentable
interface Caches {
rows?: GridRowsInternalCache;
}

/**
* The core API interface that is available in the grid `apiRef`.
Expand Down Expand Up @@ -46,6 +52,11 @@ export interface GridCoreApi {
* @ignore - do not document
*/
unstable_eventManager: EventManager;
/**
* The caches used by hooks and state initializers.
* @ignore - do not document.
*/
unstable_caches: Caches;
/**
* Registers a handler for an event.
* @param {string} event The name of the event.
Expand Down
5 changes: 0 additions & 5 deletions packages/grid/x-data-grid/src/models/api/gridRowApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { GridRowsInternalCache } from '../../hooks/features/rows/gridRowsState';
import {
GridRowModel,
GridRowId,
Expand Down Expand Up @@ -33,10 +32,6 @@ export interface GridRowGroupChildrenGetterParams {
* The Row API interface that is available in the grid `apiRef`.
*/
export interface GridRowApi {
/**
* @ignore - do not document
*/
rowsCache: GridRowsInternalCache;
/**
* Gets the full set of rows as [[Map<GridRowId, GridRowModel>]].
* @returns {Map<GridRowId, GridRowModel>} The full set of rows.
Expand Down

0 comments on commit 37dbfcc

Please sign in to comment.