@@ -51,7 +51,7 @@ export function insert<T>(
51
51
52
52
export function createSortedStateAdapter < T , Id extends EntityId > (
53
53
selectId : IdSelector < T , Id > ,
54
- sort : Comparer < T > ,
54
+ comparer : Comparer < T > ,
55
55
) : EntityStateAdapter < T , Id > {
56
56
type R = DraftableEntityState < T , Id >
57
57
@@ -212,7 +212,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
212
212
213
213
function resortEntities ( state : R ) {
214
214
const allEntities = Object . values ( state . entities ) as T [ ]
215
- allEntities . sort ( sort )
215
+ allEntities . sort ( comparer )
216
216
const newSortedIds = allEntities . map ( selectId )
217
217
const { ids } = state
218
218
if ( ! areArraysEqual ( ids , newSortedIds ) ) {
@@ -240,7 +240,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
240
240
const seenIds = new Set < Id > ( )
241
241
242
242
if ( addedItems . length ) {
243
- const newEntities = addedItems . slice ( ) . sort ( sort )
243
+ const newEntities = addedItems . slice ( ) . sort ( comparer )
244
244
245
245
// Insert/overwrite all new/updated
246
246
newEntities . forEach ( ( model ) => {
@@ -260,7 +260,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
260
260
continue
261
261
}
262
262
263
- const comparison = sort ( oldEntity , newEntity )
263
+ const comparison = comparer ( oldEntity , newEntity )
264
264
if ( comparison < 0 ) {
265
265
// Sort the existing item first
266
266
newSortedIds . push ( oldId )
@@ -296,7 +296,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
296
296
n ++
297
297
}
298
298
} else if ( updatedIds ) {
299
- oldEntities . sort ( sort )
299
+ oldEntities . sort ( comparer )
300
300
newSortedIds = oldEntities . map ( selectId )
301
301
}
302
302
@@ -319,8 +319,6 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
319
319
320
320
// //let sortedEntities: T[] = []
321
321
322
- // const wasPreviouslyEmpty = ids.length === 0
323
-
324
322
// let sortedEntities = ids // Array.from(new Set(state.ids as Id[]))
325
323
// .map((id) => entities[id])
326
324
// .filter(Boolean)
@@ -337,10 +335,12 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
337
335
// }
338
336
// }
339
337
// }
340
- const sortedEntities = ids // Array.from(new Set(state.ids as Id[]))
338
+ let sortedEntities = ids // Array.from(new Set(state.ids as Id[]))
341
339
. map ( ( id ) => entities [ id ] )
342
340
. filter ( Boolean )
343
341
342
+ const wasPreviouslyEmpty = sortedEntities . length === 0
343
+
344
344
// let oldIds = state.ids as Id[]
345
345
// // if (updatedIds) {
346
346
// // oldIds = oldIds.filter((id) => !updatedIds.has(id))
@@ -355,14 +355,18 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
355
355
// .filter(Boolean)
356
356
357
357
// Insert/overwrite all new/updated
358
- addedItems . forEach ( ( item ) => {
358
+ for ( const item of addedItems ) {
359
359
entities [ selectId ( item ) ] = item
360
360
// console.log('Inserting: ', isDraft(item) ? current(item) : item)
361
- insert ( sortedEntities , item , sort )
362
- } )
361
+ if ( ! wasPreviouslyEmpty ) {
362
+ insert ( sortedEntities , item , comparer )
363
+ }
364
+ }
363
365
364
- if ( updatedIds ?. size ) {
365
- sortedEntities . sort ( sort )
366
+ if ( wasPreviouslyEmpty ) {
367
+ sortedEntities = addedItems . slice ( ) . sort ( comparer )
368
+ } else if ( updatedIds ?. size ) {
369
+ sortedEntities . sort ( comparer )
366
370
}
367
371
368
372
const newSortedIds = sortedEntities . map ( selectId )
@@ -412,7 +416,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
412
416
}
413
417
414
418
// Now when we sort, things should be _close_ already, and fewer comparisons are needed.
415
- allEntities . sort ( sort )
419
+ allEntities . sort ( comparer )
416
420
417
421
const newSortedIds = allEntities . map ( selectId )
418
422
const { ids } = state
@@ -485,7 +489,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
485
489
// }
486
490
487
491
// Now when we sort, things should be _close_ already, and fewer comparisons are needed.
488
- allEntities . sort ( sort )
492
+ allEntities . sort ( comparer )
489
493
490
494
const newSortedIds = allEntities . map ( selectId )
491
495
const { ids } = state
@@ -532,7 +536,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
532
536
}
533
537
534
538
// Now when we sort, things should be _close_ already, and fewer comparisons are needed.
535
- allEntities . sort ( sort )
539
+ allEntities . sort ( comparer )
536
540
537
541
const newSortedIds = allEntities . map ( selectId )
538
542
const { ids } = state
0 commit comments