-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/data provider strategies #585
base: master
Are you sure you want to change the base?
Feature/data provider strategies #585
Conversation
@@ -21,23 +23,29 @@ export class ExampleDataProviderComponent { | |||
|
|||
page$: Observable<Atom[]>; | |||
|
|||
dataProvider: DataProvider<Atom>; | |||
gridModel: GridModel; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gridModel = this.qgrid.model();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -21,23 +23,29 @@ export class ExampleDataProviderComponent { | |||
|
|||
page$: Observable<Atom[]>; | |||
|
|||
dataProvider: DataProvider<Atom>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initialize dataPovider right here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make it private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we again try to initialize dataProvider right here?
RequestCountOnceStrategy, | ||
CacheAlreadyRequestedPageStrategy, | ||
ReverseDataStrategy, | ||
], { server, gridModel: this.gridModel, pageSize: 50 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove pageSize here, use it from this.gridModel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const server = new FakeServer(this.dataService);
new DataProvider<Atom>(this.gridModel, [
new RequestCountOnceStrategy(server),
new CacheAlreadyRequestedPageStrategy(server, { backgroundPages: 2 }),
new ReverseDataStrategy(),
]);
protected server: DataProviderServer<T>; | ||
protected gridModel: GridModel; | ||
|
||
processData(memo: T[], page?: number, size?: number): Observable<T[]> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use gridModel to get page and size
packages/qgrid-ngx-plugins/src/lib/data-provider/strategies/request-count-once.ts
Outdated
Show resolved
Hide resolved
this.gridModel = this.qgrid.model(); | ||
|
||
this.dataProvider = new DataProvider<Atom>([ | ||
RequestCountOnceStrategy, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RequestTotalCountOnceStategy
this.dataProvider = new DataProvider<Atom>([ | ||
RequestCountOnceStrategy, | ||
CacheAlreadyRequestedPageStrategy, | ||
ReverseDataStrategy, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add BackgroundLoadPageStrategy(server, { numberOfPagesToLoad: 2 });
} | ||
} | ||
|
||
class FakeServer { | ||
class FakeServer implements DataProviderServer<Atom> { | ||
constructor( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DataProviderCache()
@PataProviderBackground()
getPage() {}
@DataProviderCache()
@PataProviderBackground()
getPage() {}
@@ -21,23 +23,29 @@ export class ExampleDataProviderComponent { | |||
|
|||
page$: Observable<Atom[]>; | |||
|
|||
dataProvider: DataProvider<Atom>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make it private
|
||
class ReverseDataStrategy<T> extends DataProviderStrategy<T> { | ||
processData(memo: T[]): Observable<T[]> { | ||
return of(memo.slice().reverse()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we need to do memo slice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename it please to ExampleReverseDataStrategy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we need to do memo slice?
.reverse() mutates initial array
export abstract class DataProviderStrategy<T> { | ||
protected gridModel: GridModel; | ||
|
||
abstract processData(memo: T[]): Observable<T[]>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make it as an interface with method
process(data: T[], context: { model: GridModel }): Observable<T[]>
still not sure about the signature with Observable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
export interface DataProviderServer<T> { | ||
getPage(page?: number, size?: number): Observable<T[]>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why page and size are optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we doesn't know how user will implement communication with server. Maybe it won't need any information about page or size
import { GridModel } from '@qgrid/ngx'; | ||
import { Observable } from 'rxjs'; | ||
|
||
export interface DataProviderOptions<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we need this interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
this.setPaginationCount(this.totalCount); | ||
return of(memo); | ||
} | ||
return this.server.getTotal() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add new line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
private setPaginationCount(count: number): void { | ||
this.gridModel.pagination({ count }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure that we need a dedicated method to just setup pagination count
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
@@ -0,0 +1,13 @@ | |||
import { GridModel } from '@qgrid/ngx'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can u put all strategies directly to the data-provider folder? also remove please index.ts file, we don't use them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -0,0 +1,13 @@ | |||
import { GridModel } from '@qgrid/ngx'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename file to the server.ts or maybe page-server.ts and PageServer class? not sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed interface to DataProviderPageServer
@@ -21,23 +23,29 @@ export class ExampleDataProviderComponent { | |||
|
|||
page$: Observable<Atom[]>; | |||
|
|||
dataProvider: DataProvider<Atom>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we again try to initialize dataProvider right here?
@@ -0,0 +1,45 @@ | |||
import { GridModel } from '@qgrid/ngx'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file name should be cache-already-requested-page-strategy.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
||
constructor( | ||
private server: Pick<DataProviderPageServer<T>, 'getPage'>, | ||
private pagesToLoad: number = 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be options { pagesToLoad: number }
, consider to rename pagesToLoad to numberOfBackgroundPages or backgroundPages
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
const { current, size } = model.pagination(); | ||
|
||
if (this.pagesToLoad) { | ||
this.loadInBackground(this.pagesToLoad, model); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if process will conflict with loadInBackground? need to have synchronization mechanizm
private pagesToLoad: number = 0, | ||
) { } | ||
|
||
process(memo: T[], { model }: DataProviderProcessContext): Observable<T[]> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename memo to data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
return of(this.gridRowsCache.get(current)); | ||
} | ||
|
||
const shouldRequestData = !memo?.length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why memo can be undefined? our method signature doesn't accept it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah. It's always at least an empty array. Removed question mark
import { Observable } from 'rxjs'; | ||
|
||
export interface DataProviderPageServer<T> { | ||
getPage(page?: number, size?: number): Observable<T[]>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why page and size are optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made them required
@@ -0,0 +1,5 @@ | |||
import { GridModel } from '@qgrid/ngx'; | |||
|
|||
export interface DataProviderProcessContext { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you rename it to DataProviderContext and put under the data-provider.ts file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
import { DataProviderProcessContext } from './data-provider-process-context'; | ||
|
||
export interface DataProviderStrategy<T> { | ||
process(memo: T[], context: DataProviderProcessContext): Observable<T[]>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memo -> data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
private server: Pick<DataProviderPageServer<T>, 'getTotal'>, | ||
) { } | ||
|
||
process(memo: T[], { model }: DataProviderProcessContext): Observable<T[]> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memo -> data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
) { } | ||
|
||
process(memo: T[], { model }: DataProviderProcessContext): Observable<T[]> { | ||
if (this.totalCount > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what will happen if count will be 0 from the server?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if there was empty array from server wi will try to send that request everytime. Do you think we should remove it?
|
||
constructor( | ||
private server: Pick<DataProviderPageServer<T>, 'getPage'>, | ||
private options: { pagesToLoad: number } = { pagesToLoad: 0 }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by default pagesToLoad should be equal to 1, if more than 1 do background load
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
process(data: T[], { model }: DataProviderContext): Observable<T[]> { | ||
const { current, size } = model.pagination(); | ||
|
||
if (this.paginationSize !== size) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move it under the separate method like invalidateCache()
will be more readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
const shouldRequestData = !data.length; | ||
return (shouldRequestData ? this.server.getPage(current, size) : of(data)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with if
statement will be more readable
.pipe(tap(rows => this.pageCache.set(current, rows))); | ||
} | ||
|
||
private loadInBackground(pagesToLoad: number, model: GridModel): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loadBackgroundPages maybe beters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
No description provided.