Skip to content

Commit 5decf06

Browse files
fix: layers get duplicated when changing dashboard filter value (#1876)
--------- Co-authored-by: Antoine Hurard <antoine.reliefapps@gmail.com>
1 parent f42683e commit 5decf06

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

libs/shared/src/lib/components/ui/map/map.component.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import {
5252
isEqual,
5353
flatMapDeep,
5454
} from 'lodash';
55-
import { debounceTime, takeUntil } from 'rxjs';
55+
import { BehaviorSubject, concatMap, debounceTime, takeUntil } from 'rxjs';
5656
import { MapPopupService } from './map-popup/map-popup.service';
5757
import { Platform } from '@angular/cdk/platform';
5858
import { ContextService } from '../../../services/context/context.service';
@@ -152,6 +152,9 @@ export class MapComponent
152152
private basemapTree: L.Control.Layers.TreeObject[][] = [];
153153
private overlaysTree: L.Control.Layers.TreeObject[][] = [];
154154

155+
/** Refreshing layers. When true, should prevent layers to be duplicated */
156+
private refreshingLayers = new BehaviorSubject<boolean>(true);
157+
155158
/**
156159
* Map widget component
157160
*
@@ -253,10 +256,27 @@ export class MapComponent
253256
});
254257
//}
255258
}, 1000);
259+
/**
260+
* Keep checking until filters are applied in order to apply next one
261+
*/
262+
const loadNextFilters = (): Promise<void> => {
263+
const checkAgain = (resolve: () => void) => {
264+
if (this.refreshingLayers.getValue()) {
265+
resolve();
266+
} else {
267+
setTimeout(() => checkAgain(resolve), 100);
268+
}
269+
};
270+
return new Promise(checkAgain);
271+
};
256272

257273
// Listen to dashboard filters changes
258274
this.contextService.filter$
259-
.pipe(debounceTime(500), takeUntil(this.destroy$))
275+
.pipe(
276+
debounceTime(500),
277+
concatMap(() => loadNextFilters()),
278+
takeUntil(this.destroy$)
279+
)
260280
.subscribe(() => {
261281
this.filterLayers();
262282
});
@@ -950,6 +970,7 @@ export class MapComponent
950970
if (isEqual(filters, this.appliedDashboardFilters)) {
951971
return;
952972
}
973+
this.refreshingLayers.next(false);
953974
this.appliedDashboardFilters = filters;
954975
const { layers: layersToGet, controls } = this.extractSettings();
955976

@@ -1000,6 +1021,8 @@ export class MapComponent
10001021
flatten(this.overlaysTree)
10011022
);
10021023
}
1024+
1025+
this.refreshingLayers.next(true);
10031026
}
10041027

10051028
/**

0 commit comments

Comments
 (0)