@@ -13,6 +13,7 @@ import {
13
13
selectIdValue ,
14
14
ensureEntitiesArray ,
15
15
splitAddedUpdatedEntities ,
16
+ getCurrent ,
16
17
} from './utils'
17
18
18
19
export function findInsertIndex < T > (
@@ -65,11 +66,17 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
65
66
function addManyMutably (
66
67
newEntities : readonly T [ ] | Record < Id , T > ,
67
68
state : R ,
69
+ existingIds ?: Id [ ] ,
68
70
) : void {
69
71
newEntities = ensureEntitiesArray ( newEntities )
70
72
73
+ const existingKeys = new Set < Id > (
74
+ existingIds ?? ( current ( state . ids ) as Id [ ] ) ,
75
+ )
76
+
71
77
const models = newEntities . filter (
72
- ( model ) => ! ( selectIdValue ( model , selectId ) in state . entities ) ,
78
+ // (model) => !(selectId(model) in state.entities),
79
+ ( model ) => ! existingKeys . has ( selectIdValue ( model , selectId ) ) ,
73
80
)
74
81
75
82
if ( models . length !== 0 ) {
@@ -103,7 +110,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
103
110
state . entities = { } as Record < Id , T >
104
111
state . ids = [ ]
105
112
106
- addManyMutably ( newEntities , state )
113
+ addManyMutably ( newEntities , state , [ ] )
107
114
}
108
115
109
116
function updateOneMutably ( update : Update < T , Id > , state : R ) : void {
@@ -153,14 +160,18 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
153
160
newEntities : readonly T [ ] | Record < Id , T > ,
154
161
state : R ,
155
162
) : void {
156
- const [ added , updated ] = splitAddedUpdatedEntities < T , Id > (
163
+ const [ added , updated , existingIdsArray ] = splitAddedUpdatedEntities < T , Id > (
157
164
newEntities ,
158
165
selectId ,
159
166
state ,
160
167
)
161
168
162
- updateManyMutably ( updated , state )
163
- addManyMutably ( added , state )
169
+ if ( updated . length ) {
170
+ updateManyMutably ( updated , state )
171
+ }
172
+ if ( added . length ) {
173
+ addManyMutably ( added , state , existingIdsArray )
174
+ }
164
175
}
165
176
166
177
function areArraysEqual ( a : readonly unknown [ ] , b : readonly unknown [ ] ) {
@@ -311,10 +322,14 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
311
322
updatedIds ,
312
323
replacedIds ,
313
324
) => {
314
- const entities = state . entities as Record < Id , T >
315
- let ids = state . ids as Id [ ]
325
+ const currentEntities = getCurrent ( state . entities ) as Record < Id , T >
326
+ const currentIds = getCurrent ( state . ids ) as Id [ ]
327
+ // const entities = state.entities as Record<Id, T>
328
+ const stateEntities = state . entities as Record < Id , T >
329
+ // let ids = state.ids as Id[]
330
+ let ids = currentIds
316
331
if ( replacedIds ) {
317
- ids = Array . from ( new Set ( ids ) )
332
+ ids = Array . from ( new Set ( currentIds ) )
318
333
}
319
334
320
335
// //let sortedEntities: T[] = []
@@ -335,9 +350,16 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
335
350
// }
336
351
// }
337
352
// }
338
- let sortedEntities = ids // Array.from(new Set(state.ids as Id[]))
339
- . map ( ( id ) => entities [ id ] )
340
- . filter ( Boolean )
353
+ let sortedEntities : T [ ] = [ ]
354
+ for ( const id of ids ) {
355
+ const entity = currentEntities [ id ]
356
+ if ( entity ) {
357
+ sortedEntities . push ( entity )
358
+ }
359
+ }
360
+ // let sortedEntities = ids // Array.from(new Set(state.ids as Id[]))
361
+ // .map((id) => currentEntities[id])
362
+ // .filter(Boolean)
341
363
342
364
const wasPreviouslyEmpty = sortedEntities . length === 0
343
365
@@ -356,7 +378,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
356
378
357
379
// Insert/overwrite all new/updated
358
380
for ( const item of addedItems ) {
359
- entities [ selectId ( item ) ] = item
381
+ stateEntities [ selectId ( item ) ] = item
360
382
// console.log('Inserting: ', isDraft(item) ? current(item) : item)
361
383
if ( ! wasPreviouslyEmpty ) {
362
384
insert ( sortedEntities , item , comparer )
@@ -366,12 +388,18 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
366
388
if ( wasPreviouslyEmpty ) {
367
389
sortedEntities = addedItems . slice ( ) . sort ( comparer )
368
390
} else if ( updatedIds ?. size ) {
391
+ for ( const updatedId of updatedIds ) {
392
+ const item : T = currentEntities [ updatedId ]
393
+ // const currentIndex = sortedEntities.indexOf(item)
394
+ // const newIndex = findInsertIndex(sortedEntities, item, comparer)
395
+ // console.log('Item: ', updatedId, { currentIndex, newIndex })
396
+ }
369
397
sortedEntities . sort ( comparer )
370
398
}
371
399
372
400
const newSortedIds = sortedEntities . map ( selectId )
373
401
// console.log('New sorted IDs: ', newSortedIds)
374
- if ( ! areArraysEqual ( state . ids , newSortedIds ) ) {
402
+ if ( ! areArraysEqual ( currentIds , newSortedIds ) ) {
375
403
state . ids = newSortedIds
376
404
}
377
405
}
0 commit comments