Skip to content

Commit

Permalink
feat: allow hasMore param in setEntitiesResult of withEntitiesRemoteS…
Browse files Browse the repository at this point in the history
…crollPagination
  • Loading branch information
Gabriel Guerrero authored and gabrielguerrero committed Apr 29, 2024
1 parent 531b36d commit f5a4422
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 83 deletions.
2 changes: 1 addition & 1 deletion apps/example-app/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"prefix": "ngrx-traits",
"targets": {
"build": {
"executor": "@angular-devkit/build-angular:browser",
"executor": "@angular-devkit/build-angular:browser-esbuild",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/apps/example-app",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { inject } from '@angular/core';
import {
getInfiniteScrollDataSource,
withCallStatus,
withEntitiesLoadingCall,
withEntitiesRemoteFilter,
Expand Down Expand Up @@ -41,3 +42,37 @@ export const ProductsBranchStore = signalStore(
}),
withLogger('branchStore'),
);
// const collection = 'products';
// export const ProductsBranchStore2 = signalStore(
// withEntities({
// entity,
// collection,
// }),
// withCallStatus({ initialValue: 'loading', collection }),
// withEntitiesRemoteFilter({
// entity,
// defaultFilter: { search: '' },
// collection,
// }),
// withEntitiesRemoteScrollPagination({
// bufferSize: 30,
// entity,
// collection,
// }),
// withEntitiesLoadingCall({
// collection,
// fetchEntities: async ({ productsRequest, productsFilter }) => {
// const res = await lastValueFrom(
// inject(BranchService).getBranches({
// search: productsFilter().search,
// skip: productsRequest().startIndex,
// take: productsRequest().size,
// }),
// );
// return { entities: res.resultList, total: res.total };
// },
// }),
// withLogger('branchStore'),
// );
// const store = new ProductsBranchStore2();
// getInfiniteScrollDataSource({ store, collection, entity });
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
withEntitiesRemotePagination,
withEntitiesRemoteSort,
withEntitiesSingleSelection,
withSyncToWebStorage,
} from '@ngrx-traits/signals';
import { signalStore, type } from '@ngrx/signals';
import { withEntities } from '@ngrx/signals/entities';
Expand All @@ -38,6 +37,11 @@ export const ProductsRemoteStore = signalStore(
pageSize: 5,
pagesToCache: 2,
}),
// withEntitiesRemoteScrollPagination({
// bufferSize: 5,
// collection,
// entity
// }),
withEntitiesRemoteSort({
entity,
collection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import {
export function withEntitiesLocalFilter<
Entity extends { id: string | number },
Filter extends Record<string, unknown>,
>(options: {
>(config: {
filterFn: (entity: Entity, filter?: Filter) => boolean;
defaultFilter: Filter;
entity?: Entity;
Expand Down Expand Up @@ -133,7 +133,7 @@ export function withEntitiesLocalFilter<
Entity extends { id: string | number },
Collection extends string,
Filter extends Record<string, unknown>,
>(options: {
>(config: {
filterFn: (entity: Entity, filter?: Filter) => boolean;
defaultFilter: Filter;
entity?: Entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('withEntitiesRemoteFilter', () => {
: false,
);
}
return of(result);
return of({ entities: result, total: result.length });
},
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export function withEntitiesRemoteFilter<
collection?: Collection;
}): SignalStoreFeature<
{
state: NamedEntityState<Entity, any>;
state: NamedEntityState<Entity, string>;
signals: NamedEntitySignals<Entity, Collection>;
methods: NamedCallStatusMethods<Collection>;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ import {
NamedCallStatusState,
} from '../with-call-status/with-call-status.model';
import { getWithCallStatusKeys } from '../with-call-status/with-call-status.util';
import { EntitiesPaginationRemoteMethods } from '../with-entities-pagination/with-entities-remote-pagination.model';
import {
NamedSetEntitiesResult,
SetEntitiesResult,
} from '../with-entities-pagination/with-entities-local-pagination.model';
import { getWithEntitiesRemotePaginationKeys } from '../with-entities-pagination/with-entities-remote-pagination.util';

/**
Expand Down Expand Up @@ -127,13 +130,13 @@ export function withEntitiesLoadingCall<
>,
) =>
| Observable<
Input['methods'] extends EntitiesPaginationRemoteMethods<Entity>
? { entities: Entity[]; total: number }
: Entity[] | { entities: Entity[] } // should this be { entities: Entity[];} for consistency?
Input['methods'] extends SetEntitiesResult<infer ResultParam>
? ResultParam
: Entity[] | { entities: Entity[] }
>
| Promise<
Input['methods'] extends EntitiesPaginationRemoteMethods<Entity>
? { entities: Entity[]; total: number }
Input['methods'] extends SetEntitiesResult<infer ResultParam>
? ResultParam
: Entity[] | { entities: Entity[] }
>;
mapPipe?: 'switchMap' | 'concatMap' | 'exhaustMap';
Expand Down Expand Up @@ -225,26 +228,20 @@ export function withEntitiesLoadingCall<
>,
) =>
| Observable<
Entity[] | { entities: Entity[]; total?: number }
// // TODO bellow is not working as expected
// Input['methods'] extends NamedEntitiesPaginationSetResultMethods<
// Entity,
// Collection
// >
// ? { entities: Entity[]; total: number } & Prettify<
// NamedEntitiesPaginationSetResultMethods<Entity, Collection>
// >
// : Entity[] | { entities: Entity[] }
Input['methods'] extends NamedSetEntitiesResult<
Collection,
infer ResultParam
>
? ResultParam
: Entity[] | { entities: Entity[] }
>
| Promise<
Entity[] | { entities: Entity[]; total?: number }
// TODO bellow is not working as expected
// Input['methods'] extends NamedEntitiesPaginationRemoteSetResult<
// Entity,
// Collection
// >
// ? { entities: Entity[]; total: number }
// : Entity[] | { entities: Entity[] }
Input['methods'] extends NamedSetEntitiesResult<
Collection,
infer ResultParam
>
? ResultParam
: Entity[] | { entities: Entity[] }
>;
mapPipe?: 'switchMap' | 'concatMap' | 'exhaustMap';
}): SignalStoreFeature<
Expand Down Expand Up @@ -288,7 +285,6 @@ export function withEntitiesLoadingCall<
const setError = store.methods[setErrorKey] as (error: unknown) => void;
const setEntitiesResult = store.methods[setEntitiesResultKey] as (result: {
entities: Entity[];
total: number;
}) => void;

return signalStoreFeature(
Expand All @@ -313,28 +309,19 @@ export function withEntitiesLoadingCall<
),
).pipe(
map((result) => {
if (Array.isArray(result)) {
if (setEntitiesResult) setEntitiesResult(result);
else {
const entities = Array.isArray(result)
? result
: result.entities;
patchState(
state,
collection
? setAllEntities(result as Entity[], {
? setAllEntities(entities as Entity[], {
collection,
})
: setAllEntities(result),
: setAllEntities(entities),
);
} else {
const { entities, total } = result;
if (setEntitiesResult)
setEntitiesResult({ entities, total });
else
patchState(
state,
collection
? setAllEntities(entities as Entity[], {
collection,
})
: setAllEntities(entities),
);
}
setLoaded();
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function getInfiniteScrollDataSource<Entity, Collection extends string>(
}
| {
collection: Collection;
entity: Entity;
store: NamedEntitySignals<Entity, Collection> &
NamedEntitiesScrollPaginationMethods<Entity, Collection>;
},
Expand All @@ -47,13 +48,11 @@ export function getInfiniteScrollDataSource<Entity, Collection extends string>(
this.subscription = collectionViewer.viewChange
.pipe(
filter(({ end, start }) => {
const { bufferSize, total } = entitiesScrollCache();
const { bufferSize, hasMore } = entitiesScrollCache();
// filter first request that is done by the cdkscroll,
// filter last request
// only do requests when you pass a specific threshold
return (
start != 0 && end <= total! && end + bufferSize >= entities.length
);
return start != 0 && hasMore && end + bufferSize >= entities.length;
}),
)
.subscribe(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ export type NamedEntitiesPaginationLocalMethods<Collection extends string> = {
pageIndex: number;
}) => void;
};

export type SetEntitiesResult<ResultParam> = {
setEntitiesResult: (result: ResultParam) => void;
};
export type NamedSetEntitiesResult<Collection extends string, ResultParam> = {
[K in Collection as `set${Capitalize<string & K>}Result`]: (
result: ResultParam,
) => void;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Signal } from '@angular/core';
import {
EntitiesPaginationLocalMethods,
NamedEntitiesPaginationLocalMethods,
NamedSetEntitiesResult,
SetEntitiesResult,
} from './with-entities-local-pagination.model';

export type PaginationState = {
Expand Down Expand Up @@ -60,17 +62,13 @@ export type NamedEntitiesPaginationRemoteComputed<
isLoading: boolean;
}>;
};

export type EntitiesPaginationRemoteMethods<Entity> =
EntitiesPaginationLocalMethods & {
setEntitiesResult: (result: { entities: Entity[]; total: number }) => void;
};
EntitiesPaginationLocalMethods &
SetEntitiesResult<{ entities: Entity[]; total: number }>;

export type NamedEntitiesPaginationRemoteMethods<
Entity,
Collection extends string,
> = NamedEntitiesPaginationLocalMethods<Collection> & {
[K in Collection as `set${Capitalize<string & K>}Result`]: (result: {
entities: Entity[];
total: number;
}) => void;
};
> = NamedEntitiesPaginationLocalMethods<Collection> &
NamedSetEntitiesResult<Collection, { entities: Entity[]; total: number }>;
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Signal } from '@angular/core';

import {
NamedSetEntitiesResult,
SetEntitiesResult,
} from './with-entities-local-pagination.model';

export type ScrollPaginationState = {
bufferSize: number;
total: number | undefined;
hasMore: boolean;
};
export type EntitiesScrollPaginationState = {
Expand All @@ -26,18 +30,37 @@ export type NamedEntitiesScrollPaginationComputed<
size: number;
}>;
};
export type EntitiesScrollPaginationMethods<Entity> = {
setEntitiesResult: (result: { entities: Entity[]; total: number }) => void;
export type EntitiesScrollPaginationMethods<Entity> = SetEntitiesResult<
| {
entities: Entity[];
total: number;
}
| {
entities: Entity[];
hasMore: boolean;
}
| {
entities: Entity[];
}
> & {
loadMoreEntities: () => void;
};
export type NamedEntitiesScrollPaginationMethods<
Entity,
Collection extends string,
> = {
[K in Collection as `set${Capitalize<string & K>}Result`]: (result: {
entities: Entity[];
total: number;
}) => void;
} & {
> = NamedSetEntitiesResult<
Collection,
| {
entities: Entity[];
total: number;
}
| {
entities: Entity[];
hasMore: boolean;
}
| {
entities: Entity[];
}
> & {
[K in Collection as `loadMore${Capitalize<string & K>}`]: () => void;
};
Loading

0 comments on commit f5a4422

Please sign in to comment.