Skip to content

Commit

Permalink
Merge pull request #1701 from ghiscoding/chore/warn-no-preparse-dates
Browse files Browse the repository at this point in the history
chore: move no pre-parse date warning to vanilla grid comp
  • Loading branch information
ghiscoding committed Sep 29, 2024
2 parents ea40b74 + d448dc5 commit 7f0a446
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
28 changes: 0 additions & 28 deletions packages/common/src/services/__tests__/sort.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,34 +388,6 @@ describe('SortService', () => {
expect(spyOnLocalSort).toHaveBeenCalledWith(gridStub, mockSortedCols);
});

it('should expect a console warning when dataset is larger than 5K items without pre-parsing enabled', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn').mockReturnValue();
vi.spyOn(dataViewStub, 'getLength').mockReturnValueOnce(5001);
const mockColumns: Column[] = [
{ id: 'firstName', field: 'firstName' },
{ id: 'updatedDate', field: 'updatedDate', type: FieldType.dateIso },
];
vi.spyOn(gridStub, 'getColumns').mockReturnValueOnce(mockColumns);

service.bindLocalOnSort(gridStub);

expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('[Slickgrid-Universal] For getting better perf, we suggest you enable the `preParseDateColumns` grid option'));
});

it('should expect a console warning when dataset is larger than 5K items without pre-parsing enabled', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn').mockReturnValue();
vi.spyOn(dataViewStub, 'getLength').mockReturnValueOnce(5001);
const mockColumns: Column[] = [
{ id: 'firstName', field: 'firstName' },
{ id: 'updatedDate', field: 'updatedDate', type: FieldType.dateIso },
];
vi.spyOn(gridStub, 'getColumns').mockReturnValueOnce(mockColumns);

service.bindLocalOnSort(gridStub);

expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('[Slickgrid-Universal] For getting better perf, we suggest you enable the `preParseDateColumns` grid option'));
});

