Skip to content

Commit 7d42280

Browse files
committed
fix(ol-source-vector): prevent frequent source re-creation
before, it caused extra renderings with nested computed's and problems with Vue@^3.4.15 closes #297
1 parent b1dedd0 commit 7d42280

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/components/sources/OlSourceVector.vue

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import VectorSource, { type Options } from "ol/source/Vector";
99
import type VectorLayer from "ol/layer/Vector";
1010
import type HeatmapLayer from "ol/layer/Heatmap";
1111
import type { Ref } from "vue";
12-
import { inject, watch, onMounted, onUnmounted, provide, computed } from "vue";
12+
import { inject, watch, onMounted, onUnmounted, provide, ref } from "vue";
1313
import type Geometry from "ol/geom/Geometry";
1414
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
1515
import {
@@ -38,38 +38,36 @@ const layer = heatmapLayer || vectorLayer;
3838
3939
const { properties } = usePropsAsObjectProperties(props);
4040
41-
const source = computed(() => new VectorSource(properties));
41+
const createSource = () => new VectorSource(properties);
42+
43+
let source = createSource();
4244
4345
useOpenLayersEvents(source, FEATURE_EVENTS);
4446
4547
const applySource = () => {
4648
layer?.value?.setSource(null);
47-
layer?.value?.setSource(source.value);
48-
layer?.value?.changed();
49+
source = createSource();
50+
layer?.value?.setSource(source);
4951
};
50-
watch(properties, () => {
51-
applySource();
52-
});
52+
watch(properties, () => applySource());
5353
5454
watch(
5555
() => layer?.value,
56-
() => {
57-
applySource();
58-
},
56+
() => applySource(),
5957
);
6058
6159
onMounted(() => {
62-
layer?.value?.setSource(source.value);
60+
layer?.value.setSource(source);
6361
});
6462
6563
onUnmounted(() => {
66-
layer?.value?.setSource(null);
64+
layer?.value.setSource(null);
6765
});
6866
69-
provide("vectorSource", source);
67+
provide("vectorSource", ref(source));
7068
7169
defineExpose({
7270
layer,
73-
source,
71+
source: ref(source),
7472
});
7573
</script>

0 commit comments

Comments
 (0)