Skip to content

Commit

Permalink
refactor(ngrid): re-structure datasource code
Browse files Browse the repository at this point in the history
  • Loading branch information
shlomiassaf committed Dec 21, 2020
1 parent fc259ba commit 6194f0e
Show file tree
Hide file tree
Showing 24 changed files with 129 additions and 122 deletions.
1 change: 0 additions & 1 deletion libs/ngrid/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './lib/events/index';
export * from './lib/paginator/index';
export * from './lib/registry/index';
export * from './lib/utils/index';
export * from './lib/models/column';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Observable, Subject, combineLatest, of, from, isObservable, asapScheduler } from 'rxjs';
import { filter, map, switchMap, tap, debounceTime, observeOn } from 'rxjs/operators';

import { PblPaginator, PblPaginatorChangeEvent } from '../paginator/types';
import { PblNgridDataSourceSortChange, DataSourceFilter } from './types';
import { filter as filteringFn } from './filtering';
import { applySort } from './sorting';
import { PblPaginator, PblPaginatorChangeEvent } from '../triggers/pagination/types';
import { DataSourceFilter, filter as filteringFn } from '../triggers/filter';
import { PblNgridDataSourceSortChange, applySort } from '../triggers/sort';

import {
RefreshDataWrapper,
Expand All @@ -15,9 +14,8 @@ import {
TriggerChangedEventFor,
PblDataSourceAdapterProcessedResult,
PblDataSourceTriggerChangeHandler,
} from './data-source-adapter.types';

import { createChangeContainer, fromRefreshDataWrapper, EMPTY } from './data-source-adapter.helpers';
} from './types';
import { createChangeContainer, fromRefreshDataWrapper, EMPTY } from './utils';

const CUSTOM_BEHAVIOR_TRIGGER_KEYS: Array<keyof PblDataSourceConfigurableTriggers> = ['sort', 'filter', 'pagination'];
const TRIGGER_KEYS: Array<keyof PblDataSourceTriggers> = [...CUSTOM_BEHAVIOR_TRIGGER_KEYS, 'data'];
Expand All @@ -28,7 +26,9 @@ const DEFAULT_INITIAL_CACHE_STATE: PblDataSourceTriggerCache<any> = { filter: EM
/**
* An adapter that handles changes
*/
export class PblDataSourceAdapter<T = any, TData = any, TEvent extends PblDataSourceTriggerChangedEvent<TData> = PblDataSourceTriggerChangedEvent<TData>> {
export class PblDataSourceAdapter<T = any,
TData = any,
TEvent extends PblDataSourceTriggerChangedEvent<TData> = PblDataSourceTriggerChangedEvent<TData>> {

static hasCustomBehavior(config: Partial<Record<keyof PblDataSourceConfigurableTriggers, boolean>>): boolean {
for (const key of CUSTOM_BEHAVIOR_TRIGGER_KEYS) {
Expand All @@ -49,8 +49,8 @@ export class PblDataSourceAdapter<T = any, TData = any, TEvent extends PblDataSo
return false;
}

onSourceChanged: Observable<T[]>;
onSourceChanging: Observable<void>;
readonly onSourceChanged: Observable<T[]>;
readonly onSourceChanging: Observable<void>;

get inFlight() { return this._inPreFlight || this._inFlight.size > 0; }

Expand Down Expand Up @@ -110,7 +110,11 @@ export class PblDataSourceAdapter<T = any, TData = any, TEvent extends PblDataSo
constructor(public sourceFactory: PblDataSourceTriggerChangeHandler<T, TEvent>,
config?: false | Partial<Record<keyof PblDataSourceConfigurableTriggers, boolean>>) {
this.config = Object.assign({}, config || {});
this.initStreams();

this._refresh$ = new Subject<RefreshDataWrapper<TData>>();
this._onSourceChange$ = new Subject<T[]>();
this.onSourceChanged = this._onSourceChange$.pipe(filter( d => d !== SOURCE_CHANGING_TOKEN ));
this.onSourceChanging = this._onSourceChange$.pipe(filter( d => d === SOURCE_CHANGING_TOKEN ));
}

dispose(): void {
Expand Down Expand Up @@ -394,17 +398,4 @@ export class PblDataSourceAdapter<T = any, TData = any, TEvent extends PblDataSo
tap( data => this.emitOnSourceChanged(event, data as T[]) ),
);
}

/* Note: Currently this is only used in the constructor.
However, if called elsewhere (i.e. if we can re-init the adapter) we need to track all code that is using
`onSourceChanged` and or `onSourceChanging` and make it support the replacement of the observable.
Because the API is public it will probably won't work so the best solution might be to switch
`onSourceChanged` and `onSourceChanging` to subjects that are alive always and emit them internally in this class. */
private initStreams(): void {
this._onSourceChange$ = new Subject<T[]>();
this.onSourceChanged = this._onSourceChange$.pipe(filter( d => d !== SOURCE_CHANGING_TOKEN ));
this.onSourceChanging = this._onSourceChange$.pipe(filter( d => d === SOURCE_CHANGING_TOKEN ));
this._refresh$ = new Subject<RefreshDataWrapper<TData>>();
this._lastSource = undefined;
}
}
2 changes: 2 additions & 0 deletions libs/ngrid/core/src/lib/data-source/adapter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export { PblDataSourceAdapter } from './adapter';
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PblPaginatorChangeEvent } from '../paginator/types';
import { DataSourceOf } from './data-source';
import { PblNgridDataSourceSortChange, DataSourceFilter } from './types';
import { DataSourceOf } from '../types';
import { PblPaginatorChangeEvent } from '../triggers/pagination/types';
import { DataSourceFilter } from '../triggers/filter/types';
import { PblNgridDataSourceSortChange } from '../triggers/sort/types';

/** @internal */
export type RefreshDataWrapper<T> = { data: T };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PblNgridDataSourceSortChange, DataSourceFilter } from './types';
import { DataSourceFilter } from '../triggers/filter/types';
import { PblNgridDataSourceSortChange } from '../triggers/sort/types';

