Skip to content

Commit

Permalink
fix: ensure onMounted and inject is only called in setup context
Browse files Browse the repository at this point in the history
closes #385, #382
  • Loading branch information
d-koppenhagen committed Oct 2, 2024
1 parent 47df0a1 commit 256dd87
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/composables/useOpenLayersEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "vue";
import type BaseObject from "ol/Object";
import type { EventTypes } from "ol/Observable";
import type { Vue3OpenlayersGlobalOptions } from "@/types";

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

Expand Down Expand Up @@ -47,7 +48,6 @@ export const FEATURE_EVENTS = [
"removefeature",
];

// Define the composable function
export function useOpenLayersEvents(
feature:
| BaseObject
Expand All @@ -57,7 +57,12 @@ export function useOpenLayersEvents(
eventNames: string[],
) {
const instance = getCurrentInstance();
const globalOptions = inject("ol-options");
let globalOptions: Vue3OpenlayersGlobalOptions = {
debug: false,
};
if (instance) {
globalOptions = inject("ol-options");
}

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

onMounted(() => {
updateOpenLayersEventHandlers();
});
if (instance) {
onMounted(() => {
updateOpenLayersEventHandlers();
});
}

return {
updateOpenLayersEventHandlers,
Expand Down
17 changes: 15 additions & 2 deletions src/composables/usePropsAsObjectProperties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { inject, reactive, type UnwrapNestedRefs, toRefs } from "vue";
import {
inject,
reactive,
type UnwrapNestedRefs,
toRefs,
getCurrentInstance,
} from "vue";
import type { Vue3OpenlayersGlobalOptions } from "@/types";

type OlClassOptions<T> = T extends { styles: infer S }
? { style: S } & Omit<T, "styles">
Expand All @@ -22,7 +29,13 @@ function checkAndUpdateStylePropDef<T extends Record<string, unknown>>(
export default function usePropsAsObjectProperties<
T extends Record<string, unknown>,
>(props: T): UnwrapNestedRefs<OlClassOptions<T>> {
const globalOptions = inject("ol-options");
const instance = getCurrentInstance();
let globalOptions: Vue3OpenlayersGlobalOptions = {
debug: false,
};
if (instance) {
globalOptions = inject("ol-options");
}

const revisedProps = checkAndUpdateStylePropDef<T>(props);

Expand Down

0 comments on commit 256dd87

Please sign in to comment.