Skip to content

Commit

Permalink
[DataGrid] Fix memory leak on grid unmount (#6579)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherniavskii committed Oct 24, 2022
1 parent f9046a0 commit 93a58b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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;
}
};

0 comments on commit 93a58b7

Please sign in to comment.