@@ -1758,6 +1758,54 @@ export function loadGatewaySessionRow(
17581758 * avoiding excessive yielding overhead for small stores.
17591759 */
17601760const SESSIONS_LIST_YIELD_BATCH_SIZE = 10 ;
1761+ const SESSIONS_LIST_TOP_N_LIMIT = 200 ;
1762+
1763+ type SessionEntryPair = [ string , SessionEntry ] ;
1764+
1765+ function compareSessionEntryPairsByUpdatedAt ( a : SessionEntryPair , b : SessionEntryPair ) : number {
1766+ return ( b [ 1 ] ?. updatedAt ?? 0 ) - ( a [ 1 ] ?. updatedAt ?? 0 ) ;
1767+ }
1768+
1769+ function resolveSessionsListLimit (
1770+ opts : import ( "./protocol/index.js" ) . SessionsListParams ,
1771+ ) : number | undefined {
1772+ if ( typeof opts . limit !== "number" || ! Number . isFinite ( opts . limit ) ) {
1773+ return undefined ;
1774+ }
1775+ return Math . max ( 1 , Math . floor ( opts . limit ) ) ;
1776+ }
1777+
1778+ function selectNewestLimitedEntries (
1779+ entries : SessionEntryPair [ ] ,
1780+ limit : number ,
1781+ ) : SessionEntryPair [ ] {
1782+ const selected : SessionEntryPair [ ] = [ ] ;
1783+ for ( const entry of entries ) {
1784+ const insertAt = selected . findIndex (
1785+ ( candidate ) => compareSessionEntryPairsByUpdatedAt ( entry , candidate ) < 0 ,
1786+ ) ;
1787+ if ( insertAt >= 0 ) {
1788+ selected . splice ( insertAt , 0 , entry ) ;
1789+ if ( selected . length > limit ) {
1790+ selected . pop ( ) ;
1791+ }
1792+ } else if ( selected . length < limit ) {
1793+ selected . push ( entry ) ;
1794+ }
1795+ }
1796+ return selected ;
1797+ }
1798+
1799+ function sortAndLimitSessionEntries (
1800+ entries : SessionEntryPair [ ] ,
1801+ limit : number | undefined ,
1802+ ) : SessionEntryPair [ ] {
1803+ if ( limit !== undefined && limit <= SESSIONS_LIST_TOP_N_LIMIT ) {
1804+ return selectNewestLimitedEntries ( entries , limit ) ;
1805+ }
1806+ const sorted = entries . toSorted ( compareSessionEntryPairsByUpdatedAt ) ;
1807+ return limit === undefined ? sorted : sorted . slice ( 0 , limit ) ;
1808+ }
17611809
17621810export function filterAndSortSessionEntries ( params : {
17631811 store : Record < string , SessionEntry > ;
@@ -1829,8 +1877,7 @@ export function filterAndSortSessionEntries(params: {
18291877 return true ;
18301878 }
18311879 return entry ?. label === label ;
1832- } )
1833- . toSorted ( ( a , b ) => ( b [ 1 ] ?. updatedAt ?? 0 ) - ( a [ 1 ] ?. updatedAt ?? 0 ) ) ;
1880+ } ) ;
18341881
18351882 if ( search ) {
18361883 entries = entries . filter ( ( [ key , entry ] ) => {
@@ -1852,12 +1899,7 @@ export function filterAndSortSessionEntries(params: {
18521899 entries = entries . filter ( ( [ , entry ] ) => ( entry ?. updatedAt ?? 0 ) >= cutoff ) ;
18531900 }
18541901
1855- if ( typeof opts . limit === "number" && Number . isFinite ( opts . limit ) ) {
1856- const limit = Math . max ( 1 , Math . floor ( opts . limit ) ) ;
1857- entries = entries . slice ( 0 , limit ) ;
1858- }
1859-
1860- return entries ;
1902+ return sortAndLimitSessionEntries ( entries , resolveSessionsListLimit ( opts ) ) ;
18611903}
18621904
18631905export function listSessionsFromStore ( params : {
0 commit comments