@@ -62,11 +62,7 @@ export function useCollection({ wrap }: UseCollectionProps) {
6262 itemMap . current . delete ( id ) ;
6363 }
6464 }
65-
66- const newItems = Array . from ( itemMap . current . values ( ) ) ;
67- itemsRef . current = sortBasedOnDOMPosition ( newItems ) ;
68- batchTimeout . current = null ;
69- } , 166 ) ;
65+ } , 44 ) ;
7066
7167 return ( ) => {
7268 itemMap . current . set ( item . id , {
@@ -80,7 +76,7 @@ export function useCollection({ wrap }: UseCollectionProps) {
8076
8177 return {
8278 registerItem,
83- item : ( id ?: string | null ) : Item | null => {
79+ item : useCallback ( ( id ?: string | null ) : Item | null => {
8480 if ( ! id ) return null ;
8581 let item = itemMap . current . get ( id ) ;
8682 if ( ! item ) {
@@ -90,36 +86,48 @@ export function useCollection({ wrap }: UseCollectionProps) {
9086 }
9187 }
9288 return item ?? null ;
93- } ,
94- getNext : ( current : string ) : Item | null => {
95- const index = itemsRef . current . findIndex ( ( { id } ) => id === current ) ;
96-
97- const newIndex = wrap
98- ? ( index + 1 ) % itemsRef . current . length
99- : Math . min ( index + 1 , itemsRef . current . length - 1 ) ;
100-
101- return itemsRef . current [ newIndex ] ?? null ;
102- } ,
103- getPrevious : ( current : string ) : Item | null => {
104- const index = itemsRef . current . findIndex ( ( { id } ) => id === current ) ;
105-
106- const newIndex = wrap
107- ? ( index - 1 + itemsRef . current . length ) % itemsRef . current . length
108- : Math . max ( index - 1 , 0 ) ;
109-
110- return itemsRef . current [ newIndex ] ?? null ;
111- } ,
112- getFirst : ( ) : Item | null => {
89+ } , [ ] ) ,
90+ getNext : useCallback (
91+ ( current : string ) : Item | null => {
92+ const index = itemsRef . current . findIndex ( ( { id } ) => id === current ) ;
93+
94+ const newIndex = wrap
95+ ? ( index + 1 ) % itemsRef . current . length
96+ : Math . min ( index + 1 , itemsRef . current . length - 1 ) ;
97+
98+ return itemsRef . current [ newIndex ] ?? null ;
99+ } ,
100+ [ wrap ] ,
101+ ) ,
102+ getPrevious : useCallback (
103+ ( current : string ) : Item | null => {
104+ const index = itemsRef . current . findIndex ( ( { id } ) => id === current ) ;
105+
106+ const newIndex = wrap
107+ ? ( index - 1 + itemsRef . current . length ) % itemsRef . current . length
108+ : Math . max ( index - 1 , 0 ) ;
109+
110+ return itemsRef . current [ newIndex ] ?? null ;
111+ } ,
112+ [ wrap ] ,
113+ ) ,
114+ getFirst : useCallback ( ( ) : Item | null => {
113115 return itemsRef . current [ 0 ] ?? null ;
114- } ,
115- getLast : ( ) : Item | null => {
116+ } , [ ] ) ,
117+ getLast : useCallback ( ( ) : Item | null => {
116118 return itemsRef . current [ itemsRef . current . length - 1 ] ?? null ;
117- } ,
118- getIndex : ( current : string ) : number => {
119+ } , [ ] ) ,
120+ getIndex : useCallback ( ( current : string ) : number => {
119121 return itemsRef . current . findIndex ( ( { id } ) => id === current ) ;
120- } ,
121- itemAt : ( index : number ) : Item | null => {
122+ } , [ ] ) ,
123+ itemAt : useCallback ( ( index : number ) : Item | null => {
122124 return itemsRef . current [ index ] ?? null ;
123- } ,
125+ } , [ ] ) ,
126+ sortItems : useCallback ( ( ) => {
127+ const newItems = Array . from (
128+ itemMap . current . values ( ) . filter ( ( item ) => ! item . stale ) ,
129+ ) ;
130+ itemsRef . current = sortBasedOnDOMPosition ( newItems ) ;
131+ } , [ ] ) ,
124132 } ;
125133}
0 commit comments