Skip to content

Commit

Permalink
Remove binded events onBeforeUnmount.
Browse files Browse the repository at this point in the history
  • Loading branch information
francois2metz committed May 20, 2024
1 parent 5fa7f3a commit 5ca4e07
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions lib/components/marker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,20 @@ export default defineComponent({
const map = inject(mapSymbol)!,
marker = shallowRef<Marker>(),
markerRoot = ref(),
isMounted = ref(false);
isMounted = ref(false),
boundEvents = new Map();

function emitEvent(
eventName: "dragstart" | "drag" | "dragend",
additionalCb?: () => void,
) {
const fn = () => {
if (additionalCb) additionalCb();
emit(eventName);
};
marker.value!.on(eventName, fn);
boundEvents.set(eventName, fn);
}

provide(markerSymbol, marker);

Expand All @@ -106,15 +119,14 @@ export default defineComponent({
marker.value = new Marker(opts);
marker.value.setLngLat(props.coordinates).addTo(map.value!);

marker.value.on("dragstart", () => emit("dragstart"));
marker.value.on("drag", () => {
emitEvent("dragstart");
emitEvent("drag", () => {
emit("update:coordinates", marker.value?.getLngLat());
emit("drag");
});
marker.value.on("dragend", () => {
emitEvent("dragend", () => {
emit("update:coordinates", marker.value?.getLngLat());
emit("dragend");
});

isMounted.value = true;
});

Expand Down Expand Up @@ -143,7 +155,12 @@ export default defineComponent({
(v) => marker.value?.setRotationAlignment(v || "auto"),
);

onBeforeUnmount(() => marker.value?.remove());
onBeforeUnmount(() => {
boundEvents.forEach((fn, eventName) => {
marker.value?.off(eventName, fn);
});
marker.value?.remove();
});

return () => [
h(
Expand Down

0 comments on commit 5ca4e07

Please sign in to comment.