Skip to content

Commit a9a1c99

Browse files
committed
fix(ol-source-xyz): watch for changed props
closes #359
1 parent 0363578 commit a9a1c99

File tree

4 files changed

+26
-33
lines changed

4 files changed

+26
-33
lines changed

Diff for: src/components/sources/OlSourceXYZ.vue

+1-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { inject } from "vue";
88
import type TileLayer from "ol/layer/Tile";
99
import type { ImageTile } from "ol";
1010
import { TILE_SOURCE_EVENTS } from "@/composables/useOpenLayersEvents";
11-
import type { TileGrid } from "ol/tilegrid";
1211
import useSource from "@/composables/useSource";
1312
1413
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
@@ -37,15 +36,7 @@ const props = withDefaults(defineProps<Options>(), {
3736
3837
const layer = inject<Ref<TileLayer<XYZ>> | null>("tileLayer");
3938
40-
const { source } = useSource(
41-
XYZ,
42-
layer,
43-
{
44-
...props,
45-
tileGrid: props.tileGrid as TileGrid | undefined,
46-
},
47-
TILE_SOURCE_EVENTS,
48-
);
39+
const { source } = useSource(XYZ, layer, props, TILE_SOURCE_EVENTS);
4940
5041
defineExpose({
5142
layer,

Diff for: src/components/sources/OlSourceXyz.vue

+1-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { inject } from "vue";
88
import type TileLayer from "ol/layer/Tile";
99
import type { ImageTile } from "ol";
1010
import { TILE_SOURCE_EVENTS } from "@/composables/useOpenLayersEvents";
11-
import type { TileGrid } from "ol/tilegrid";
1211
import useSource from "@/composables/useSource";
1312
1413
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
@@ -37,15 +36,7 @@ const props = withDefaults(defineProps<Options>(), {
3736
3837
const layer = inject<Ref<TileLayer<XYZ>> | null>("tileLayer");
3938
40-
const { source } = useSource(
41-
XYZ,
42-
layer,
43-
{
44-
...props,
45-
tileGrid: props.tileGrid as TileGrid | undefined,
46-
},
47-
TILE_SOURCE_EVENTS,
48-
);
39+
const { source } = useSource(XYZ, layer, props, TILE_SOURCE_EVENTS);
4940
5041
defineExpose({
5142
layer,

Diff for: src/composables/useSource.ts

+23-13
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,32 @@ export default function useSource<T extends Source>(
2020
props: ConstructorParameters<typeof SourceClass>[0],
2121
eventsToHandle: string[] = [],
2222
) {
23-
const properties = usePropsAsObjectProperties({
24-
...props,
25-
...(props.projection
26-
? {
27-
projection: projectionFromProperties(
28-
props.projection as ProjectionLike,
29-
),
30-
}
31-
: {}),
32-
});
23+
function createSource() {
24+
const properties = usePropsAsObjectProperties({
25+
...props,
26+
...(props.projection
27+
? {
28+
projection: projectionFromProperties(
29+
props.projection as ProjectionLike,
30+
),
31+
}
32+
: {}),
33+
});
3334

34-
const source = shallowRef(new SourceClass(properties));
35+
return shallowRef(new SourceClass(properties));
36+
}
37+
38+
let source = createSource();
3539

36-
useOpenLayersEvents(source, eventsToHandle);
40+
const { updateOpenLayersEventHandlers } = useOpenLayersEvents(
41+
source,
42+
eventsToHandle,
43+
);
3744

3845
function updateSource() {
46+
source = createSource();
47+
updateOpenLayersEventHandlers();
48+
layer?.value?.setSource(null);
3949
layer?.value?.setSource(source.value);
4050
}
4151

@@ -49,7 +59,7 @@ export default function useSource<T extends Source>(
4959

5060
watch(() => layer, updateSource);
5161

52-
watch(() => properties, updateSource, { deep: true, immediate: true });
62+
watch(() => props, updateSource, { deep: true, immediate: true });
5363

5464
return {
5565
source,

Diff for: src/demos/XYZSourceDemo.vue

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</option>
1010
</select>
1111
</form>
12+
{{ selected }}
1213
<ol-map
1314
:loadTilesWhileAnimating="true"
1415
:loadTilesWhileInteracting="true"

0 commit comments

Comments
 (0)