From 20418be2bbb309667f03042929313756bb3aacea Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 26 Sep 2024 10:18:41 +0200 Subject: [PATCH] Rerun search when moving map, with a debounce --- app/javascript/controllers/mapbox_controller.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/javascript/controllers/mapbox_controller.js b/app/javascript/controllers/mapbox_controller.js index 51253b5b..9f2c7f00 100644 --- a/app/javascript/controllers/mapbox_controller.js +++ b/app/javascript/controllers/mapbox_controller.js @@ -19,6 +19,7 @@ export default class extends Controller { this.markers = {}; this.map = false; this.resizeObserver = false; + this.reloadMapDebouncer = false; } connect() { @@ -172,6 +173,7 @@ export default class extends Controller { loadPositionOn(event) { this.map.on(event, () => { this.searchThisAreaTarget.classList.remove('hidden'); + this.deboounceReloadMap(500); }); } @@ -181,6 +183,17 @@ export default class extends Controller { this.searchThisAreaTarget.classList.add('hidden'); } + deboounceReloadMap(time) { + if (this.reloadMapDebouncer) { + clearTimeout(this.reloadMapDebouncer); + } + + this.reloadMapDebouncer = setTimeout(() => { + this.reloadMapDebouncer = false; + this.reloadPosition(); + }, time); + } + addMarker(space) { // If marker is already added, then just update that element, otherwise, create a new one const element = this.markers[space.id]