Skip to content

Commit c00d992

Browse files
committed
fix(ol-animated-cluster-layer): prevent re-computation of cluster and layer in property changes
1 parent c48b446 commit c00d992

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

src/components/layers/OlAnimatedClusterLayer.vue

+44-22
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
</template>
66

77
<script setup lang="ts">
8-
import { inject, provide, onUnmounted, onMounted, watch, computed } from "vue";
8+
import {
9+
inject,
10+
provide,
11+
onUnmounted,
12+
onMounted,
13+
watch,
14+
shallowRef,
15+
} from "vue";
916
import { Cluster } from "ol/source";
1017
import { easeOut } from "ol/easing";
1118
import AnimatedCluster from "ol-ext/layer/AnimatedCluster";
@@ -54,34 +61,49 @@ const layerGroup = inject<LayerGroup | null>("layerGroup", null);
5461
5562
const { properties } = usePropsAsObjectProperties(props);
5663
57-
const clusterSource = computed(() => {
58-
return new Cluster({
64+
const clusterSource = shallowRef(
65+
new Cluster({
5966
distance: properties.distance,
6067
geometryFunction: (feature) => feature.getGeometry() as Point,
61-
});
62-
});
68+
}),
69+
);
70+
71+
useOpenLayersEvents(clusterSource, FEATURE_EVENTS);
6372
64-
const vectorLayer = computed(() => {
65-
return new AnimatedCluster({
73+
watch(
74+
() => properties.distance,
75+
(newValue) => {
76+
clusterSource.value.setDistance(newValue);
77+
},
78+
);
79+
80+
const vectorLayer = shallowRef(
81+
new AnimatedCluster({
6682
...properties,
6783
source: clusterSource.value,
68-
});
69-
});
70-
71-
useOpenLayersEvents(clusterSource, FEATURE_EVENTS);
84+
}),
85+
);
7286
73-
const source = computed(() => vectorLayer.value.getSource());
87+
watch(
88+
() => properties,
89+
(newValue) => {
90+
vectorLayer.value.setProperties(properties);
7491
75-
watch(properties, () => {
76-
vectorLayer.value.setProperties(properties);
77-
vectorLayer.value.changed();
92+
for (const key in newValue) {
93+
const keyInObj = key as keyof typeof newValue;
94+
if (newValue[keyInObj]) {
95+
vectorLayer.value.set(key, newValue[keyInObj]);
96+
}
97+
}
7898
79-
if (layerGroup) {
80-
const layerCollection = layerGroup.getLayers();
81-
layerCollection.push(vectorLayer.value);
82-
layerGroup.setLayers(layerCollection);
83-
}
84-
});
99+
if (layerGroup) {
100+
const layerCollection = layerGroup.getLayers();
101+
layerCollection.push(vectorLayer.value);
102+
layerGroup.setLayers(layerCollection);
103+
}
104+
},
105+
{ deep: true },
106+
);
85107
86108
onMounted(() => {
87109
map?.addLayer(vectorLayer.value);
@@ -93,7 +115,7 @@ onUnmounted(() => {
93115
map?.removeLayer(vectorLayer.value);
94116
});
95117
96-
provide("vectorLayer", source);
118+
provide("vectorLayer", clusterSource);
97119
provide("stylable", vectorLayer);
98120
99121
defineExpose({

0 commit comments

Comments
 (0)