Skip to content

Commit

Permalink
[DataGrid] Reduce dependency on lodash, save 1kB gzipped (#450)
Browse files Browse the repository at this point in the history

* review
  • Loading branch information
oliviertassinari authored Oct 20, 2020
1 parent c8245c0 commit 1a3bf5f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 212 deletions.
7 changes: 3 additions & 4 deletions packages/grid/_modules_/grid/GridComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { useForkRef } from '@material-ui/core/utils';
import { debounce, useForkRef } from '@material-ui/core/utils';
import { GridComponentProps } from './GridComponentProps';
import {
useApiRef,
Expand Down Expand Up @@ -30,7 +30,6 @@ import {
} from './components';
import { useApi, useColumns, useKeyboard, useRows } from './hooks/root';
import { useLogger, useLoggerFactory } from './hooks/utils';
import { debounce } from './utils';
import { useEvents } from './hooks/root/useEvents';
import { ErrorBoundary } from './components/error-boundary';
import { useOptionsProp } from './hooks/utils/useOptionsProp';
Expand Down Expand Up @@ -148,12 +147,12 @@ export const GridComponent = React.forwardRef<HTMLDivElement, GridComponentProps
},
[gridLogger, apiRef],
);
const debouncedOnResize = React.useMemo(() => debounce(onResize, 100), [onResize]) as any;
const debouncedOnResize = React.useMemo(() => debounce(onResize, 100), [onResize]);

React.useEffect(() => {
return () => {
gridLogger.info('canceling resize...');
debouncedOnResize.cancel();
debouncedOnResize.clear();
};
}, [gridLogger, debouncedOnResize]);

Expand Down
6 changes: 3 additions & 3 deletions packages/grid/_modules_/grid/hooks/utils/useScrollFn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { debounce } from '@material-ui/core/utils';
import { useLogger } from './useLogger';
import { debounce } from '../../utils/utils';
import { useRafUpdate } from './useRafUpdate';

export interface ScrollParams {
Expand All @@ -25,7 +25,7 @@ export function useScrollFn(

const debouncedResetPointerEvents = React.useMemo(() => debounce(restorePointerEvents, 300), [
restorePointerEvents,
]) as any;
]);

const scrollTo: (v: ScrollParams) => void = React.useCallback(
(v) => {
Expand Down Expand Up @@ -54,7 +54,7 @@ export function useScrollFn(

React.useEffect(() => {
return () => {
debouncedResetPointerEvents.cancel();
debouncedResetPointerEvents.clear();
};
}, [scrollingElementRef, debouncedResetPointerEvents]);

Expand Down
200 changes: 0 additions & 200 deletions packages/grid/_modules_/grid/lib/lodash/debounce.js

This file was deleted.

10 changes: 5 additions & 5 deletions packages/grid/_modules_/grid/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _debounce from '../lib/lodash/debounce';
import isEqual from '../lib/lodash/isEqual';

export { isEqual };
Expand All @@ -8,25 +7,26 @@ export interface DebouncedFunction extends Function {
flush: () => void;
}

export function debounce(func: any, wait?: number, options?: any): DebouncedFunction {
return _debounce(func, wait, options) as DebouncedFunction;
}

export function isDate(value: any): value is Date {
return value instanceof Date;
}

export function isArray(value: any): value is Array<any> {
return Array.isArray(value);
}

export function isString(value: any): value is string {
return typeof value === 'string';
}

export function isNumber(value: any): value is number {
return typeof value === 'number';
}

export function isFunction(value: any): value is Function {
return typeof value === 'function';
}

export function isObject(value: any): value is Record<string, any> {
return typeof value === 'object';
}

0 comments on commit 1a3bf5f

Please sign in to comment.