source: node_modules/@reduxjs/toolkit/src/entities/models.ts@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 5.0 KB
Line 
1import type { Draft } from 'immer'
2import type { PayloadAction } from '../createAction'
3import type { CastAny, Id } from '../tsHelpers'
4import type { UncheckedIndexedAccess } from '../uncheckedindexed.js'
5import type { GetSelectorsOptions } from './state_selectors'
6
7/**
8 * @public
9 */
10export type EntityId = number | string
11
12/**
13 * @public
14 */
15export type Comparer<T> = (a: T, b: T) => number
16
17/**
18 * @public
19 */
20export type IdSelector<T, Id extends EntityId> = (model: T) => Id
21
22/**
23 * @public
24 */
25export type Update<T, Id extends EntityId> = { id: Id; changes: Partial<T> }
26
27/**
28 * @public
29 */
30export interface EntityState<T, Id extends EntityId> {
31 ids: Id[]
32 entities: Record<Id, T>
33}
34
35/**
36 * @public
37 */
38export interface EntityAdapterOptions<T, Id extends EntityId> {
39 selectId?: IdSelector<T, Id>
40 sortComparer?: false | Comparer<T>
41}
42
43export type PreventAny<S, T, Id extends EntityId> = CastAny<
44 S,
45 EntityState<T, Id>
46>
47
48export type DraftableEntityState<T, Id extends EntityId> =
49 | EntityState<T, Id>
50 | Draft<EntityState<T, Id>>
51
52/**
53 * @public
54 */
55export interface EntityStateAdapter<T, Id extends EntityId> {
56 addOne<S extends DraftableEntityState<T, Id>>(
57 state: PreventAny<S, T, Id>,
58 entity: T,
59 ): S
60 addOne<S extends DraftableEntityState<T, Id>>(
61 state: PreventAny<S, T, Id>,
62 action: PayloadAction<T>,
63 ): S
64
65 addMany<S extends DraftableEntityState<T, Id>>(
66 state: PreventAny<S, T, Id>,
67 entities: readonly T[] | Record<Id, T>,
68 ): S
69 addMany<S extends DraftableEntityState<T, Id>>(
70 state: PreventAny<S, T, Id>,
71 entities: PayloadAction<readonly T[] | Record<Id, T>>,
72 ): S
73
74 setOne<S extends DraftableEntityState<T, Id>>(
75 state: PreventAny<S, T, Id>,
76 entity: T,
77 ): S
78 setOne<S extends DraftableEntityState<T, Id>>(
79 state: PreventAny<S, T, Id>,
80 action: PayloadAction<T>,
81 ): S
82 setMany<S extends DraftableEntityState<T, Id>>(
83 state: PreventAny<S, T, Id>,
84 entities: readonly T[] | Record<Id, T>,
85 ): S
86 setMany<S extends DraftableEntityState<T, Id>>(
87 state: PreventAny<S, T, Id>,
88 entities: PayloadAction<readonly T[] | Record<Id, T>>,
89 ): S
90 setAll<S extends DraftableEntityState<T, Id>>(
91 state: PreventAny<S, T, Id>,
92 entities: readonly T[] | Record<Id, T>,
93 ): S
94 setAll<S extends DraftableEntityState<T, Id>>(
95 state: PreventAny<S, T, Id>,
96 entities: PayloadAction<readonly T[] | Record<Id, T>>,
97 ): S
98
99 removeOne<S extends DraftableEntityState<T, Id>>(
100 state: PreventAny<S, T, Id>,
101 key: Id,
102 ): S
103 removeOne<S extends DraftableEntityState<T, Id>>(
104 state: PreventAny<S, T, Id>,
105 key: PayloadAction<Id>,
106 ): S
107
108 removeMany<S extends DraftableEntityState<T, Id>>(
109 state: PreventAny<S, T, Id>,
110 keys: readonly Id[],
111 ): S
112 removeMany<S extends DraftableEntityState<T, Id>>(
113 state: PreventAny<S, T, Id>,
114 keys: PayloadAction<readonly Id[]>,
115 ): S
116
117 removeAll<S extends DraftableEntityState<T, Id>>(
118 state: PreventAny<S, T, Id>,
119 ): S
120
121 updateOne<S extends DraftableEntityState<T, Id>>(
122 state: PreventAny<S, T, Id>,
123 update: Update<T, Id>,
124 ): S
125 updateOne<S extends DraftableEntityState<T, Id>>(
126 state: PreventAny<S, T, Id>,
127 update: PayloadAction<Update<T, Id>>,
128 ): S
129
130 updateMany<S extends DraftableEntityState<T, Id>>(
131 state: PreventAny<S, T, Id>,
132 updates: ReadonlyArray<Update<T, Id>>,
133 ): S
134 updateMany<S extends DraftableEntityState<T, Id>>(
135 state: PreventAny<S, T, Id>,
136 updates: PayloadAction<ReadonlyArray<Update<T, Id>>>,
137 ): S
138
139 upsertOne<S extends DraftableEntityState<T, Id>>(
140 state: PreventAny<S, T, Id>,
141 entity: T,
142 ): S
143 upsertOne<S extends DraftableEntityState<T, Id>>(
144 state: PreventAny<S, T, Id>,
145 entity: PayloadAction<T>,
146 ): S
147
148 upsertMany<S extends DraftableEntityState<T, Id>>(
149 state: PreventAny<S, T, Id>,
150 entities: readonly T[] | Record<Id, T>,
151 ): S
152 upsertMany<S extends DraftableEntityState<T, Id>>(
153 state: PreventAny<S, T, Id>,
154 entities: PayloadAction<readonly T[] | Record<Id, T>>,
155 ): S
156}
157
158/**
159 * @public
160 */
161export interface EntitySelectors<T, V, IdType extends EntityId> {
162 selectIds: (state: V) => IdType[]
163 selectEntities: (state: V) => Record<IdType, T>
164 selectAll: (state: V) => T[]
165 selectTotal: (state: V) => number
166 selectById: (state: V, id: IdType) => Id<UncheckedIndexedAccess<T>>
167}
168
169/**
170 * @public
171 */
172export interface EntityStateFactory<T, Id extends EntityId> {
173 getInitialState(
174 state?: undefined,
175 entities?: Record<Id, T> | readonly T[],
176 ): EntityState<T, Id>
177 getInitialState<S extends object>(
178 state: S,
179 entities?: Record<Id, T> | readonly T[],
180 ): EntityState<T, Id> & S
181}
182
183/**
184 * @public
185 */
186export interface EntityAdapter<T, Id extends EntityId>
187 extends EntityStateAdapter<T, Id>,
188 EntityStateFactory<T, Id>,
189 Required<EntityAdapterOptions<T, Id>> {
190 getSelectors(
191 selectState?: undefined,
192 options?: GetSelectorsOptions,
193 ): EntitySelectors<T, EntityState<T, Id>, Id>
194 getSelectors<V>(
195 selectState: (state: V) => EntityState<T, Id>,
196 options?: GetSelectorsOptions,
197 ): EntitySelectors<T, V, Id>
198}
Note: See TracBrowser for help on using the repository browser.