Skip to content

Commit

Permalink
useAsyncList: queue all the work upfront
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Feb 20, 2023
1 parent 7648347 commit 47e7b5b
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions packages/compose/src/hooks/use-async-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,16 @@ function useAsyncList< T >(
setCurrent( firstItems );

const asyncQueue = createQueue();
const append = ( nextIndex: number ) => () => {
if ( list.length <= nextIndex ) {
return;
}
flushSync( () => {
setCurrent( ( state ) => [
...state,
...list.slice( nextIndex, nextIndex + step ),
] );
for ( let i = firstItems.length; i < list.length; i += step ) {
asyncQueue.add( {}, () => {
flushSync( () => {
setCurrent( ( state ) => [
...state,
...list.slice( i, i + step ),
] );
} );
} );
asyncQueue.add( {}, append( nextIndex + step ) );
};
asyncQueue.add( {}, append( firstItems.length ) );
}

return () => asyncQueue.reset();
}, [ list ] );
Expand Down

0 comments on commit 47e7b5b

Please sign in to comment.