it('should enable pre-parse and expect "preParseSingleDateItem()" being called when "grid.onCellChange" is called', async () => {
const mockColumns = [{ id: 'firstName', field: 'firstName' }, { id: 'updatedDate', field: 'updatedDate', type: FieldType.dateIso }] as Column[];
const mockData = [{ firstName: 'John', updatedDate: '2020-01-01' }, { firstName: 'Jane', updatedDate: '2020-02-02' }];
Expand Down
7 changes: 0 additions & 7 deletions packages/common/src/services/sort.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import type { SharedService } from './shared.service';
import type { RxJsFacade, Subject } from './rxjsFacade';
import { type SlickDataView, type SlickEventData, SlickEventHandler, type SlickGrid } from '../core/index';

const WARN_NO_PREPARSE_DATE_SIZE = 5000; // data size to warn user when pre-parse isn't enabled

export class SortService {
protected _currentLocalSorters: CurrentSorter[] = [];
protected _eventHandler: SlickEventHandler;
Expand Down Expand Up @@ -104,11 +102,6 @@ export class SortService {
if (this._gridOptions.preParseDateColumns) {
this._eventHandler.subscribe(grid.onCellChange, (_e, args) => this.preParseSingleDateItem(args.item));
this.pubSubService.subscribe(['onItemAdded', 'onItemUpdated'], (item) => this.preParseSingleDateItem(item));
} else if (this._dataView?.getLength() > WARN_NO_PREPARSE_DATE_SIZE && grid.getColumns().some(c => isColumnDateType(c.type))) {
console.warn(
'[Slickgrid-Universal] For getting better perf, we suggest you enable the `preParseDateColumns` grid option, ' +
'for more info visit:: https://ghiscoding.gitbook.io/slickgrid-universal/column-functionalities/sorting#pre-parse-date-columns-for-better-perf'
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type ExtensionList,
type ExtensionService,
type ExtensionUtility,
FieldType,
Filters,
type FilterService,
type Formatter,
Expand Down Expand Up @@ -521,6 +522,40 @@ describe('Slick-Vanilla-Grid-Bundle Component instantiated via Constructor', ()
expect(resizerSpy).toHaveBeenCalledWith();
});

it('should expect a console warning when grid is initialized with a dataset larger than 5K items without pre-parsing enabled', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn').mockReturnValue();
vi.spyOn(mockDataView, 'getItemCount').mockReturnValueOnce(5001);
const mockColumns: Column[] = [
{ id: 'firstName', field: 'firstName' },
{ id: 'updatedDate', field: 'updatedDate', type: FieldType.dateIso },
];
vi.spyOn(mockGrid, 'getColumns').mockReturnValueOnce(mockColumns);

component.gridOptions = { enableAutoResize: true };
component.initialization(divContainer, slickEventHandler);

expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('[Slickgrid-Universal] For getting better perf, we suggest you enable the `preParseDateColumns` grid option'));
});

it('should expect a console warning when assigned dataset is larger than 5K items without pre-parsing enabled', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn').mockReturnValue();
vi.spyOn(mockDataView, 'getItemCount').mockReturnValueOnce(0);
const mockColumns: Column[] = [
{ id: 'firstName', field: 'firstName' },
{ id: 'updatedDate', field: 'updatedDate', type: FieldType.dateIso },
];
vi.spyOn(mockGrid, 'getColumns').mockReturnValueOnce(mockColumns);

component.gridOptions = { enableAutoResize: true };
component.initialization(divContainer, slickEventHandler);

// we'll do a fake dataset assignment of 5001 items
vi.spyOn(mockDataView, 'getItemCount').mockReturnValueOnce(5001);
component.dataset = [{ firstName: 'John', updatedDate: '2020-02-01' }];

expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('[Slickgrid-Universal] For getting better perf, we suggest you enable the `preParseDateColumns` grid option'));
});

describe('autoAddCustomEditorFormatter grid option', () => {
it('should initialize the grid and automatically add custom Editor Formatter when provided in the grid options', () => {
component.gridOptions = { autoAddCustomEditorFormatter: customEditableInputFormatter };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {

// utilities
emptyElement,
isColumnDateType,
SlickEventHandler,
SlickDataView,
SlickGrid,
Expand All @@ -59,6 +60,8 @@ import { SlickPaginationComponent } from '@slickgrid-universal/pagination-compon
import { type SlickerGridInstance } from '../interfaces/slickerGridInstance.interface';
import { UniversalContainerService } from '../services/universalContainer.service';

const WARN_NO_PREPARSE_DATE_SIZE = 5000; // data size to warn user when pre-parse isn't enabled

export class SlickVanillaGridBundle<TData = any> {
protected _currentDatasetLength = 0;
protected _eventPubSubService!: EventPubSubService;
Expand Down Expand Up @@ -169,6 +172,8 @@ export class SlickVanillaGridBundle<TData = any> {
this.slickGrid.autosizeColumns();
this._isAutosizeColsCalled = true;
}

this.suggestDateParsingWhenHelpful();
}

get datasetHierarchical(): any[] | undefined {
Expand Down Expand Up @@ -704,6 +709,7 @@ export class SlickVanillaGridBundle<TData = any> {
// all instances (SlickGrid, DataView & all Services)
this._eventPubSubService.publish('onSlickerGridCreated', this.instances);
this._isGridInitialized = true;
this.suggestDateParsingWhenHelpful();
}

hasBackendInfiniteScroll(): boolean {
Expand Down Expand Up @@ -1533,6 +1539,15 @@ export class SlickVanillaGridBundle<TData = any> {
});
}

protected suggestDateParsingWhenHelpful(): void {
if (this.dataView && this.dataView.getItemCount() > WARN_NO_PREPARSE_DATE_SIZE && !this.gridOptions.preParseDateColumns && this.slickGrid?.getColumns().some(c => isColumnDateType(c.type))) {
console.warn(
'[Slickgrid-Universal] For getting better perf, we suggest you enable the `preParseDateColumns` grid option, ' +
'for more info visit:: https://ghiscoding.gitbook.io/slickgrid-universal/column-functionalities/sorting#pre-parse-date-columns-for-better-perf'
);
}
}

/**
* When the Editor(s) has a "editor.collection" property, we'll load the async collection.
* Since this is called after the async call resolves, the pointer will not be the same as the "column" argument passed.
Expand Down

0 comments on commit 7f0a446

Please sign in to comment.