Skip to content

Commit 1e58d66

Browse files
committed
Adding debounce for pulling new panel info
1 parent 14e431e commit 1e58d66

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

debug_toolbar/static/debug_toolbar/js/toolbar.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { $$, ajax, replaceToolbarState } from "./utils.js";
1+
import { $$, ajax, replaceToolbarState, debounce } from "./utils.js";
22

33
function onKeyDown(event) {
44
if (event.keyCode === 27) {
@@ -259,27 +259,21 @@ const djdt = {
259259
update_on_ajax() {
260260
const sidebar_url =
261261
document.getElementById("djDebug").dataset.sidebarUrl;
262+
const slowjax = debounce(ajax, 400);
262263

263264
const origOpen = XMLHttpRequest.prototype.open;
264265
XMLHttpRequest.prototype.open = function () {
265266
this.addEventListener("load", function () {
266-
if (
267-
this.responseURL !== "" &&
268-
this.responseURL.indexOf("__debug__") === -1
269-
) {
270-
let signed = this.getResponseHeader(
271-
"dj-toolbar-store-id-signature"
272-
);
273-
const store_id = this.getResponseHeader(
274-
"dj-toolbar-store-id"
275-
);
276-
if (signed !== null) {
277-
signed = encodeURIComponent(signed);
278-
const dest = `${sidebar_url}?signed=${signed}`;
279-
ajax(dest).then(function (data) {
280-
replaceToolbarState(store_id, data);
281-
});
282-
}
267+
let signed = this.getResponseHeader(
268+
"dj-toolbar-store-id-signature"
269+
);
270+
const store_id = this.getResponseHeader("dj-toolbar-store-id");
271+
if (signed !== null) {
272+
signed = encodeURIComponent(signed);
273+
const dest = `${sidebar_url}?signed=${signed}`;
274+
slowjax(dest).then(function (data) {
275+
replaceToolbarState(store_id, data);
276+
});
283277
}
284278
});
285279
origOpen.apply(this, arguments);

debug_toolbar/static/debug_toolbar/js/utils.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,20 @@ function replaceToolbarState(newStoreId, data) {
118118
});
119119
}
120120

121-
export { $$, ajax, ajaxForm, replaceToolbarState };
121+
function debounce(func, delay) {
122+
let timer = null;
123+
let resolves = [];
124+
125+
return function (...args) {
126+
clearTimeout(timer);
127+
timer = setTimeout(() => {
128+
const result = func(...args);
129+
resolves.forEach((r) => r(result));
130+
resolves = [];
131+
}, delay);
132+
133+
return new Promise((r) => resolves.push(r));
134+
};
135+
}
136+
137+
export { $$, ajax, ajaxForm, replaceToolbarState, debounce };

0 commit comments

Comments
 (0)