Skip to content

Commit

Permalink
perf: decrease number of calls to translate all extensions only once
Browse files Browse the repository at this point in the history
- this PR requires and follows Slickgrid-Universal ghiscoding/slickgrid-universal#1359
  • Loading branch information
ghiscoding committed Jan 24, 2024
1 parent 6b18b14 commit 21a5016
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1042,11 +1042,12 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
const refreshSpy = jest.spyOn(component, 'refreshGridData');

const mockData = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Jane', lastName: 'Smith' }];
jest.spyOn(mockDataView, 'getItems').mockReturnValue(mockData);
jest.spyOn(mockDataView, 'getItems').mockReturnValueOnce(mockData);
component.gridOptions = {
enablePagination: true,
presets: { pagination: { pageSize: 2, pageNumber: expectedPageNumber } }
};
component.paginationOptions = undefined;
component.paginationOptions = { pageSize: 2, pageNumber: 2, pageSizes: [2, 10, 25, 50], totalItems: 100 };

component.dataset = mockData;
Expand Down Expand Up @@ -1163,7 +1164,7 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
component.gridOptions.backendServiceApi!.internalPostProcess!({ data: { notUsers: { nodes: [{ firstName: 'John' }], totalCount: 2 } } } as GraphqlPaginatedResult);

expect(spy).not.toHaveBeenCalled();
// expect(component.dataset).toEqual([]);
expect(component.dataset).toEqual([]);
});

it('should invoke "updateFilters" method with filters returned from "getColumnFilters" of the Filter Service when there is no Presets defined', () => {
Expand Down Expand Up @@ -1949,8 +1950,7 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
expect(footerSpy).toHaveBeenCalledWith(expectation);
});

it.skip('should have custom footer with metrics when the DataView "onSetItemsCalled" event is triggered', () => {
const invalidateSpy = jest.spyOn(mockGrid, 'invalidate');
it('should have custom footer with metrics when the DataView "onSetItemsCalled" event is triggered', () => {
const expectation = {
startTime: expect.toBeDate(),
endTime: expect.toBeDate(),
Expand All @@ -1964,7 +1964,6 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
const footerSpy = jest.spyOn(component.slickFooter!, 'metrics', 'set');
mockDataView.onSetItemsCalled.notify({ idProperty: 'id', itemCount: 0 });

expect(invalidateSpy).toHaveBeenCalled();
expect(footerSpy).toHaveBeenCalledWith(expectation);
expect(component.metrics).toEqual(expectation);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ import { ContainerService } from '../services/container.service';
]
})
export class AngularSlickgridComponent<TData = any> implements AfterViewInit, OnDestroy {
protected _dataset?: any[] | null;
protected _dataset?: TData[] | null;
protected _columnDefinitions!: Column[];
protected _currentDatasetLength = 0;
protected _eventHandler: SlickEventHandler = new SlickEventHandler();
Expand Down Expand Up @@ -358,7 +358,7 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
this._isGridInitialized = true;

// recheck the empty warning message after grid is shown so that it works in every use case
if (this.gridOptions && this.gridOptions.enableEmptyDataWarningMessage && Array.isArray(this.dataset)) {
if (this.gridOptions?.enableEmptyDataWarningMessage && Array.isArray(this.dataset)) {
const finalTotalCount = this.dataset.length;
this.displayEmptyDataWarning(finalTotalCount < 1);
}
Expand Down Expand Up @@ -789,7 +789,7 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
newColumnDefinitions = this.swapInternalEditorToSlickGridFactoryEditor(newColumnDefinitions);

if (this.gridOptions.enableTranslate) {
this.extensionService.translateColumnHeaders(false, newColumnDefinitions);
this.extensionService.translateColumnHeaders(undefined, newColumnDefinitions);
} else {
this.extensionService.renderColumnHeaders(newColumnDefinitions, true);
}
Expand Down Expand Up @@ -835,19 +835,15 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
// translate some of them on first load, then on each language change
if (gridOptions.enableTranslate) {
this.extensionService.translateAllExtensions();
this.translateColumnHeaderTitleKeys();
this.translateColumnGroupKeys();
}

this.subscriptions.push(
this.translate.onLangChange.subscribe(() => {
this.translate.onLangChange.subscribe(({ lang }) => {
// publish event of the same name that Slickgrid-Universal uses on a language change event
this._eventPubSubService.publish('onLanguageChange');

if (gridOptions.enableTranslate) {
this.extensionService.translateAllExtensions();
this.translateColumnHeaderTitleKeys();
this.translateColumnGroupKeys();
this.extensionService.translateAllExtensions(lang);
if (gridOptions.createPreHeaderPanel && !gridOptions.enableDraggableGrouping) {
this.groupingService.translateGroupingAndColSpan();
}
Expand Down Expand Up @@ -1407,16 +1403,6 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
});
}

protected translateColumnHeaderTitleKeys() {
// translate all columns (including hidden columns)
this.extensionUtility.translateItems(this.sharedService.allColumns, 'nameKey', 'name');
}

protected translateColumnGroupKeys() {
// translate all column groups (including hidden columns)
this.extensionUtility.translateItems(this.sharedService.allColumns, 'columnGroupKey', 'columnGroup');
}

/**
* Update the "internalColumnEditor.collection" property.
* 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 21a5016

Please sign in to comment.