Skip to content
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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

Dalantren
Copy link
Contributor

No description provided.

@@ -21,23 +23,29 @@ export class ExampleDataProviderComponent {

page$: Observable<Atom[]>;

dataProvider: DataProvider<Atom>;
gridModel: GridModel;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gridModel = this.qgrid.model();

Copy link
Contributor Author

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>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialize dataPovider right here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it private

Copy link
Collaborator

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 });
Copy link
Collaborator

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

Copy link
Collaborator

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[]> {
Copy link
Collaborator

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

this.gridModel = this.qgrid.model();

this.dataProvider = new DataProvider<Atom>([
RequestCountOnceStrategy,
Copy link
Collaborator

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,
Copy link
Collaborator

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(
Copy link
Collaborator

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>;
Copy link
Collaborator

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());
Copy link
Collaborator

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?

Copy link
Collaborator

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

Copy link
Contributor Author

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[]>;
Copy link
Collaborator

@klumba12 klumba12 Mar 24, 2022

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

Copy link
Contributor Author

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[]>;
Copy link
Collaborator

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?

Copy link
Contributor Author

@Dalantren Dalantren Mar 25, 2022

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> {
Copy link
Collaborator

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?

Copy link
Contributor Author

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new line

Copy link
Contributor Author

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 });
Copy link
Collaborator

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

Copy link
Contributor Author

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';
Copy link
Collaborator

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

Copy link
Contributor Author

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';
Copy link
Collaborator

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

Copy link
Contributor Author

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>;
Copy link
Collaborator

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';
Copy link
Collaborator

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

Copy link
Contributor Author

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,
Copy link
Collaborator

@klumba12 klumba12 Mar 25, 2022

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

Copy link
Contributor Author

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);
Copy link
Collaborator

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[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename memo to data

Copy link
Contributor Author

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;
Copy link
Collaborator

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

Copy link
Contributor Author

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[]>;
Copy link
Collaborator

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?

Copy link
Contributor Author

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 {
Copy link
Collaborator

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?

Copy link
Contributor Author

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[]>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo -> data

Copy link
Contributor Author

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[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo -> data

Copy link
Contributor Author

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) {
Copy link
Collaborator

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?

Copy link
Contributor Author

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 },
Copy link
Collaborator

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

Copy link
Contributor Author

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) {
Copy link
Collaborator

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

Copy link
Contributor Author

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))
Copy link
Collaborator

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadBackgroundPages maybe beters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants