Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mlmoravek committed Jun 6, 2024
1 parent c41d6c7 commit ebcc09f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,7 @@ onBeforeMount(() => {
});
onMounted(() => {
if (shouldQueue.value) correctParent.value.innerHTML = "";
correctParent.value.insertAdjacentElement(
"afterbegin",
notificationRef.value.$el,
);
showNotice();
setAutoClose();
});
Expand All @@ -235,6 +231,14 @@ const shouldQueue = computed(() =>
: false,
);
function showNotice(): void {
if (shouldQueue.value) correctParent.value.innerHTML = "";
correctParent.value.insertAdjacentElement(
"afterbegin",
notificationRef.value.$el,
);
}
/** Set timer to auto close message */
function setAutoClose(): void {
if (!props.infinite) {
Expand Down
35 changes: 19 additions & 16 deletions packages/oruga/src/composables/useEventListener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
onBeforeUnmount,
onMounted,
watch,
getCurrentScope,
onScopeDispose,
type MaybeRefOrGetter,
type Ref,
type Component,
Expand Down Expand Up @@ -49,18 +50,18 @@ export function useEventListener(
});
};

const stop = (): void => {
if (typeof cleanup === "function") cleanup();
};

let watchStop;
let stopWatch;

if (typeof options?.trigger !== "undefined") {
watchStop = watch(options.trigger, (value) => {
// toggle listener
if (value) register();
else stop();
});
stopWatch = watch(
options.trigger,
(value) => {
// toggle listener
if (value) register();
else stop();
},
{ flush: "post" },
);
}

if (options?.immediate) register();
Expand All @@ -75,11 +76,13 @@ export function useEventListener(
});
}

// remove listener before unmounting
onBeforeUnmount(() => {
stop();
if (typeof watchStop === "function") watchStop();
});
const stop = (): void => {
// remove listener before unmounting
if (typeof stopWatch === "function") stopWatch();
if (typeof cleanup === "function") cleanup();
};

if (getCurrentScope()) onScopeDispose(stop);

return stop;
}

0 comments on commit ebcc09f

Please sign in to comment.