Skip to content

Commit

Permalink
perf(pop-up): Reduce usage of getLayerForFeature
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipLeitner committed Nov 19, 2024
1 parent 8cbe4fc commit 02e27df
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
54 changes: 31 additions & 23 deletions projects/hslayers/common/query-popup/query-popup-base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {ReplaySubject} from 'rxjs';
import {Feature} from 'ol';
import {Geometry} from 'ol/geom';

import {HsFeatureLayer} from './query-popup.service.model';
import {HsMapService} from 'hslayers-ng/services/map';
import {HsPanelItem} from 'hslayers-ng/common/panels';
import {HsQueryPopupData} from './popup-data';
import {HsQueryPopupWidgetContainerService} from './query-popup-widget-container.service';
import {HsUtilsService} from 'hslayers-ng/services/utils';
import {getPopUp, getTitle} from 'hslayers-ng/common/extensions';
import {getName, getPopUp, getTitle} from 'hslayers-ng/common/extensions';

@Injectable({
providedIn: 'root',
Expand All @@ -33,29 +34,36 @@ export class HsQueryPopupBaseService extends HsQueryPopupData {
this.zone.run(() => {
this.featuresUnderMouse = features;
if (this.featuresUnderMouse.length) {
const layersFound = this.hsUtilsService.removeDuplicates(
this.featuresUnderMouse.map((f) =>
this.hsMapService.getLayerForFeature(f),
),
'title',
const featuresByLayer = features.reduce(
(acc, feature) => {
const layer = this.hsMapService.getLayerForFeature(feature);
if (layer === undefined) {
return acc;
}
const popUp = getPopUp(layer);
if (popUp) {
const layerName = getName(layer);
if (!acc[layerName]) {
const needSpecialWidgets =
popUp.widgets || popUp.displayFunction;
acc[layerName] = {
layer: layer,
title: getTitle(layer),
features: [],
panelObserver: needSpecialWidgets
? new ReplaySubject<HsPanelItem>()
: undefined,
};
}
acc[layerName].features.push(feature);
}
return acc;
},
{} as {[key: string]: HsFeatureLayer},
);
this.featureLayersUnderMouse = layersFound
.filter((l) => getPopUp(l)) //Only list the layers which have popUp defined
.map((l) => {
const needSpecialWidgets =
getPopUp(l)?.widgets || getPopUp(l)?.displayFunction;
const layer = {
title: getTitle(l),
layer: l,
features: this.featuresUnderMouse.filter(
(f) => this.hsMapService.getLayerForFeature(f) == l,
),
panelObserver: needSpecialWidgets
? new ReplaySubject<HsPanelItem>()
: undefined,
};
return layer;
});

this.featureLayersUnderMouse = Object.values(featuresByLayer);

for (const layer of this.featureLayersUnderMouse) {
if (layer.panelObserver) {
const popupDef = getPopUp(layer.layer);
Expand Down
13 changes: 3 additions & 10 deletions projects/hslayers/common/query-popup/query-popup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {HsMapService} from 'hslayers-ng/services/map';
import {HsQueryPopupServiceModel} from './query-popup.service.model';
import {HsQueryPopupWidgetContainerService} from './query-popup-widget-container.service';
import {getFeatures} from 'hslayers-ng/common/extensions';
import {getPopUp} from 'hslayers-ng/common/extensions';

@Component({
selector: 'hs-query-popup',
Expand Down Expand Up @@ -71,16 +70,10 @@ export class HsQueryPopupComponent
if (this.data.service == undefined) {
return DISPLAY_NONE;
}
const featuresWithPopup = this.data.service.featuresUnderMouse.filter(
(f) => {
const layer = this.hsMapService.getLayerForFeature(f);
if (!layer) {
return DISPLAY_NONE;
}
return getPopUp(layer) != undefined;
},
const featureCount = this.data.service.featureLayersUnderMouse.reduce(
(acc, featureLayer) => acc + featureLayer.features.length,
0,
);
const featureCount = featuresWithPopup.length;
if (featureCount > 0) {
let tmpForHover: any[] = [];
this.data.service.featuresUnderMouse.forEach((feature) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {HsQueryPopupData} from './popup-data';

export type HsFeatureLayer = {
title: string;
feature: Feature<Geometry>[];
features: Feature<Geometry>[];
layer: VectorLayer<VectorSource<Feature>>;
panelObserver: ReplaySubject<HsPanelItem>;
};
Expand Down
3 changes: 2 additions & 1 deletion projects/hslayers/services/draw/draw.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export class HsDrawService extends HsDrawServiceParams {

this.hsEventBusService.vectorQueryFeatureSelection.subscribe((event) => {
this.selectedFeatures.push(event.feature);
if (getEditor(event.selector.getLayer(event.feature)).editable) {
const layer = event.selector.getLayer(event.feature);
if (getEditor(layer)?.editable) {
this.modify.setActive(true);
}
});
Expand Down
2 changes: 1 addition & 1 deletion projects/hslayers/services/query/query-vector.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class HsQueryVectorService {

this.hsEventBusService.vectorQueryFeatureSelection.subscribe((e) => {
if (e?.feature) {
const layer = this.hsMapService.getLayerForFeature(e.feature);
const layer = e.selector.getLayer(e.feature);
if (layer && getOnFeatureSelected(layer)) {
const originalFeature = this.getSelectedFeature(e.feature);
if (originalFeature) {
Expand Down

0 comments on commit 02e27df

Please sign in to comment.