6
6
Update ,
7
7
EntityMap
8
8
} from './models'
9
- import { createStateOperator , DidMutate } from './state_adapter'
9
+ import { createStateOperator } from './state_adapter'
10
10
import { createUnsortedStateAdapter } from './unsorted_state_adapter'
11
11
import { selectIdValue } from './utils'
12
12
@@ -21,37 +21,32 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
21
21
selectId
22
22
)
23
23
24
- function addOneMutably ( entity : T , state : R ) : DidMutate
25
- function addOneMutably ( entity : any , state : any ) : DidMutate {
24
+ function addOneMutably ( entity : T , state : R ) : void
25
+ function addOneMutably ( entity : any , state : any ) : void {
26
26
return addManyMutably ( [ entity ] , state )
27
27
}
28
28
29
- function addManyMutably ( newModels : T [ ] , state : R ) : DidMutate
30
- function addManyMutably ( newModels : any [ ] , state : any ) : DidMutate {
29
+ function addManyMutably ( newModels : T [ ] , state : R ) : void
30
+ function addManyMutably ( newModels : any [ ] , state : any ) : void {
31
31
const models = newModels . filter (
32
32
model => ! ( selectIdValue ( model , selectId ) in state . entities )
33
33
)
34
34
35
- if ( models . length === 0 ) {
36
- return DidMutate . None
37
- } else {
35
+ if ( models . length !== 0 ) {
38
36
merge ( models , state )
39
- return DidMutate . Both
40
37
}
41
38
}
42
39
43
- function setAllMutably ( models : T [ ] , state : R ) : DidMutate
44
- function setAllMutably ( models : any [ ] , state : any ) : DidMutate {
40
+ function setAllMutably ( models : T [ ] , state : R ) : void
41
+ function setAllMutably ( models : any [ ] , state : any ) : void {
45
42
state . entities = { }
46
43
state . ids = [ ]
47
44
48
45
addManyMutably ( models , state )
49
-
50
- return DidMutate . Both
51
46
}
52
47
53
- function updateOneMutably ( update : Update < T > , state : R ) : DidMutate
54
- function updateOneMutably ( update : any , state : any ) : DidMutate {
48
+ function updateOneMutably ( update : Update < T > , state : R ) : void
49
+ function updateOneMutably ( update : any , state : any ) : void {
55
50
return updateManyMutably ( [ update ] , state )
56
51
}
57
52
@@ -72,43 +67,19 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
72
67
return newKey !== update . id
73
68
}
74
69
75
- function updateManyMutably ( updates : Update < T > [ ] , state : R ) : DidMutate
76
- function updateManyMutably ( updates : any [ ] , state : any ) : DidMutate {
70
+ function updateManyMutably ( updates : Update < T > [ ] , state : R ) : void
71
+ function updateManyMutably ( updates : any [ ] , state : any ) : void {
77
72
const models : T [ ] = [ ]
78
73
79
- const didMutateIds =
80
- updates . filter ( update => takeUpdatedModel ( models , update , state ) ) . length >
81
- 0
82
-
83
- if ( models . length === 0 ) {
84
- return DidMutate . None
85
- } else {
86
- const originalIds = state . ids
87
- const updatedIndexes : any [ ] = [ ]
88
- state . ids = state . ids . filter ( ( id : any , index : number ) => {
89
- if ( id in state . entities ) {
90
- return true
91
- } else {
92
- updatedIndexes . push ( index )
93
- return false
94
- }
95
- } )
74
+ updates . forEach ( update => takeUpdatedModel ( models , update , state ) )
96
75
76
+ if ( models . length !== 0 ) {
97
77
merge ( models , state )
98
-
99
- if (
100
- ! didMutateIds &&
101
- updatedIndexes . every ( ( i : number ) => state . ids [ i ] === originalIds [ i ] )
102
- ) {
103
- return DidMutate . EntitiesOnly
104
- } else {
105
- return DidMutate . Both
106
- }
107
78
}
108
79
}
109
80
110
- function mapMutably ( map : EntityMap < T > , state : R ) : DidMutate
111
- function mapMutably ( updatesOrMap : any , state : any ) : DidMutate {
81
+ function mapMutably ( map : EntityMap < T > , state : R ) : void
82
+ function mapMutably ( updatesOrMap : any , state : any ) : void {
112
83
const updates : Update < T > [ ] = state . ids . reduce (
113
84
( changes : any [ ] , id : string | number ) => {
114
85
const change = updatesOrMap ( state . entities [ id ] )
@@ -120,16 +91,16 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
120
91
[ ]
121
92
)
122
93
123
- return updateManyMutably ( updates , state )
94
+ updateManyMutably ( updates , state )
124
95
}
125
96
126
- function upsertOneMutably ( entity : T , state : R ) : DidMutate
127
- function upsertOneMutably ( entity : any , state : any ) : DidMutate {
97
+ function upsertOneMutably ( entity : T , state : R ) : void
98
+ function upsertOneMutably ( entity : any , state : any ) : void {
128
99
return upsertManyMutably ( [ entity ] , state )
129
100
}
130
101
131
- function upsertManyMutably ( entities : T [ ] , state : R ) : DidMutate
132
- function upsertManyMutably ( entities : any [ ] , state : any ) : DidMutate {
102
+ function upsertManyMutably ( entities : T [ ] , state : R ) : void
103
+ function upsertManyMutably ( entities : any [ ] , state : any ) : void {
133
104
const added : any [ ] = [ ]
134
105
const updated : any [ ] = [ ]
135
106
@@ -142,54 +113,42 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
142
113
}
143
114
}
144
115
145
- const didMutateByUpdated = updateManyMutably ( updated , state )
146
- const didMutateByAdded = addManyMutably ( added , state )
147
-
148
- switch ( true ) {
149
- case didMutateByAdded === DidMutate . None &&
150
- didMutateByUpdated === DidMutate . None :
151
- return DidMutate . None
152
- case didMutateByAdded === DidMutate . Both ||
153
- didMutateByUpdated === DidMutate . Both :
154
- return DidMutate . Both
155
- default :
156
- return DidMutate . EntitiesOnly
116
+ updateManyMutably ( updated , state )
117
+ addManyMutably ( added , state )
118
+ }
119
+
120
+ function areArraysEqual ( a : any [ ] , b : any [ ] ) {
121
+ if ( a . length !== b . length ) {
122
+ return false
123
+ }
124
+
125
+ for ( let i = 0 ; i < a . length && i < b . length ; i ++ ) {
126
+ if ( a [ i ] === b [ i ] ) {
127
+ continue
128
+ }
129
+ return false
157
130
}
131
+ return true
158
132
}
159
133
160
134
function merge ( models : T [ ] , state : R ) : void
161
135
function merge ( models : any [ ] , state : any ) : void {
162
136
models . sort ( sort )
163
137
164
- const ids : any [ ] = [ ]
138
+ // Insert/overwrite all new/updated
139
+ models . forEach ( model => {
140
+ state . entities [ selectId ( model ) ] = model
141
+ } )
165
142
166
- let i = 0
167
- let j = 0
143
+ const allEntities = Object . values ( state . entities )
144
+ allEntities . sort ( sort )
168
145
169
- while ( i < models . length && j < state . ids . length ) {
170
- const model = models [ i ]
171
- const modelId = selectIdValue ( model , selectId )
172
- const entityId = state . ids [ j ]
173
- const entity = state . entities [ entityId ]
146
+ const newSortedIds = allEntities . map ( selectId )
147
+ const { ids } = state
174
148
175
- if ( sort ( model , entity ) <= 0 ) {
176
- ids . push ( modelId )
177
- i ++
178
- } else {
179
- ids . push ( entityId )
180
- j ++
181
- }
149
+ if ( ! areArraysEqual ( ids , newSortedIds ) ) {
150
+ state . ids = newSortedIds
182
151
}
183
-
184
- if ( i < models . length ) {
185
- state . ids = ids . concat ( models . slice ( i ) . map ( selectId ) )
186
- } else {
187
- state . ids = ids . concat ( state . ids . slice ( j ) )
188
- }
189
-
190
- models . forEach ( model => {
191
- state . entities [ selectId ( model ) ] = model
192
- } )
193
152
}
194
153
195
154
return {
0 commit comments