1313/* eslint-disable max-depth */
1414/* eslint-disable no-param-reassign */
1515/* eslint-disable no-plusplus */
16- import type { DataSource } from '@js/common/data' ;
16+ import type { DataSource , Store } from '@js/common/data' ;
1717import $ from '@js/core/renderer' ;
1818import type { Callback } from '@js/core/utils/callbacks' ;
1919import { deferRender , equalByValue } from '@js/core/utils/common' ;
2020import type { DeferredObj } from '@js/core/utils/deferred' ;
2121import { Deferred , when } from '@js/core/utils/deferred' ;
22- import { extend } from '@js/core/utils/extend' ;
2322import { each } from '@js/core/utils/iterator' ;
2423import { isDefined } from '@js/core/utils/type' ;
2524import errors from '@js/ui/widget/ui.errors' ;
2625import { findChanges } from '@ts/core/utils/m_array_compare' ;
26+ import { fromPromise } from '@ts/core/utils/m_deferred' ;
27+ import type { StoreLoadOptions } from '@ts/data/data_source/types' ;
28+ import type { ColumnsChanges } from '@ts/grids/grid_core/columns_controller/types' ;
29+ import type {
30+ ChangedEvent , LoadOperation , OperationTypes , RawItemData ,
31+ } from '@ts/grids/grid_core/data_source_adapter/types' ;
32+ import { isLocalStore } from '@ts/grids/grid_core/data_source_adapter/utils/store' ;
2733import type { EditingController } from '@ts/grids/grid_core/editing/m_editing' ;
2834import type { EditorFactory } from '@ts/grids/grid_core/editor_factory/m_editor_factory' ;
2935import type { ErrorHandlingController } from '@ts/grids/grid_core/error_handling/m_error_handling' ;
@@ -32,18 +38,16 @@ import type { FilterSyncController } from '@ts/grids/grid_core/filter/m_filter_s
3238import type { FocusController } from '@ts/grids/grid_core/focus/m_focus' ;
3339import type { HeaderFilterController } from '@ts/grids/grid_core/header_filter/m_header_filter' ;
3440import type { KeyboardNavigationController } from '@ts/grids/grid_core/keyboard_navigation/m_keyboard_navigation' ;
41+ import modules from '@ts/grids/grid_core/m_modules' ;
42+ import type {
43+ Controllers , Module , OptionChanged , RowKey ,
44+ } from '@ts/grids/grid_core/m_types' ;
45+ import gridCoreUtils from '@ts/grids/grid_core/m_utils' ;
3546import type { SelectionController } from '@ts/grids/grid_core/selection/m_selection' ;
3647import type { StateStoringController } from '@ts/grids/grid_core/state_storing/m_state_storing_core' ;
3748import type { ValidatingController } from '@ts/grids/grid_core/validating/m_validating' ;
49+ import type { VirtualScrollController } from '@ts/grids/grid_core/virtual_scrolling/m_virtual_scrolling_core' ;
3850
39- import type { ColumnsChanges } from '../columns_controller/types' ;
40- import type { ChangedEvent , LoadOperation , OperationTypes } from '../data_source_adapter/types' ;
41- import modules from '../m_modules' ;
42- import type {
43- Controllers , Module , OptionChanged , RowKey ,
44- } from '../m_types' ;
45- import gridCoreUtils from '../m_utils' ;
46- import type { VirtualScrollController } from '../virtual_scrolling/m_virtual_scrolling_core' ;
4751import { DataHelperMixin } from './data_helper_mixin' ;
4852import type {
4953 BinaryDataFilterExpression ,
@@ -58,7 +62,6 @@ import type {
5862 PagingOptionName ,
5963 PagingResult ,
6064 ProcessedItem ,
61- RawItemData ,
6265} from './types' ;
6366import { resolvePaginate , syncPaging } from './utils/paging' ;
6467import { generateRowValues } from './utils/row_values' ;
@@ -262,7 +265,7 @@ export class DataController extends DataHelperMixin(modules.Controller) {
262265 const isValueChanged = args . value !== args . previousValue ;
263266 if ( isValueChanged ) {
264267 const store = this . store ( ) ;
265- if ( store ) {
268+ if ( isLocalStore ( store ) ) {
266269 store . _array = args . value ;
267270 }
268271 }
@@ -1454,31 +1457,29 @@ export class DataController extends DataHelperMixin(modules.Controller) {
14541457 /**
14551458 * @extended : virtual_scrolling
14561459 */
1457- public isEmpty ( ) {
1460+ public isEmpty ( ) : boolean {
14581461 return ! this . items ( ) . length ;
14591462 }
14601463
1461- public pageCount ( ) {
1464+ public pageCount ( ) : number {
14621465 return this . _dataSource ? this . _dataSource . pageCount ( ) : 1 ;
14631466 }
14641467
14651468 public dataSource ( ) {
14661469 return this . _dataSource ;
14671470 }
14681471
1469- public store ( ) {
1470- const dataSource = this . _dataSource ;
1471- return dataSource ?. store ( ) ;
1472+ public store ( ) : Store | undefined {
1473+ return this . _dataSource ?. store ( ) ;
14721474 }
14731475
1474- public loadAll ( data , skipFilter = false ) {
1475- // @ts -expect-error
1476- const d = new Deferred ( ) ;
1476+ public loadAll ( data ?: RawItemData [ ] , skipFilter = false ) : DeferredObj < ProcessedItem [ ] > {
1477+ const d = Deferred < ProcessedItem [ ] > ( ) ;
14771478 const dataSource = this . _dataSource ;
14781479
14791480 if ( dataSource ) {
14801481 if ( data ) {
1481- const options : Record < string , any > = {
1482+ const loadOperation : Omit < LoadOperation , 'data' > & Required < Pick < LoadOperation , 'data' > > = {
14821483 data,
14831484 isCustomLoading : true ,
14841485 storeLoadOptions : { isLoadingAll : true } ,
@@ -1488,94 +1489,104 @@ export class DataController extends DataHelperMixin(modules.Controller) {
14881489 sort : dataSource . sort ( ) ,
14891490 } ,
14901491 } ;
1491- dataSource . _handleDataLoaded ( options ) ;
1492- when ( options . data ) . done ( ( data ) => {
1493- data = this . _beforeProcessItems ( data ) ;
1494- d . resolve ( this . _processItems ( data , { changeType : 'loadingAll' } ) , options . extra ?. summary ) ;
1495- } ) . fail ( d . reject ) ;
1492+ dataSource . _handleDataLoaded ( loadOperation ) ;
1493+
1494+ when < RawItemData [ ] > ( loadOperation . data )
1495+ . done ( ( loadedData : RawItemData [ ] ) : void => {
1496+ const items = this . _processItems (
1497+ this . _beforeProcessItems ( loadedData ) ,
1498+ { changeType : 'loadingAll' } ,
1499+ ) ;
1500+ // @ts -expect-error DataGrid-only summary leaks into grid_core
1501+ d . resolve ( items , loadOperation . extra ?. summary ) ;
1502+ } )
1503+ . fail ( d . reject as ( ...args : unknown [ ] ) => void ) ;
14961504 } else if ( ! dataSource . isLoading ( ) ) {
1497- const loadOptions = extend ( { } , dataSource . loadOptions ( ) , { isLoadingAll : true , requireTotalCount : false } ) ;
1498- dataSource . load ( loadOptions ) . done ( ( items , extra ) => {
1499- items = this . _beforeProcessItems ( items ) ;
1500- items = this . _processItems ( items , { changeType : 'loadingAll' } ) ;
1501- d . resolve ( items , extra ?. summary ) ;
1502- } ) . fail ( d . reject ) ;
1505+ const loadOptions : StoreLoadOptions & { isLoadingAll : boolean } = {
1506+ ...dataSource . loadOptions ( ) ,
1507+ isLoadingAll : true ,
1508+ requireTotalCount : false ,
1509+ } ;
1510+ dataSource . load ( loadOptions )
1511+ . done ( ( loadedItems : RawItemData [ ] , extra : LoadOperation [ 'extra' ] ) : void => {
1512+ const items = this . _processItems (
1513+ this . _beforeProcessItems ( loadedItems ) ,
1514+ { changeType : 'loadingAll' } ,
1515+ ) ;
1516+ // @ts -expect-error DataGrid-only summary leaks into grid_core
1517+ d . resolve ( items , extra ?. summary ) ;
1518+ } )
1519+ . fail ( d . reject ) ;
15031520 } else {
15041521 d . reject ( ) ;
15051522 }
15061523 } else {
15071524 d . resolve ( [ ] ) ;
15081525 }
1526+
15091527 return d ;
15101528 }
15111529
1512- public getAllDataRowKeys ( ) : Promise < RowKey [ ] > {
1513- return Promise . resolve ( this . loadAll ( undefined ) as unknown as Promise < ProcessedItem [ ] > )
1514- . then ( ( items ) => items
1515- . filter ( ( item ) => item . rowType === 'data' )
1516- . map ( ( item ) => item . key ) ) ;
1530+ public async getAllDataRowKeys ( ) : Promise < RowKey [ ] > {
1531+ const items = await Promise . resolve ( this . loadAll ( undefined ) ) ;
1532+
1533+ return items
1534+ . filter ( ( item ) => item . rowType === 'data' )
1535+ . map ( ( item ) : RowKey => item . key ) ;
15171536 }
15181537
1519- public getKeyByRowIndex ( rowIndex , byLoaded ?) {
1538+ public getKeyByRowIndex ( rowIndex : number , byLoaded ?: boolean ) : RowKey | undefined {
15201539 const item = this . items ( byLoaded ) [ rowIndex ] ;
1521- if ( item ) {
1522- return item . key ;
1523- }
1540+
1541+ return item ?. key ;
15241542 }
15251543
1526- public getRowIndexByKey ( key , byLoaded ?) {
1544+ public getRowIndexByKey ( key : RowKey , byLoaded ?: boolean ) : number {
15271545 return gridCoreUtils . getIndexByKey ( key , this . items ( byLoaded ) ) ;
15281546 }
15291547
1530- public getRowByKey ( key : unknown ) : ProcessedItem | undefined {
1548+ public getRowByKey ( key : RowKey ) : ProcessedItem | undefined {
15311549 return this . items ( ) ?. [ this . getRowIndexByKey ( key ) ] ;
15321550 }
15331551
1534- public keyOf ( data ) {
1535- const store = this . store ( ) ;
1536- if ( store ) {
1537- return store . keyOf ( data ) ;
1538- }
1552+ public keyOf ( data : RawItemData ) : RowKey | undefined {
1553+ return this . store ( ) ?. keyOf ( data ) ;
15391554 }
15401555
1541- private byKey ( key ) {
1556+ private byKey ( key : RowKey ) : DeferredObj < RawItemData > {
15421557 const store = this . store ( ) ;
1543- const rowIndex = this . getRowIndexByKey ( key ) ;
1544- let result ;
15451558
1546- if ( ! store ) return ;
1559+ if ( ! store ) {
1560+ return Deferred < RawItemData > ( ) . reject ( ) ;
1561+ }
1562+
1563+ const rowIndex = this . getRowIndexByKey ( key ) ;
15471564
15481565 if ( rowIndex >= 0 ) {
1549- // @ts -expect-error
1550- result = new Deferred ( ) . resolve ( this . items ( ) [ rowIndex ] . data ) ;
1566+ return Deferred < RawItemData > ( ) . resolve ( this . items ( ) [ rowIndex ] . data ) ;
15511567 }
15521568
1553- return result || store . byKey ( key ) ;
1569+ return fromPromise ( store . byKey ( key ) ) ;
15541570 }
15551571
1556- public key ( ) {
1557- const store = this . store ( ) ;
1558-
1559- if ( store ) {
1560- return store . key ( ) ;
1561- }
1572+ public key ( ) : string | string [ ] | undefined {
1573+ return this . store ( ) ?. key ( ) ;
15621574 }
15631575
15641576 /**
15651577 * @extended : virtual_scrolling
15661578 */
15671579 // eslint-disable-next-line @typescript-eslint/no-unused-vars
1568- public getRowIndexOffset ( byLoadedRows ?: boolean ) {
1580+ public getRowIndexOffset ( byLoadedRows ?: boolean ) : number {
15691581 return 0 ;
15701582 }
15711583
1572- private getDataByKeys ( rowKeys ) {
1573- // @ts -expect-error
1574- const result = new Deferred ( ) ;
1575- const deferreds : any [ ] = [ ] ;
1576- const data : any [ ] = [ ] ;
1584+ private getDataByKeys ( rowKeys : RowKey [ ] ) : DeferredObj < RawItemData [ ] > {
1585+ const result = Deferred < RawItemData [ ] > ( ) ;
1586+ const deferreds : DeferredObj < RawItemData > [ ] = [ ] ;
1587+ const data : RawItemData [ ] = [ ] ;
15771588
1578- each ( rowKeys , ( index , key ) => {
1589+ each ( rowKeys , ( index : number , key : RowKey ) => {
15791590 deferreds . push ( this . byKey ( key ) . done ( ( keyData ) => {
15801591 data [ index ] = keyData ;
15811592 } ) ) ;
0 commit comments