import { ObjectUtil } from "ts-object-utils" ; /***
 * You can create a new instance or inherit and override default methods
 * Allows access to data and size. Clone with rows creates a new data provider and let listview know where to calculate row layout from.
 */ export abstract class BaseDataProvider { public rowHasChanged : ( r1 : any , r2 : any ) => boolean ; public getStableId : ( index : number ) => string ; private _firstIndexToProcess : number = 0 ; private _size : number = 0 ; private _data : any [ ] = [ ] ; private _hasStableIds = false ; private _requiresDataChangeHandling = false ; constructor ( rowHasChanged , getStableId ? ) { this . rowHasChanged = rowHasChanged ; if ( getStableId ) { this . getStableId = getStableId ; this . _hasStableIds = true ; } else { this . getStableId = ( index ) => index . toString ( ) ; } } public abstract newInstance ( rowHasChanged , getStableId ? ) ; public getDataForIndex ( index ) { return this . _data [ index ] ; } public getAllData ( ) { return this . _data ; } public getSize ( ) { return this . _size ; } public hasStableIds ( ) { return this . _hasStableIds ; } public requiresDataChangeHandling ( ) { return this . _requiresDataChangeHandling ; } public getFirstIndexToProcessInternal ( ) { return this . _firstIndexToProcess ; } public cloneWithRows ( newData , firstModifiedIndex ? ) { const dp = this . newInstance ( this . rowHasChanged , this . getStableId ) ; const newSize = newData . length ; const iterCount = Math . min ( this . _size , newSize ) ; if ( ObjectUtil . isNullOrUndefined ( firstModifiedIndex ) ) { let i = 0 ; for ( i = 0 ; i < iterCount ; i ++ ) { if ( this . rowHasChanged ( this . _data [ i ] , newData [ i ] ) ) { break ; } } dp . _firstIndexToProcess = i ; } else { dp . _firstIndexToProcess = Math . max ( Math . min ( firstModifiedIndex , this . _data . length ) , 0 ) ; } if ( dp . _firstIndexToProcess !== this . _data . length ) { dp . _requiresDataChangeHandling = true ; } dp . _data = newData ; dp . _size = newSize ; return dp ; } } export default class DataProvider extends BaseDataProvider { public newInstance ( rowHasChanged , getStableId ? ) { return new DataProvider ( rowHasChanged , getStableId ) ; } }