Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit 4715554

Browse files
authored
feat: entities array observable in data entity collections (#619)
* feat: entities array observable in data entity collections * feat: entities array observable test * feat: entities array observable doc
1 parent 80d9229 commit 4715554

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

docs/pages/state-repository.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class ArticleEntitiesState extends NgxsDataEntityCollectionsRepository<Article>
8282
| state.snapshot | NgxsEntityCollections&lt;V, K&gt; | Memoized state value (getter) |
8383
| state.reset() | void | Reset state with default state value |
8484
| state.state\$ | Observable&lt;NgxsEntityCollections&lt;V, K&gt;&gt; | State data stream that you can subscribe to changes |
85+
| state.entityArray\$ | Observable&lt;V[]&gt; | Observable of entities array |
8586
| dispatch(actions: ActionType &verbar; ActionType[]) | void | Standard dispatch method |
8687
| state.addOne(entity: V) | void | Add one entity to the collection |
8788
| state.addMany(entities: V[]) | void | Add multiple entities to the collection |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { StateRepository } from '@ngxs-labs/data/decorators';
2+
import { State } from '@ngxs/store';
3+
import { createEntityCollections } from '@angular-ru/common/entity';
4+
import { NgxsDataEntityCollectionsRepository } from '@ngxs-labs/data/repositories';
5+
import { ngxsTestingPlatform } from '@ngxs-labs/data/testing';
6+
import { Injectable } from '@angular/core';
7+
8+
9+
describe('[TEST]: Entity observables', () => {
10+
describe('entityArray$', () => {
11+
interface StudentEntity {
12+
id: number;
13+
name: string;
14+
}
15+
16+
@StateRepository()
17+
@State({
18+
name: 'student',
19+
defaults: createEntityCollections()
20+
})
21+
@Injectable()
22+
class StudentEntitiesState extends NgxsDataEntityCollectionsRepository<StudentEntity, string> {}
23+
24+
it(
25+
'correct create entity arrays',
26+
ngxsTestingPlatform([StudentEntitiesState], (_store, studentEntities) => {
27+
const entityArrayEvents: StudentEntity[][] = [];
28+
29+
studentEntities.entitiesArray$.subscribe((entities) => {
30+
entityArrayEvents.push(entities)
31+
});
32+
33+
studentEntities.setAll([
34+
{
35+
id: 1,
36+
name: 'Maxim',
37+
},
38+
{
39+
id: 2,
40+
name: 'Ivan',
41+
},
42+
{
43+
id: 3,
44+
name: 'Nikola',
45+
},
46+
{
47+
id: 4,
48+
name: 'Petr',
49+
}
50+
]);
51+
52+
studentEntities.reset();
53+
54+
studentEntities.addOne({
55+
id: 1,
56+
name: 'Maxim',
57+
});
58+
59+
studentEntities.removeAll();
60+
61+
const entity: StudentEntity = {
62+
id: 4,
63+
name: 'Mark',
64+
};
65+
66+
studentEntities.upsertOne(entity);
67+
68+
studentEntities.removeByEntity(entity);
69+
70+
expect(entityArrayEvents).toEqual([
71+
[],
72+
[
73+
{
74+
id: 1,
75+
name: 'Maxim',
76+
},
77+
{
78+
id: 2,
79+
name: 'Ivan',
80+
},
81+
{
82+
id: 3,
83+
name: 'Nikola',
84+
},
85+
{
86+
id: 4,
87+
name: 'Petr',
88+
}
89+
],
90+
[],
91+
[
92+
{
93+
id: 1,
94+
name: 'Maxim',
95+
}
96+
],
97+
[],
98+
[
99+
{
100+
id: 4,
101+
name: 'Mark',
102+
}
103+
],
104+
[]
105+
]);
106+
})
107+
);
108+
});
109+
});

lib/repositories/src/ngxs-data-entity-collections/ngxs-data-entity-collections.repository.ts

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ export abstract class AbstractNgxsDataEntityCollectionsRepository<
5959
return this.state$.pipe(map((value: EntityCollections<V, K, C>): EntityDictionary<K, V> => value.entities));
6060
}
6161

62+
@Computed()
63+
public get entitiesArray$(): Observable<V[]> {
64+
return this.state$.pipe(
65+
map((value: EntityCollections<V, K, C>): V[] => value.ids.map(id => value.entities[id]))
66+
);
67+
}
68+
6269
protected get ctx(): EntityContext<V, K, C> {
6370
return ensureDataStateContext<EntityCollections<V, K, C>, StateContext<EntityCollections<V, K, C>>>(
6471
this.context as StateContext<EntityCollections<V, K, C>>

0 commit comments

Comments
 (0)