@ngxs-labs/entity-state
Version:
<p align="center"> <img src="https://raw.githubusercontent.com/ngxs-labs/emitter/master/docs/assets/logo.png"> </p>
125 lines (124 loc) • 6.06 kB
TypeScript
import { Type } from '@angular/core';
import { StateContext } from '@ngxs/store';
import { EntityAddAction, EntityCreateOrReplaceAction, EntityGoToPageAction, EntityRemoveAction, EntitySetActiveAction, EntitySetErrorAction, EntitySetLoadingAction, EntitySetPageSizeAction, EntityUpdateAction, EntityUpdateActiveAction } from './actions';
import { IdStrategy } from './id-strategy';
import { Dictionary } from './internal';
import { EntityStateModel, StateSelector } from './models';
import IdGenerator = IdStrategy.IdGenerator;
/**
* Returns a new object which serves as the default state.
* No entities, loading is false, error is undefined, active is undefined.
* pageSize is 10 and pageIndex is 0.
*/
export declare function defaultEntityState<T>(defaults?: Partial<EntityStateModel<T>>): EntityStateModel<T>;
export declare abstract class EntityState<T extends {}> {
private readonly idKey;
private readonly storePath;
protected readonly idGenerator: IdGenerator<T>;
protected constructor(storeClass: Type<EntityState<T>>, _idKey: keyof T, idStrategy: Type<IdGenerator<T>>);
/**
* This function is called every time an entity is updated.
* It receives the current entity and a partial entity that was either passed directly or generated with a function.
* The default implementation uses the spread operator to create a new entity.
* You must override this method if your entity type does not support the spread operator.
* @see Updater
* @param current The current entity, readonly
* @param updated The new data as a partial entity
* @example
* // default behavior
* onUpdate(current: Readonly<T updated: Partial<T>): T {
return {...current, ...updated};
}
*/
onUpdate(current: Readonly<T>, updated: Partial<T>): T;
/**
* Returns a selector for the activeId
*/
static get activeId(): StateSelector<string>;
/**
* Returns a selector for the active entity
*/
static get active(): StateSelector<any>;
/**
* Returns a selector for the keys of all entities
*/
static get keys(): StateSelector<string[]>;
/**
* Returns a selector for all entities, sorted by insertion order
*/
static get entities(): StateSelector<any[]>;
/**
* Returns a selector for the nth entity, sorted by insertion order
*/
static nthEntity(index: number): StateSelector<any>;
/**
* Returns a selector for paginated entities, sorted by insertion order
*/
static get paginatedEntities(): StateSelector<any[]>;
/**
* Returns a selector for the map of entities
*/
static get entitiesMap(): StateSelector<Dictionary<any>>;
/**
* Returns a selector for the size of the entity map
*/
static get size(): StateSelector<number>;
/**
* Returns a selector for the error
*/
static get error(): StateSelector<Error | undefined>;
/**
* Returns a selector for the loading state
*/
static get loading(): StateSelector<boolean>;
/**
* Returns a selector for the latest added entity
*/
static get latest(): StateSelector<any>;
/**
* Returns a selector for the latest added entity id
*/
static get latestId(): StateSelector<string | undefined>;
/**
* Returns a selector for the update timestamp
*/
static get lastUpdated(): StateSelector<Date>;
/**
* Returns a selector for age, based on the update timestamp
*/
static get age(): StateSelector<number>;
/**
* The entities given by the payload will be added.
* For certain ID strategies this might fail, if it provides an existing ID.
* In all cases it will overwrite the ID value in the entity with the calculated ID.
*/
add({ setState }: StateContext<EntityStateModel<T>>, { payload }: EntityAddAction<T>): void;
/**
* The entities given by the payload will be added.
* It first checks if the ID provided by each entity does exist.
* If it does the current entity will be replaced.
* In all cases it will overwrite the ID value in the entity with the calculated ID.
*/
createOrReplace({ setState }: StateContext<EntityStateModel<T>>, { payload }: EntityCreateOrReplaceAction<T>): void;
update({ setState }: StateContext<EntityStateModel<T>>, { payload }: EntityUpdateAction<T>): void;
updateAll({ setState }: StateContext<EntityStateModel<T>>, { payload }: EntityUpdateAction<T>): void;
updateActive({ setState }: StateContext<EntityStateModel<T>>, { payload }: EntityUpdateActiveAction<T>): void;
removeActive({ getState, setState }: StateContext<EntityStateModel<T>>): void;
remove({ getState, setState, patchState }: StateContext<EntityStateModel<T>>, { payload }: EntityRemoveAction<T>): void;
removeAll({ setState }: StateContext<EntityStateModel<T>>): void;
reset({ setState }: StateContext<EntityStateModel<T>>): void;
setLoading({ patchState }: StateContext<EntityStateModel<T>>, { payload }: EntitySetLoadingAction): void;
setActive({ patchState }: StateContext<EntityStateModel<T>>, { payload }: EntitySetActiveAction): void;
clearActive({ patchState }: StateContext<EntityStateModel<T>>): void;
setError({ patchState }: StateContext<EntityStateModel<T>>, { payload }: EntitySetErrorAction): void;
goToPage({ getState, patchState }: StateContext<EntityStateModel<T>>, { payload }: EntityGoToPageAction): void;
setPageSize({ patchState }: StateContext<EntityStateModel<T>>, { payload }: EntitySetPageSizeAction): void;
private setup;
/**
* Returns the id of the given entity, based on the defined idKey.
* This methods allows Partial entities and thus might return undefined.
* Other methods calling this one have to handle this case themselves.
* @param data a partial entity
*/
protected idOf(data: Partial<T>): string | undefined;
}