From ffb57ea61d6a767c30de41d109e3734f00c17181 Mon Sep 17 00:00:00 2001 From: Filip Leitner Date: Mon, 14 Oct 2024 12:59:38 +0200 Subject: [PATCH] fix(map-swipe): Swipe properties of layers not updating with layerManagerUpdates when mapSwipe panel is active closes #5332 --- .../components/map-swipe/map-swipe.service.ts | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/projects/hslayers/components/map-swipe/map-swipe.service.ts b/projects/hslayers/components/map-swipe/map-swipe.service.ts index 83b2befa1f..68c042500d 100644 --- a/projects/hslayers/components/map-swipe/map-swipe.service.ts +++ b/projects/hslayers/components/map-swipe/map-swipe.service.ts @@ -1,5 +1,6 @@ import {Injectable, NgZone} from '@angular/core'; -import {buffer, filter, first, map} from 'rxjs'; +import {Observable, merge} from 'rxjs'; +import {buffer, filter, first, map, switchMap, take} from 'rxjs/operators'; import {Layer} from 'ol/layer'; import {Source} from 'ol/source'; @@ -78,18 +79,28 @@ export class HsMapSwipeService { } }); - this.hsEventBusService.layerManagerUpdates + //Stream used to update layers while mapSwipe panel is active + const isMapSwipe$ = this.getLayerUpdateWithCurrentPanel().pipe( + filter(({panel}) => panel === 'mapSwipe'), + map(({layer}) => [layer]), + ); + //Stream used to update layers while mapSwipe panel is not active + const isNotMapSwipe$ = this.getLayerUpdateWithCurrentPanel().pipe( + filter(({panel}) => panel !== 'mapSwipe'), + map(({layer}) => layer), + //buffer layerManagerUpdates until mapSwipe panel is opened + buffer( + this.hsLayoutService.mainpanel$.pipe(filter((p) => p === 'mapSwipe')), + ), + ); + + merge(isMapSwipe$, isNotMapSwipe$) .pipe( - //Buffer layerManagerUpdates until mapSwipe panel is opened - buffer( - this.hsLayoutService.mainpanel$.pipe(filter((p) => p === 'mapSwipe')), - ), - //Do not accept empty array - filter((layers) => layers.length > 0), + filter((layers) => layers?.length > 0), map((layers) => this.removeDuplicateUpdates(layers as Layer[])), ) - .subscribe((filteredLayers) => { - this.fillSwipeLayers(filteredLayers); + .subscribe((layers) => { + this.fillSwipeLayers(layers); }); this.hsLayerEditorService.layerTitleChange.subscribe(({layer}) => { @@ -100,6 +111,25 @@ export class HsMapSwipeService { }); } + /** + * Get layer manager update along with the current mainPanel + */ + private getLayerUpdateWithCurrentPanel(): Observable<{ + layer: Layer; + panel: string; + }> { + return this.hsEventBusService.layerManagerUpdates.pipe( + //filter out nulls + filter((layer): layer is Layer => !!layer), + switchMap((layer) => + this.hsLayoutService.mainpanel$.pipe( + take(1), + map((panel) => ({layer, panel})), + ), + ), + ); + } + /** * Remove duplicate layerManagerUpdates to get one update per layer */