From 07ab8293312d7fd15ef133156d89efcaa047c31d Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Fri, 10 Nov 2023 14:17:40 +0100 Subject: [PATCH 1/2] Add debounce on reload-listener.js --- .../static/django-browser-reload/reload-listener.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/django_browser_reload/static/django-browser-reload/reload-listener.js b/src/django_browser_reload/static/django-browser-reload/reload-listener.js index de6bd5b..fb8892a 100644 --- a/src/django_browser_reload/static/django-browser-reload/reload-listener.js +++ b/src/django_browser_reload/static/django-browser-reload/reload-listener.js @@ -8,13 +8,20 @@ if (!window.SharedWorker) { console.debug('😭 django-browser-reload cannot work in this browser.') } else { + function debounce(func, timeout = 300){ + let timer; + return (...args) => { + clearTimeout(timer); + timer = setTimeout(() => { func.apply(this, args); }, timeout); + }; + } const worker = new SharedWorker(workerScriptPath, { name: 'django-browser-reload' }) worker.port.addEventListener('message', (event) => { if (event.data === 'Reload') { - location.reload() + debounce(() => location.reload()); } }) From be3f72bd9e834534848a5625f958cf9c75013922 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 10 Nov 2023 13:18:27 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../static/django-browser-reload/reload-listener.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/django_browser_reload/static/django-browser-reload/reload-listener.js b/src/django_browser_reload/static/django-browser-reload/reload-listener.js index fb8892a..11eba12 100644 --- a/src/django_browser_reload/static/django-browser-reload/reload-listener.js +++ b/src/django_browser_reload/static/django-browser-reload/reload-listener.js @@ -8,12 +8,12 @@ if (!window.SharedWorker) { console.debug('😭 django-browser-reload cannot work in this browser.') } else { - function debounce(func, timeout = 300){ - let timer; + function debounce (func, timeout = 300) { + let timer return (...args) => { - clearTimeout(timer); - timer = setTimeout(() => { func.apply(this, args); }, timeout); - }; + clearTimeout(timer) + timer = setTimeout(() => { func.apply(this, args) }, timeout) + } } const worker = new SharedWorker(workerScriptPath, { name: 'django-browser-reload' @@ -21,7 +21,7 @@ worker.port.addEventListener('message', (event) => { if (event.data === 'Reload') { - debounce(() => location.reload()); + debounce(() => location.reload()) } })