import {
RefreshDataWrapper,
Expand All @@ -7,7 +8,7 @@ import {
PblDataSourceTriggerCache,
PblDataSourceTriggerChangedEvent,
TriggerChangedEventFor,
} from './data-source-adapter.types';
} from './types';

export const EMPTY: any = Object.freeze({});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { PblDataSource, PblDataSourceOptions } from './data-source';
import { PblDataSourceAdapter } from './data-source-adapter';
import {
PblDataSourceConfigurableTriggers,
PblDataSourceTriggerChangedEvent,
PblDataSourceTriggerChangeHandler,
} from './data-source-adapter.types';
import { PblDataSource, PblDataSourceOptions } from '../data-source';
import { PblDataSourceAdapter } from '../adapter/adapter';
import { PblDataSourceConfigurableTriggers, PblDataSourceTriggerChangedEvent, PblDataSourceTriggerChangeHandler } from '../adapter/types';

interface AdapterParams<T, TEvent extends PblDataSourceTriggerChangedEvent<any> = PblDataSourceTriggerChangedEvent<any>> {
onTrigger?: PblDataSourceTriggerChangeHandler<T, TEvent>;
Expand Down
13 changes: 4 additions & 9 deletions libs/ngrid/core/src/lib/data-source/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ import { DataSource } from '@angular/cdk/table';
import { moveItemInArray } from '@angular/cdk/drag-drop';

import { unrx } from '../utils/unrx';
import { PblNgridPaginatorKind, PblPaginator } from '../paginator/types'
import { PblPagingPaginator } from '../paginator/paging-paginator'
import { PblTokenPaginator } from '../paginator/token-paginator'
import { PblNgridEventEmitter } from '../events/events';
import { PblColumnDefinition } from '../models/column';
import { DataSourcePredicate, DataSourceFilter, DataSourceFilterToken, PblNgridSortDefinition, PblNgridDataSourceSortChange } from './types';
import { createFilter } from './filtering';
import { PblDataSourceAdapter } from './data-source-adapter';
import { PblDataSourceTriggerCache, PblDataSourceTriggerChangedEvent, PblDataSourceAdapterProcessedResult } from './data-source-adapter.types';

export type DataSourceOf<T> = T[] | Promise<T[]> | Observable<T[]>;
import { PblNgridPaginatorKind, PblPaginator, PblPagingPaginator, PblTokenPaginator } from './triggers/pagination'
import { DataSourcePredicate, DataSourceFilter, DataSourceFilterToken, createFilter } from './triggers/filter'
import { PblNgridSortDefinition, PblNgridDataSourceSortChange } from './triggers/sort'
import { PblDataSourceAdapter, PblDataSourceTriggerCache, PblDataSourceTriggerChangedEvent, PblDataSourceAdapterProcessedResult } from './adapter';

const PROCESSING_SUBSCRIPTION_GROUP = {};

Expand Down
4 changes: 2 additions & 2 deletions libs/ngrid/core/src/lib/data-source/factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PblDataSource } from './data-source';
import { PblDataSourceAdapter } from './data-source-adapter';
import { PblDataSourceBaseFactory } from './data-source-base-factory';
import { PblDataSourceAdapter } from './adapter/adapter';
import { PblDataSourceBaseFactory } from './base/factory';

