Skip to content

Commit 22b6915

Browse files
committed
refactor(grids): extract refresh-options util and cast DataController getters
1 parent bb5d8e5 commit 22b6915

4 files changed

Lines changed: 47 additions & 18 deletions

File tree

packages/devextreme/js/__internal/grids/grid_core/data_controller/data_controller.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import type {
6666
UserState,
6767
} from './types';
6868
import { resolvePaginate, syncPaging } from './utils/paging';
69+
import { getRefreshOptions } from './utils/refresh';
6970
import { generateRowValues } from './utils/row_values';
7071

7172
export class DataController extends DataHelperMixin(modules.Controller) {
@@ -1658,7 +1659,7 @@ export class DataController extends DataHelperMixin(modules.Controller) {
16581659
}
16591660

16601661
public isCustomLoading(): boolean {
1661-
return !!(this._isCustomLoading || this._dataSource?.isCustomLoading());
1662+
return this._isCustomLoading || !!this._dataSource?.isCustomLoading();
16621663
}
16631664

16641665
public beginCustomLoading(messageText?: string): void {
@@ -1677,12 +1678,7 @@ export class DataController extends DataHelperMixin(modules.Controller) {
16771678
* @extended: virtual_scrolling, selection
16781679
*/
16791680
public refresh(options?: boolean | RefreshOptions): DeferredObj<unknown> {
1680-
let refreshOptions: RefreshOptions = { reload: true, lookup: true };
1681-
if (options === true) {
1682-
refreshOptions = { reload: true, changesOnly: true };
1683-
} else if (options) {
1684-
refreshOptions = options;
1685-
}
1681+
const refreshOptions = getRefreshOptions(options);
16861682

16871683
const dataSource = this.getDataSource();
16881684
const { changesOnly } = refreshOptions;
@@ -1736,7 +1732,7 @@ export class DataController extends DataHelperMixin(modules.Controller) {
17361732
/**
17371733
* @extended editing
17381734
*/
1739-
public repaintRows(rowIndexes: number | (number | undefined)[], changesOnly?: boolean): void {
1735+
public repaintRows(rowIndexes: number | (number | undefined)[] | undefined, changesOnly?: boolean): void {
17401736
const rowIndices = Array.isArray(rowIndexes) ? rowIndexes : [rowIndexes];
17411737

17421738
if (rowIndices.length > 1 || isDefined(rowIndices[0])) {
@@ -1763,8 +1759,8 @@ export class DataController extends DataHelperMixin(modules.Controller) {
17631759
};
17641760
}
17651761

1766-
public getCachedStoreData(): unknown {
1767-
return this._dataSource?.getCachedStoreData();
1762+
public getCachedStoreData(): RawItemData[] | undefined {
1763+
return this._dataSource?.getCachedStoreData() as RawItemData[] | undefined;
17681764
}
17691765

17701766
/**
@@ -1777,42 +1773,42 @@ export class DataController extends DataHelperMixin(modules.Controller) {
17771773
}
17781774

17791775
public load(): DeferredObj<unknown> {
1780-
return this._dataSource?.load();
1776+
return this._dataSource?.load() as DeferredObj<unknown>;
17811777
}
17821778

17831779
/**
17841780
* @extended: editing, virtual_scrolling
17851781
*/
17861782

17871783
public reload(reload?: boolean, changesOnly?: boolean): DeferredObj<unknown> {
1788-
return this._dataSource?.reload(reload, changesOnly);
1784+
return this._dataSource?.reload(reload, changesOnly) as DeferredObj<unknown>;
17891785
}
17901786

17911787
public push(...args: unknown[]): unknown {
17921788
return this._dataSource?.push(...args);
17931789
}
17941790

17951791
private itemsCount(): number {
1796-
return this._dataSource ? this._dataSource.itemsCount() : 0;
1792+
return (this._dataSource ? this._dataSource.itemsCount() : 0) as number;
17971793
}
17981794

17991795
public totalItemsCount(): number {
1800-
return this._dataSource ? this._dataSource.totalItemsCount() : 0;
1796+
return (this._dataSource ? this._dataSource.totalItemsCount() : 0) as number;
18011797
}
18021798

18031799
public hasKnownLastPage(): boolean {
1804-
return this._dataSource ? this._dataSource.hasKnownLastPage() : true;
1800+
return (this._dataSource ? this._dataSource.hasKnownLastPage() : true) as boolean;
18051801
}
18061802

18071803
/**
18081804
* @extended: state_storing
18091805
*/
18101806
public isLoaded(): boolean {
1811-
return this._dataSource ? this._dataSource.isLoaded() : true;
1807+
return (this._dataSource ? this._dataSource.isLoaded() : true) as boolean;
18121808
}
18131809

18141810
public totalCount(): number {
1815-
return this._dataSource ? this._dataSource.totalCount() : 0;
1811+
return (this._dataSource ? this._dataSource.totalCount() : 0) as number;
18161812
}
18171813

18181814
public hasLoadOperation(): boolean {

packages/devextreme/js/__internal/grids/grid_core/data_controller/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface RefreshOptions {
2222
}
2323

2424
export interface UserState {
25-
searchText: unknown;
25+
searchText: string | undefined;
2626
pageIndex: number;
2727
pageSize: number;
2828
expandedRowKeys?: unknown;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
3+
import { getRefreshOptions } from '../refresh';
4+
5+
describe('getRefreshOptions', () => {
6+
it('should reload with changes only when options is true', () => {
7+
expect(getRefreshOptions(true)).toEqual({ reload: true, changesOnly: true });
8+
});
9+
10+
it('should reload with lookup when options is falsy', () => {
11+
expect(getRefreshOptions()).toEqual({ reload: true, lookup: true });
12+
expect(getRefreshOptions(false)).toEqual({ reload: true, lookup: true });
13+
});
14+
15+
it('should return the passed options object as is', () => {
16+
const options = { load: true, changesOnly: true };
17+
18+
expect(getRefreshOptions(options)).toBe(options);
19+
});
20+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { RefreshOptions } from '../types';
2+
3+
export function getRefreshOptions(options?: boolean | RefreshOptions): RefreshOptions {
4+
if (options === true) {
5+
return { reload: true, changesOnly: true };
6+
}
7+
8+
if (!options) {
9+
return { reload: true, lookup: true };
10+
}
11+
12+
return options;
13+
}

0 commit comments

Comments
 (0)