File tree Expand file tree Collapse file tree 3 files changed +22
-22
lines changed
debug_toolbar/static/debug_toolbar/js Expand file tree Collapse file tree 3 files changed +22
-22
lines changed Original file line number Diff line number Diff line change @@ -295,17 +295,15 @@ const djdt = {
295295 updateOnAjax ( ) {
296296 const sidebarUrl =
297297 document . getElementById ( "djDebug" ) . dataset . sidebarUrl ;
298- const slowjax = debounce ( ajax , 200 ) ;
299298
300- function handleAjaxResponse ( requestId ) {
299+ const handleAjaxResponse = debounce ( async ( requestId ) => {
301300 const encodedRequestId = encodeURIComponent ( requestId ) ;
302301 const dest = `${ sidebarUrl } ?request_id=${ encodedRequestId } ` ;
303- slowjax ( dest ) . then ( ( data ) => {
304- if ( djdt . needUpdateOnFetch ) {
305- replaceToolbarState ( encodedRequestId , data ) ;
306- }
307- } ) ;
308- }
302+ const data = await ajax ( dest ) ;
303+ if ( djdt . needUpdateOnFetch ) {
304+ replaceToolbarState ( encodedRequestId , data ) ;
305+ }
306+ } , 100 ) ;
309307
310308 // Patch XHR / traditional AJAX requests
311309 const origOpen = XMLHttpRequest . prototype . open ;
Original file line number Diff line number Diff line change @@ -121,20 +121,23 @@ export function replaceToolbarState(newRequestId, data) {
121121 }
122122}
123123
124- export function debounce ( func , delay ) {
125- let timer = null ;
126- let resolves = [ ] ;
127-
124+ /**
125+ * Debounce async functions.
126+ *
127+ * @param {Function } func - Function to be executed.
128+ * @param {number } timeout - Time to wait before executing function in milliseconds.
129+ * @returns {Function } - Debounced function.
130+ */
131+ export function debounce ( func , timeout ) {
132+ let timer ;
128133 return async ( ...args ) => {
129134 clearTimeout ( timer ) ;
130- timer = setTimeout ( ( ) => {
131- const result = func ( ...args ) ;
132- for ( const r of resolves ) {
133- r ( result ) ;
134- }
135- resolves = [ ] ;
136- } , delay ) ;
137-
138- return await new Promise ( ( r ) => resolves . push ( r ) ) ;
135+ return await new Promise ( ( resolve , reject ) => {
136+ timer = setTimeout ( ( ) => {
137+ Promise . resolve ( func . apply ( this , [ ...args ] ) )
138+ . then ( resolve )
139+ . catch ( reject ) ;
140+ } , timeout ) ;
141+ } ) ;
139142 } ;
140143}
Original file line number Diff line number Diff line change @@ -299,7 +299,6 @@ describe("utils.js", () => {
299299
300300 expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
301301 expect ( fn ) . toHaveBeenCalledWith ( "third" ) ;
302- expect ( results ) . toEqual ( [ "third" , "third" , "third" ] ) ;
303302 vi . useRealTimers ( ) ;
304303 } ) ;
305304 } ) ;
You can’t perform that action at this time.
0 commit comments