Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Simplify rows cache management #4933

Merged
merged 6 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function useGridApiInitialization<Api extends GridApiCommon>(
if (!apiRef.current) {
apiRef.current = {
unstable_eventManager: new EventManager(),
unstable_caches: {},
unstable_caches: {} as Api['unstable_caches'],
state: {} as Api['state'],
instanceId: globalId,
} as Api;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ export interface GridRowTreeCreationValue {
idToIdLookup: Record<string, GridRowId>;
}

export type GridRowInternalCacheValue = Omit<GridRowTreeCreationParams, 'previousTree'>;

export interface GridRowsInternalCache {
value: GridRowInternalCacheValue;
export interface GridRowsInternalCache extends Omit<GridRowTreeCreationParams, 'previousTree'> {
/**
* The rows as they were the last time all the rows have been updated at once
* It is used to avoid processing several time the same set of rows
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { GridRowId, GridRowModel, GridRowTreeConfig } from '../../../models';
import * as React from 'react';
import {
GridRowId,
GridRowIdGetter,
GridRowModel,
GridRowsProp,
GridRowTreeConfig,
} from '../../../models';
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import { GridApiCommunity } from '../../../models/api/gridApiCommunity';
import { GridRowsInternalCache, GridRowsState } from './gridRowsState';

/**
* A helper function to check if the id provided is valid.
Expand All @@ -23,6 +33,82 @@ export function checkGridRowIdIsValid(
}
}

export const getRowIdFromRowModel = (
rowModel: GridRowModel,
getRowId?: GridRowIdGetter,
detailErrorMessage?: string,
): GridRowId => {
const id = getRowId ? getRowId(rowModel) : rowModel.id;
checkGridRowIdIsValid(id, rowModel, detailErrorMessage);
return id;
};

export const createRowsInternalCache = ({
rows,
getRowId,
loadingProp,
}: {
getRowId: DataGridProcessedProps['getRowId'];
loadingProp: DataGridProcessedProps['loading'];
rows: GridRowsProp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of making explicit that they are props? Pick could also be used.

Suggested change
getRowId: DataGridProcessedProps['getRowId'];
loadingProp: DataGridProcessedProps['loading'];
rows: GridRowsProp;
getRowIdProp: DataGridProcessedProps['getRowId'];
loadingProp: DataGridProcessedProps['loading'];
rowsProp: DataGridProcessedProps['rows'];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}): GridRowsInternalCache => {
const cache: GridRowsInternalCache = {
rowsBeforePartialUpdates: rows,
loadingPropBeforePartialUpdates: loadingProp,
idRowsLookup: {},
idToIdLookup: {},
ids: [],
};

for (let i = 0; i < rows.length; i += 1) {
const row = rows[i];
const id = getRowIdFromRowModel(row, getRowId);
cache.idRowsLookup[id] = row;
cache.idToIdLookup[id] = id;
cache.ids.push(id);
}

return cache;
};

export const getRowsStateFromCache = ({
apiRef,
previousTree,
rowCountProp,
loadingProp,
}: {
apiRef: React.MutableRefObject<GridApiCommunity>;
previousTree: GridRowTreeConfig | null;
rowCountProp: number | undefined;
loadingProp: boolean | undefined;
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
}): GridRowsState => {
const { rowsBeforePartialUpdates, ...cacheForGrouping } = apiRef.current.unstable_caches.rows;
const rowCount = rowCountProp ?? 0;

const groupingResponse = apiRef.current.unstable_applyStrategyProcessor('rowTreeCreation', {
...cacheForGrouping,
previousTree,
});

const processedGroupingResponse = apiRef.current.unstable_applyPipeProcessors(
'hydrateRows',
groupingResponse,
);

const dataTopLevelRowCount =
processedGroupingResponse.treeDepth === 1
? processedGroupingResponse.ids.length
: Object.values(processedGroupingResponse.tree).filter((node) => node.parent == null).length;

return {
...processedGroupingResponse,
groupingResponseBeforeRowHydration: groupingResponse,
loading: loadingProp,
totalRowCount: Math.max(rowCount, processedGroupingResponse.ids.length),
totalTopLevelRowCount: Math.max(rowCount, dataTopLevelRowCount),
};
};

export const getTreeNodeDescendants = (
tree: GridRowTreeConfig,
parentId: GridRowId,
Expand Down
Loading