@@ -51,7 +51,7 @@ export function insert<T>(
5151
5252export function createSortedStateAdapter < T , Id extends EntityId > (
5353 selectId : IdSelector < T , Id > ,
54- sort : Comparer < T > ,
54+ comparer : Comparer < T > ,
5555) : EntityStateAdapter < T , Id > {
5656 type R = DraftableEntityState < T , Id >
5757
@@ -212,7 +212,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
212212
213213 function resortEntities ( state : R ) {
214214 const allEntities = Object . values ( state . entities ) as T [ ]
215- allEntities . sort ( sort )
215+ allEntities . sort ( comparer )
216216 const newSortedIds = allEntities . map ( selectId )
217217 const { ids } = state
218218 if ( ! areArraysEqual ( ids , newSortedIds ) ) {
@@ -240,7 +240,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
240240 const seenIds = new Set < Id > ( )
241241
242242 if ( addedItems . length ) {
243- const newEntities = addedItems . slice ( ) . sort ( sort )
243+ const newEntities = addedItems . slice ( ) . sort ( comparer )
244244
245245 // Insert/overwrite all new/updated
246246 newEntities . forEach ( ( model ) => {
@@ -260,7 +260,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
260260 continue
261261 }
262262
263- const comparison = sort ( oldEntity , newEntity )
263+ const comparison = comparer ( oldEntity , newEntity )
264264 if ( comparison < 0 ) {
265265 // Sort the existing item first
266266 newSortedIds . push ( oldId )
@@ -296,7 +296,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
296296 n ++
297297 }
298298 } else if ( updatedIds ) {
299- oldEntities . sort ( sort )
299+ oldEntities . sort ( comparer )
300300 newSortedIds = oldEntities . map ( selectId )
301301 }
302302
@@ -319,8 +319,6 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
319319
320320 // //let sortedEntities: T[] = []
321321
322- // const wasPreviouslyEmpty = ids.length === 0
323-
324322 // let sortedEntities = ids // Array.from(new Set(state.ids as Id[]))
325323 // .map((id) => entities[id])
326324 // .filter(Boolean)
@@ -337,10 +335,12 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
337335 // }
338336 // }
339337 // }
340- const sortedEntities = ids // Array.from(new Set(state.ids as Id[]))
338+ let sortedEntities = ids // Array.from(new Set(state.ids as Id[]))
341339 . map ( ( id ) => entities [ id ] )
342340 . filter ( Boolean )
343341
342+ const wasPreviouslyEmpty = sortedEntities . length === 0
343+
344344 // let oldIds = state.ids as Id[]
345345 // // if (updatedIds) {
346346 // // oldIds = oldIds.filter((id) => !updatedIds.has(id))
@@ -355,14 +355,18 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
355355 // .filter(Boolean)
356356
357357 // Insert/overwrite all new/updated
358- addedItems . forEach ( ( item ) => {
358+ for ( const item of addedItems ) {
359359 entities [ selectId ( item ) ] = item
360360 // console.log('Inserting: ', isDraft(item) ? current(item) : item)
361- insert ( sortedEntities , item , sort )
362- } )
361+ if ( ! wasPreviouslyEmpty ) {
362+ insert ( sortedEntities , item , comparer )
363+ }
364+ }
363365
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 )
366370 }
367371
368372 const newSortedIds = sortedEntities . map ( selectId )
@@ -412,7 +416,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
412416 }
413417
414418 // Now when we sort, things should be _close_ already, and fewer comparisons are needed.
415- allEntities . sort ( sort )
419+ allEntities . sort ( comparer )
416420
417421 const newSortedIds = allEntities . map ( selectId )
418422 const { ids } = state
@@ -485,7 +489,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
485489 // }
486490
487491 // Now when we sort, things should be _close_ already, and fewer comparisons are needed.
488- allEntities . sort ( sort )
492+ allEntities . sort ( comparer )
489493
490494 const newSortedIds = allEntities . map ( selectId )
491495 const { ids } = state
@@ -532,7 +536,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
532536 }
533537
534538 // Now when we sort, things should be _close_ already, and fewer comparisons are needed.
535- allEntities . sort ( sort )
539+ allEntities . sort ( comparer )
536540
537541 const newSortedIds = allEntities . map ( selectId )
538542 const { ids } = state
0 commit comments