@@ -280,20 +280,20 @@ function isPlainObject(obj: object) {
280280 ) ;
281281}
282282
283- function debounce < T , R > ( callback : ( value : T ) => Promise < R > ) : ( value : T ) => Promise < R > {
283+ function throttle < T , R > ( callback : ( value : T ) => Promise < R > ) : ( value : T ) => Promise < R > {
284284 let timeout : ReturnType < typeof setTimeout > | undefined ;
285285 let current : Promise < R > | undefined ;
286286 let resolve : ( value : R ) => void ;
287287 let reject : ( value : unknown ) => void ;
288288
289289 return value => {
290- if ( timeout ) {
291- clearTimeout ( timeout ) ;
290+ if ( ! timeout ) {
291+ timeout = setTimeout ( ( ) => {
292+ callback ( value ) . then ( resolve , reject ) ;
293+ current = undefined ;
294+ timeout = undefined ;
295+ } ) ;
292296 }
293- timeout = setTimeout ( ( ) => {
294- callback ( value ) . then ( resolve , reject ) ;
295- current = undefined ;
296- } ) ;
297297 if ( ! current ) {
298298 current = new Promise ( ( res , rej ) => {
299299 resolve = res ;
@@ -308,7 +308,7 @@ export function batchedQuery<Query, Data, Return>(
308308 callback : ( queries : Query [ ] ) => Promise < Data > ,
309309 lookup : ( data : Data , query : Query , index : number ) => Return
310310) : ( query : Query ) => Promise < Return > {
311- const debounced = debounce ( async ( queries : Query [ ] ) => {
311+ const debounced = throttle ( async ( queries : Query [ ] ) => {
312312 const currentQueries = [ ...queries ] ;
313313 queries . length = 0 ;
314314 const result = await callback ( currentQueries ) ;
0 commit comments