UNPKG

4.13 kBTypeScriptView Raw
1import type { PayloadAction } from '../createAction';
2import type { IsAny } from '../tsHelpers';
3/**
4 * @public
5 */
6export declare type EntityId = number | string;
7/**
8 * @public
9 */
10export declare type Comparer<T> = (a: T, b: T) => number;
11/**
12 * @public
13 */
14export declare type IdSelector<T> = (model: T) => EntityId;
15/**
16 * @public
17 */
18export interface DictionaryNum<T> {
19 [id: number]: T | undefined;
20}
21/**
22 * @public
23 */
24export interface Dictionary<T> extends DictionaryNum<T> {
25 [id: string]: T | undefined;
26}
27/**
28 * @public
29 */
30export declare type Update<T> = {
31 id: EntityId;
32 changes: Partial<T>;
33};
34/**
35 * @public
36 */
37export interface EntityState<T> {
38 ids: EntityId[];
39 entities: Dictionary<T>;
40}
41/**
42 * @public
43 */
44export interface EntityDefinition<T> {
45 selectId: IdSelector<T>;
46 sortComparer: false | Comparer<T>;
47}
48export declare type PreventAny<S, T> = IsAny<S, EntityState<T>, S>;
49/**
50 * @public
51 */
52export interface EntityStateAdapter<T> {
53 addOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: T): S;
54 addOne<S extends EntityState<T>>(state: PreventAny<S, T>, action: PayloadAction<T>): S;
55 addMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: readonly T[] | Record<EntityId, T>): S;
56 addMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<readonly T[] | Record<EntityId, T>>): S;
57 setOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: T): S;
58 setOne<S extends EntityState<T>>(state: PreventAny<S, T>, action: PayloadAction<T>): S;
59 setMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: readonly T[] | Record<EntityId, T>): S;
60 setMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<readonly T[] | Record<EntityId, T>>): S;
61 setAll<S extends EntityState<T>>(state: PreventAny<S, T>, entities: readonly T[] | Record<EntityId, T>): S;
62 setAll<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<readonly T[] | Record<EntityId, T>>): S;
63 removeOne<S extends EntityState<T>>(state: PreventAny<S, T>, key: EntityId): S;
64 removeOne<S extends EntityState<T>>(state: PreventAny<S, T>, key: PayloadAction<EntityId>): S;
65 removeMany<S extends EntityState<T>>(state: PreventAny<S, T>, keys: readonly EntityId[]): S;
66 removeMany<S extends EntityState<T>>(state: PreventAny<S, T>, keys: PayloadAction<readonly EntityId[]>): S;
67 removeAll<S extends EntityState<T>>(state: PreventAny<S, T>): S;
68 updateOne<S extends EntityState<T>>(state: PreventAny<S, T>, update: Update<T>): S;
69 updateOne<S extends EntityState<T>>(state: PreventAny<S, T>, update: PayloadAction<Update<T>>): S;
70 updateMany<S extends EntityState<T>>(state: PreventAny<S, T>, updates: ReadonlyArray<Update<T>>): S;
71 updateMany<S extends EntityState<T>>(state: PreventAny<S, T>, updates: PayloadAction<ReadonlyArray<Update<T>>>): S;
72 upsertOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: T): S;
73 upsertOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: PayloadAction<T>): S;
74 upsertMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: readonly T[] | Record<EntityId, T>): S;
75 upsertMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<readonly T[] | Record<EntityId, T>>): S;
76}
77/**
78 * @public
79 */
80export interface EntitySelectors<T, V> {
81 selectIds: (state: V) => EntityId[];
82 selectEntities: (state: V) => Dictionary<T>;
83 selectAll: (state: V) => T[];
84 selectTotal: (state: V) => number;
85 selectById: (state: V, id: EntityId) => T | undefined;
86}
87/**
88 * @public
89 */
90export interface EntityAdapter<T> extends EntityStateAdapter<T> {
91 selectId: IdSelector<T>;
92 sortComparer: false | Comparer<T>;
93 getInitialState(): EntityState<T>;
94 getInitialState<S extends object>(state: S): EntityState<T> & S;
95 getSelectors(): EntitySelectors<T, EntityState<T>>;
96 getSelectors<V>(selectState: (state: V) => EntityState<T>): EntitySelectors<T, V>;
97}
98
\No newline at end of file