Skip to content

Commit 2ccdbd4

Browse files
committed
Swap state operator order to (state, arg) and support FSAs
- Swapped arguments to state operators so that they can be reused as mostly standard Redux reducers - Added a check to handle arg as either an FSA action or a value - Swapped argument order in all test cases - Added one test to provide reading payload from FSAs works
1 parent 293b0d1 commit 2ccdbd4

File tree

5 files changed

+286
-251
lines changed

5 files changed

+286
-251
lines changed

src/entities/models.ts

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PayloadAction } from '../createAction'
2+
13
export type ComparerStr<T> = (a: T, b: T) => string
24
export type ComparerNum<T> = (a: T, b: T) => number
35

@@ -32,6 +34,8 @@ export type Predicate<T> = (entity: T) => boolean
3234

3335
export type EntityMap<T> = (entity: T) => T
3436

37+
export type TypeOrPayloadAction<T> = T | PayloadAction<T>
38+
3539
export interface EntityState<T> {
3640
ids: string[] | number[]
3741
entities: Dictionary<T>
@@ -43,27 +47,61 @@ export interface EntityDefinition<T> {
4347
}
4448

4549
export interface EntityStateAdapter<T> {
46-
addOne<S extends EntityState<T>>(entity: T, state: S): S
47-
addMany<S extends EntityState<T>>(entities: T[], state: S): S
48-
49-
setAll<S extends EntityState<T>>(entities: T[], state: S): S
50-
51-
removeOne<S extends EntityState<T>>(key: string, state: S): S
52-
removeOne<S extends EntityState<T>>(key: number, state: S): S
53-
54-
removeMany<S extends EntityState<T>>(keys: string[], state: S): S
55-
removeMany<S extends EntityState<T>>(keys: number[], state: S): S
56-
removeMany<S extends EntityState<T>>(predicate: Predicate<T>, state: S): S
50+
addOne<S extends EntityState<T>>(state: S, entity: TypeOrPayloadAction<T>): S
51+
addMany<S extends EntityState<T>>(
52+
state: S,
53+
entities: TypeOrPayloadAction<T[]>
54+
): S
55+
56+
setAll<S extends EntityState<T>>(
57+
state: S,
58+
entities: TypeOrPayloadAction<T[]>
59+
): S
60+
61+
removeOne<S extends EntityState<T>>(
62+
state: S,
63+
key: TypeOrPayloadAction<string>
64+
): S
65+
removeOne<S extends EntityState<T>>(
66+
state: S,
67+
key: TypeOrPayloadAction<number>
68+
): S
69+
70+
removeMany<S extends EntityState<T>>(
71+
state: S,
72+
keys: TypeOrPayloadAction<string[]>
73+
): S
74+
removeMany<S extends EntityState<T>>(
75+
state: S,
76+
keys: TypeOrPayloadAction<number[]>
77+
): S
78+
/** @deprecated Should not pass functions inside of Redux actions */
79+
removeMany<S extends EntityState<T>>(state: S, predicate: Predicate<T>): S
5780

5881
removeAll<S extends EntityState<T>>(state: S): S
5982

60-
updateOne<S extends EntityState<T>>(update: Update<T>, state: S): S
61-
updateMany<S extends EntityState<T>>(updates: Update<T>[], state: S): S
62-
63-
upsertOne<S extends EntityState<T>>(entity: T, state: S): S
64-
upsertMany<S extends EntityState<T>>(entities: T[], state: S): S
65-
66-
map<S extends EntityState<T>>(map: EntityMap<T>, state: S): S
83+
updateOne<S extends EntityState<T>>(
84+
state: S,
85+
update: TypeOrPayloadAction<Update<T>>
86+
): S
87+
updateMany<S extends EntityState<T>>(
88+
state: S,
89+
updates: TypeOrPayloadAction<Update<T>[]>
90+
): S
91+
92+
upsertOne<S extends EntityState<T>>(
93+
state: S,
94+
entity: TypeOrPayloadAction<T>
95+
): S
96+
upsertMany<S extends EntityState<T>>(
97+
state: S,
98+
entities: TypeOrPayloadAction<T[]>
99+
): S
100+
101+
map<S extends EntityState<T>>(
102+
state: S,
103+
map: TypeOrPayloadAction<EntityMap<T>>
104+
): S
67105
}
68106

69107
export interface EntitySelectors<T, V> {

0 commit comments

Comments
 (0)