@@ -2,7 +2,7 @@ import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef,
2
2
import { useDispatch , useSelector } from 'react-redux'
3
3
import cx from 'classnames'
4
4
import { useParams } from 'react-router-dom'
5
- import { debounce , isUndefined , reject } from 'lodash'
5
+ import { debounce , findIndex , isUndefined , reject } from 'lodash'
6
6
7
7
import {
8
8
EuiText ,
@@ -103,7 +103,7 @@ const KeyList = forwardRef((props: Props, ref) => {
103
103
return
104
104
}
105
105
106
- const { lastIndex , startIndex } = renderedRowsIndexesRef . current
106
+ const { startIndex , lastIndex } = renderedRowsIndexesRef . current
107
107
onRowsRendered ( startIndex , lastIndex )
108
108
rerender ( { } )
109
109
} , [ keysState . keys ] )
@@ -163,14 +163,16 @@ const KeyList = forwardRef((props: Props, ref) => {
163
163
nameString : bufferToString ( item . name )
164
164
} ) , [ ] )
165
165
166
- const onRowsRendered = debounce ( async ( startIndex : number , lastIndex : number ) => {
166
+ const onRowsRendered = ( startIndex : number , lastIndex : number ) => {
167
167
renderedRowsIndexesRef . current = { lastIndex, startIndex }
168
168
169
169
const newItems = bufferFormatRows ( startIndex , lastIndex )
170
170
171
171
getMetadata ( startIndex , lastIndex , newItems )
172
172
rerender ( { } )
173
- } , 100 )
173
+ }
174
+
175
+ const onRowsRenderedDebounced = debounce ( onRowsRendered , 100 )
174
176
175
177
const bufferFormatRows = ( startIndex : number , lastIndex : number ) : GetKeyInfoResponse [ ] => {
176
178
const newItems = bufferFormatRangeItems (
@@ -189,39 +191,25 @@ const KeyList = forwardRef((props: Props, ref) => {
189
191
const isSomeNotUndefined = ( { type, size, length } : GetKeyInfoResponse ) =>
190
192
! isUndefined ( type ) || ! isUndefined ( size ) || ! isUndefined ( length )
191
193
192
- const emptyItems = reject ( itemsInit , isSomeNotUndefined )
194
+ const firstEmptyItemIndex = findIndex ( itemsInit , ( item ) => ! isSomeNotUndefined ( item ) )
195
+ if ( firstEmptyItemIndex === - 1 ) return
193
196
194
- if ( ! emptyItems . length ) return
197
+ const emptyItems = reject ( itemsInit , isSomeNotUndefined )
195
198
196
199
dispatch ( fetchKeysMetadata (
197
200
emptyItems . map ( ( { name } ) => name ) ,
198
201
( loadedItems ) =>
199
- onSuccessFetchedMetadata ( {
200
- startIndex,
201
- lastIndex,
202
- loadedItems,
203
- isFirstEmpty : ! isSomeNotUndefined ( itemsInit [ 0 ] ) ,
204
- } ) ,
202
+ onSuccessFetchedMetadata ( startIndex + firstEmptyItemIndex , loadedItems ) ,
205
203
( ) => { rerender ( { } ) }
206
204
) )
207
205
}
208
206
209
- const onSuccessFetchedMetadata = ( data : {
207
+ const onSuccessFetchedMetadata = (
210
208
startIndex : number ,
211
- lastIndex : number ,
212
- isFirstEmpty : boolean
213
209
loadedItems : GetKeyInfoResponse [ ] ,
214
- } ) => {
215
- const {
216
- startIndex,
217
- lastIndex,
218
- isFirstEmpty,
219
- loadedItems,
220
- } = data
210
+ ) => {
221
211
const items = loadedItems . map ( formatItem )
222
- const startIndexDel = isFirstEmpty ? startIndex : lastIndex - items . length + 1
223
-
224
- itemsRef . current . splice ( startIndexDel , items . length , ...items )
212
+ itemsRef . current . splice ( startIndex , items . length , ...items )
225
213
226
214
rerender ( { } )
227
215
}
@@ -381,7 +369,7 @@ const KeyList = forwardRef((props: Props, ref) => {
381
369
setScrollTopPosition = { setScrollTopPosition }
382
370
hideFooter = { hideFooter }
383
371
onRowsRendered = { ( { overscanStartIndex, overscanStopIndex } ) =>
384
- onRowsRendered ( overscanStartIndex , overscanStopIndex ) }
372
+ onRowsRenderedDebounced ( overscanStartIndex , overscanStopIndex ) }
385
373
/>
386
374
</ div >
387
375
</ div >
0 commit comments