Skip to content

Commit 256dd87

Browse files
committed
fix: ensure onMounted and inject is only called in setup context
closes #385, #382
1 parent 47df0a1 commit 256dd87

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

Diff for: src/composables/useOpenLayersEvents.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "vue";
1010
import type BaseObject from "ol/Object";
1111
import type { EventTypes } from "ol/Observable";
12+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
1213

1314
export const COMMON_EVENTS = ["change", "error", "propertychange"];
1415

@@ -47,7 +48,6 @@ export const FEATURE_EVENTS = [
4748
"removefeature",
4849
];
4950

50-
// Define the composable function
5151
export function useOpenLayersEvents(
5252
feature:
5353
| BaseObject
@@ -57,7 +57,12 @@ export function useOpenLayersEvents(
5757
eventNames: string[],
5858
) {
5959
const instance = getCurrentInstance();
60-
const globalOptions = inject("ol-options");
60+
let globalOptions: Vue3OpenlayersGlobalOptions = {
61+
debug: false,
62+
};
63+
if (instance) {
64+
globalOptions = inject("ol-options");
65+
}
6166

6267
function updateOpenLayersEventHandlers() {
6368
([...COMMON_EVENTS, ...eventNames] as EventTypes[]).forEach((eventName) => {
@@ -83,9 +88,11 @@ export function useOpenLayersEvents(
8388
});
8489
}
8590

86-
onMounted(() => {
87-
updateOpenLayersEventHandlers();
88-
});
91+
if (instance) {
92+
onMounted(() => {
93+
updateOpenLayersEventHandlers();
94+
});
95+
}
8996

9097
return {
9198
updateOpenLayersEventHandlers,

Diff for: src/composables/usePropsAsObjectProperties.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { inject, reactive, type UnwrapNestedRefs, toRefs } from "vue";
1+
import {
2+
inject,
3+
reactive,
4+
type UnwrapNestedRefs,
5+
toRefs,
6+
getCurrentInstance,
7+
} from "vue";
8+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
29

310
type OlClassOptions<T> = T extends { styles: infer S }
411
? { style: S } & Omit<T, "styles">
@@ -22,7 +29,13 @@ function checkAndUpdateStylePropDef<T extends Record<string, unknown>>(
2229
export default function usePropsAsObjectProperties<
2330
T extends Record<string, unknown>,
2431
>(props: T): UnwrapNestedRefs<OlClassOptions<T>> {
25-
const globalOptions = inject("ol-options");
32+
const instance = getCurrentInstance();
33+
let globalOptions: Vue3OpenlayersGlobalOptions = {
34+
debug: false,
35+
};
36+
if (instance) {
37+
globalOptions = inject("ol-options");
38+
}
2639

2740
const revisedProps = checkAndUpdateStylePropDef<T>(props);
2841

0 commit comments

Comments
 (0)