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

[DataGrid] Fix memory leak on grid unmount #6579

Merged
merged 1 commit into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4,6 +4,7 @@ import { GridSignature } from '../utils/useGridApiEventHandler';
import { DataGridProcessedProps } from '../../models/props/DataGridProps';
import { GridApiCommon, GridCoreApi } from '../../models';
import { EventManager } from '../../utils/EventManager';
import { unstable_resetCreateSelectorCache } from '../../utils/createSelector';

const isSyntheticEvent = (event: any): event is React.SyntheticEvent => {
return event.isPropagationStopped !== undefined;
Expand Down Expand Up @@ -68,6 +69,7 @@ export function useGridApiInitialization<Api extends GridApiCommon>(
const api = apiRef.current;

return () => {
unstable_resetCreateSelectorCache(api.instanceId);
api.publishEvent('unmount');
};
}, [apiRef]);
Expand Down
14 changes: 11 additions & 3 deletions packages/grid/x-data-grid/src/utils/createSelector.ts
Expand Up @@ -2,8 +2,10 @@ import * as React from 'react';
import { createSelector as reselectCreateSelector, Selector, SelectorResultArray } from 'reselect';
import { buildWarning } from './warning';

type CacheKey = number | string;

interface CacheContainer {
cache: Record<number | string, Map<any[], any>> | null;
cache: Record<CacheKey, Map<any[], any>> | null;
}

export interface OutputSelector<State, Result> {
Expand Down Expand Up @@ -92,6 +94,12 @@ export const createSelector: CreateSelectorFunction = (...args: any) => {
};

// eslint-disable-next-line @typescript-eslint/naming-convention
export const unstable_resetCreateSelectorCache = () => {
cacheContainer.cache = null;
export const unstable_resetCreateSelectorCache = (cacheKey?: CacheKey) => {
if (typeof cacheKey !== 'undefined') {
if (cacheContainer.cache && cacheContainer.cache[cacheKey]) {
delete cacheContainer.cache[cacheKey];
}
} else {
cacheContainer.cache = null;
}
};