Skip to content

Commit 7c4bfe0

Browse files
committed
fix(ol-source-*): correctly handle events
closes #365
1 parent 45902e7 commit 7c4bfe0

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/composables/__tests__/useSource.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { shallowMount } from "@vue/test-utils";
44
import { Layer } from "ol/layer";
55
import useSource from "../useSource";
66
import { Source } from "ol/source";
7+
import BaseEvent from "ol/events/Event";
78

89
describe("useSource", () => {
910
function createComponent() {
@@ -43,4 +44,14 @@ describe("useSource", () => {
4344
expect(setSourceSpy).toBeCalledTimes(3);
4445
expect(setSourceSpy).toHaveBeenLastCalledWith(null);
4546
});
47+
48+
it("should pass through events", () => {
49+
const { wrapper } = createComponent();
50+
const listenerMockImpl = vi.fn();
51+
wrapper.vm.source.on("change", listenerMockImpl);
52+
wrapper.vm.source.changed();
53+
expect(listenerMockImpl).toHaveBeenCalledTimes(1);
54+
const changeEvents = wrapper.emitted().change[0] as unknown[];
55+
expect(changeEvents[0]).toBeInstanceOf(BaseEvent);
56+
});
4657
});

src/composables/useSource.ts

+8-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export default function useSource<T extends Source>(
2020
props: ConstructorParameters<typeof SourceClass>[0],
2121
eventsToHandle: string[] = [],
2222
) {
23-
function createSource() {
23+
const source = updateSource();
24+
25+
function updateSource() {
2426
const properties = usePropsAsObjectProperties({
2527
...props,
2628
...(props.projection
@@ -31,22 +33,11 @@ export default function useSource<T extends Source>(
3133
}
3234
: {}),
3335
});
34-
35-
return shallowRef(new SourceClass(properties));
36-
}
37-
38-
let source = createSource();
39-
40-
const { updateOpenLayersEventHandlers } = useOpenLayersEvents(
41-
source,
42-
eventsToHandle,
43-
);
44-
45-
function updateSource() {
46-
source = createSource();
47-
updateOpenLayersEventHandlers();
36+
const s = shallowRef(new SourceClass(properties));
37+
useOpenLayersEvents(s, eventsToHandle);
4838
layer?.value?.setSource(null);
49-
layer?.value?.setSource(source.value);
39+
layer?.value?.setSource(s.value);
40+
return s;
5041
}
5142

5243
function removeSource() {
@@ -59,7 +50,7 @@ export default function useSource<T extends Source>(
5950

6051
watch(() => layer, updateSource);
6152

62-
watch(() => props, updateSource, { deep: true, immediate: true });
53+
watch(() => props, updateSource, { deep: true });
6354

6455
return {
6556
source,

0 commit comments

Comments
 (0)