forked from johnpapa/angular-ngrx-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity-collection-creator.ts
33 lines (27 loc) · 1.05 KB
/
entity-collection-creator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Injectable, Optional } from '@angular/core';
import { EntityCollection } from './entity-collection';
import { EntityDefinitionService } from '../entity-metadata/entity-definition.service';
@Injectable()
export class EntityCollectionCreator {
constructor(@Optional() private entityDefinitionService?: EntityDefinitionService) {}
/**
* Create the default collection for an entity type.
* @param entityName {string} entity type name
*/
create<T = any, S extends EntityCollection<T> = EntityCollection<T>>(entityName: string): S {
const def = this.entityDefinitionService && this.entityDefinitionService.getDefinition<T>(entityName, false /*shouldThrow*/);
const initialState = def && def.initialState;
return <S>(initialState || createEmptyEntityCollection<T>(entityName));
}
}
export function createEmptyEntityCollection<T>(entityName?: string): EntityCollection<T> {
return {
entityName,
ids: [],
entities: {},
filter: undefined,
loaded: false,
loading: false,
changeState: {}
} as EntityCollection<T>;
}