export class PblDataSourceFactory<T, TData = any> extends PblDataSourceBaseFactory<T, TData> {
protected createAdapter(): PblDataSourceAdapter<T, TData> {
Expand Down
45 changes: 30 additions & 15 deletions libs/ngrid/core/src/lib/data-source/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
export * from './events';

export {
PblDataSourceConfigurableTriggers,
PblDataSourceTriggers,
PblDataSourceTriggerChange,
PblDataSourceTriggerChangedEvent,
PblDataSourceTriggerChangedEventSource,
PblDataSourceAdapterProcessedResult,
PblDataSourceTriggerChangeHandler,
} from './data-source-adapter.types';
export { PblDataSourceAdapter } from './data-source-adapter';
PblNgridPaginatorKind,
PblPaginator,
PblPaginatorChangeEvent,
PblPagingPaginator,
PblTokenPaginator,
} from './triggers/pagination';

export {
PblNgridSortInstructions,
PblNgridSortDefinition,
DataSourceFilterToken,
DataSourcePredicate,
DataSourceColumnPredicate,
} from './triggers/filter';

export {
PblNgridSortInstructions,
PblNgridSortDefinition,
PblNgridSorter,
PblNgridDataSourceSortChange,
PblNgridSortOrder,
} from './types';
export { PblDataSource, PblDataSourceOptions, DataSourceOf } from './data-source';
export { PblDataSourceBaseFactory } from './data-source-base-factory';
applySort
} from './triggers/sort';

export {
PblDataSourceConfigurableTriggers,
PblDataSourceTriggers,
PblDataSourceTriggerChange,
PblDataSourceTriggerChangedEvent,
PblDataSourceTriggerChangedEventSource,
PblDataSourceAdapterProcessedResult,
PblDataSourceTriggerChangeHandler,
PblDataSourceAdapter,
} from './adapter/index';

export { DataSourceOf } from './types';
export { PblDataSource, PblDataSourceOptions } from './data-source';
export { PblDataSourceBaseFactory } from './base/factory';
export { PblDataSourceFactory, createDS } from './factory';
export { applySort, } from './sorting';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PblColumnDefinition } from '../models/column';
import { getValue } from '../utils/column';
import { PblColumnDefinition } from '../../../models/column';
import { getValue } from '../../../utils/column';
import { DataSourceFilter, DataSourceFilterToken, DataSourcePredicate, DataSourceColumnPredicate } from './types';

export function createFilter(value: DataSourceFilterToken, columns: PblColumnDefinition[]): DataSourceFilter {
Expand Down
2 changes: 2 additions & 0 deletions libs/ngrid/core/src/lib/data-source/triggers/filter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export * from './filter';
25 changes: 25 additions & 0 deletions libs/ngrid/core/src/lib/data-source/triggers/filter/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { PblColumnDefinition } from '../../../models/column';

/**
* A function the return true then the value should be included in the result or false when not.
* This is a single column filter predicated, returning false will filter out the entire row but the
* predicate is only intended to filter a specific column.
*/
export type DataSourceColumnPredicate = (filterValue: any, colValue: any, row?: any, col?: PblColumnDefinition) => boolean;

/**
* A function the return true then the row should be included in the result or false when not.
* @param row The row in the data source that the filter apply on
* @param properties A list of column instances (`PblColumnDefinition`) to filter values by.
*/
export type DataSourcePredicate = (row: any, properties: PblColumnDefinition[]) => boolean;

export type DataSourceFilterToken = undefined | DataSourcePredicate | any;

export interface DataSourceFilterType {
type: 'value' | 'predicate';
columns: PblColumnDefinition[];
filter: any | DataSourcePredicate;
}

export type DataSourceFilter = undefined | DataSourceFilterType ;
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions libs/ngrid/core/src/lib/data-source/triggers/sort/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export * from './sort';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PblColumnDefinition } from '../models/column';
import { getValue } from '../utils/column';
import { PblColumnDefinition } from '../../../models/column';
import { getValue } from '../../../utils/column';
import { PblNgridSortDefinition, PblNgridSortInstructions, PblNgridSorter } from './types';

/**
Expand Down
26 changes: 26 additions & 0 deletions libs/ngrid/core/src/lib/data-source/triggers/sort/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PblColumnDefinition } from '../../../models/column';

export type PblNgridSortOrder = 'asc' | 'desc';

export interface PblNgridSortInstructions {
order?: PblNgridSortOrder;
}

/**
* Event fired when sort changes.
*/
export interface PblNgridSortDefinition extends PblNgridSortInstructions {
sortFn?: PblNgridSorter;
}

/**
* A function that can sort a dataset based on `PblNgridSortInstructions`
*/
export interface PblNgridSorter<T = any> {
(column: PblColumnDefinition, sort: PblNgridSortInstructions, data: T[]): T[];
}

export interface PblNgridDataSourceSortChange {
column: PblColumnDefinition;
sort: PblNgridSortDefinition;
}
52 changes: 2 additions & 50 deletions libs/ngrid/core/src/lib/data-source/types.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,3 @@
import { PblColumnDefinition } from '../models/column';
import { Observable } from 'rxjs';

