Skip to content

Commit

Permalink
Rerun search when moving map, with a debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJackson-Oslo committed Sep 26, 2024
1 parent a732f4e commit 20418be
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/javascript/controllers/mapbox_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class extends Controller {
this.markers = {};
this.map = false;
this.resizeObserver = false;
this.reloadMapDebouncer = false;
}

connect() {
Expand Down Expand Up @@ -172,6 +173,7 @@ export default class extends Controller {
loadPositionOn(event) {
this.map.on(event, () => {
this.searchThisAreaTarget.classList.remove('hidden');
this.deboounceReloadMap(500);
});
}

Expand All @@ -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]
Expand Down

0 comments on commit 20418be

Please sign in to comment.