export type PblNgridSortOrder = 'asc' | 'desc';

export interface PblNgridSortInstructions {
order?: PblNgridSortOrder;
}

/**
* Event fired when sort changes.
*/
export interface PblNgridSortDefinition extends PblNgridSortInstructions {
sortFn?: PblNgridSorter;
}

/**
* A function that can sort a dataset based on `PblNgridSortInstructions`
*/
export interface PblNgridSorter<T = any> {
(column: PblColumnDefinition, sort: PblNgridSortInstructions, data: T[]): T[];
}

export interface PblNgridDataSourceSortChange {
column: PblColumnDefinition;
sort: PblNgridSortDefinition;
}


// FILTERING
/**
* A function the return true then the value should be included in the result or false when not.
* This is a single column filter predicated, returning false will filter out the entire row but the
* predicate is only intended to filter a specific column.
*/
export type DataSourceColumnPredicate = (filterValue: any, colValue: any, row?: any, col?: PblColumnDefinition) => boolean;
/**
* A function the return true then the row should be included in the result or false when not.
* @param row The row in the data source that the filter apply on
* @param properties A list of column instances (`PblColumnDefinition`) to filter values by.
*/
export type DataSourcePredicate = (row: any, properties: PblColumnDefinition[]) => boolean;

export type DataSourceFilterToken = undefined | DataSourcePredicate | any;

export interface DataSourceFilterType {
type: 'value' | 'predicate';
columns: PblColumnDefinition[];
filter: any | DataSourcePredicate;
}

export type DataSourceFilter = undefined | DataSourceFilterType ;
export type DataSourceOf<T> = T[] | Promise<T[]> | Observable<T[]>;
2 changes: 1 addition & 1 deletion libs/ngrid/core/src/lib/models/column.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataSourceColumnPredicate, PblNgridSorter } from '../data-source/types';
import { DataSourceColumnPredicate, PblNgridSorter } from '../data-source';

export type META_COLUMN_TYPES = 'header' | 'headerGroup' | 'footer';
export type COLUMN_TYPES = META_COLUMN_TYPES | 'table';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable, of, BehaviorSubject } from 'rxjs';
import { tap, finalize, take, filter, map } from 'rxjs/operators';
import { PblDataSourceTriggerChangedEvent, DataSourceOf, PblDataSourceConfigurableTriggers } from '@pebula/ngrid';
import { PblDataSourceTriggerChangedEvent, DataSourceOf, PblDataSourceConfigurableTriggers } from '@pebula/ngrid/core';
import { PblInfiniteScrollFactoryOptions, PblInfiniteScrollDsOptions, PblInfiniteScrollTriggerChangedEvent } from './infinite-scroll-datasource.types';
import { PblInfiniteScrollDataSourceCache } from './infinite-scroll-datasource.cache';
import { normalizeOptions, shouldTriggerInvisibleScroll, tryAddVirtualRowsBlock, updateCacheAndDataSource, upgradeChangeEventToInfinite } from './utils';
Expand All @@ -12,7 +12,7 @@ import { EventState } from './event-state';

// const LOG = msg => console.log(msg) ;

declare module '@pebula/ngrid/core/lib/data-source/data-source-adapter.types' {
declare module '@pebula/ngrid/core/lib/data-source/adapter/types' {
interface PblDataSourceTriggerChangedEventSource {
/**
* The source of the event was from a scroll that reached into a group of rows that the grid needs to fetch.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PblDataSourceBaseFactory, PblDataSourceAdapter } from '@pebula/ngrid';
import { PblDataSourceBaseFactory, PblDataSourceAdapter } from '@pebula/ngrid/core';
import { PblInfiniteScrollFactoryOptions, PblInfiniteScrollDsOptions, PblInfiniteScrollTriggerChangedEvent, PblInfiniteScrollCacheOptions } from './infinite-scroll-datasource.types';
import { PblInfiniteScrollDSContext } from './infinite-scroll-datasource.context';
import { PblInfiniteScrollDataSource } from './infinite-scroll-datasource';
Expand Down
2 changes: 1 addition & 1 deletion libs/ngrid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export {
PblDataSourceAdapter,
PblDataSource, PblDataSourceOptions,
PblNgridSortInstructions, PblNgridSortDefinition, PblNgridSorter, PblNgridSortOrder,
PblDataSourceBaseFactory, PblDataSourceFactory, DataSourceOf,
PblDataSourceFactory, DataSourceOf,
DataSourceFilterToken, DataSourcePredicate, DataSourceColumnPredicate,
PblDataSourceAdapterProcessedResult,
createDS, applySort,
Expand Down

0 comments on commit 6194f0e

Please sign in to comment.