Skip to content

Commit

Permalink
Clear timers and event listeners before component unmounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Arooba-git committed Jul 16, 2024
1 parent 7d29163 commit 1fd0e52
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 12 deletions.
10 changes: 8 additions & 2 deletions docs/src/components/CodeExample.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import IconJavaScript from "./IconJavaScript.vue"
import IconSvelte from "./IconSvelte.vue"
import IconAngular from "./IconAngular.vue"
import IconNuxt from "./IconNuxt.vue"
import { computed, ref } from "vue"
import { computed, ref, onBeforeUnmount } from "vue";
import { vAutoAnimate } from "../../../src"
import "../../assets/prism.css"
const timeOutID = ref();
type LanguageOption =
| "react"
| "preact"
Expand Down Expand Up @@ -77,10 +79,14 @@ const copyStatus = ref(false)
function copyCode(value: string) {
window.navigator.clipboard.writeText(value)
copyStatus.value = true
setTimeout(() => {
timeOutID.value = setTimeout(() => {
copyStatus.value = false
}, 2000)
}
onBeforeUnmount(() => {
clearTimeout(timeOutID.value);
});
</script>

<template>
Expand Down
18 changes: 14 additions & 4 deletions docs/src/components/Navigation.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script setup lang="ts">
import { ref } from "vue"
import { ref, onBeforeUnmount } from "vue";
import IconDown from "./IconDown.vue"
import PoliteAd from "./PoliteAd.vue"
import { vAutoAnimate } from "../../../src/index"
const controller = new window.AbortController();
const show = ref(false)
const activeTitle = ref("Docs Navigation")
Expand All @@ -19,14 +21,22 @@ const handleClick = () => {
show.value = window.innerWidth >= 672
}
let resizeTimer
const resizeTimer = ref<NodeJS.Timeout | undefined>();
if (typeof window !== "undefined") {
show.value = window.innerWidth >= 672
clearTimeout(resizeTimer)
clearTimeout(resizeTimer.value)
window.addEventListener("resize", () => {
resizeTimer = setTimeout(applySizing, 200)
resizeTimer.value = setTimeout(applySizing, 200)
}, {
signal: controller?.signal
})
}
onBeforeUnmount(() => {
controller.abort();
clearTimeout(resizeTimer.value)
});
</script>

<template>
Expand Down
4 changes: 4 additions & 0 deletions docs/src/components/TheLogo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ onMounted(() => {
clearInterval(interval)
}
}, duration)
this.$once('hook:beforeUnmount', () => {
clearInterval(interval);
});
})
</script>

Expand Down
10 changes: 8 additions & 2 deletions docs/src/examples/cards/ActualCards.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script setup>
import { ref } from "vue"
import { ref, onBeforeUnmount } from "vue";
import { vAutoAnimate } from "../../../../src/index"
const timeOutID = ref();
const showForm = ref(false)
const cards = ref([
{
Expand All @@ -20,10 +22,14 @@ const cards = ref([
function createCard(card) {
cards.value.unshift(card)
setTimeout(() => {
timeOutID.value = setTimeout(() => {
showForm.value = false
}, 300)
}
onBeforeUnmount(() => {
clearTimeout(timeOutID.value);
});
</script>

<template>
Expand Down
11 changes: 9 additions & 2 deletions docs/src/examples/disable/ActualDisable.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script setup lang="ts">
import { ref } from "vue"
import { ref, onBeforeUnmount } from "vue";
import { useAutoAnimate } from "../../../../src/vue"
const intervalID = ref();
const balls = ref(["red", "green", "blue"])
const [parent, enable] = useAutoAnimate({ duration: 500 })
Expand All @@ -10,9 +12,14 @@ function toggle() {
isEnabled.value ? enable(false) : enable(true)
isEnabled.value = !isEnabled.value
}
setInterval(() => {
intervalID.value = setInterval(() => {
balls.value.push(balls.value.shift()!)
}, 600)
onBeforeUnmount(() => {
clearInterval(intervalID.value);
});
</script>

<template>
Expand Down
11 changes: 9 additions & 2 deletions docs/src/examples/disable/disable.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script setup>
import { ref } from "vue"
import { ref, onBeforeUnmount } from "vue";
import { useAutoAnimate } from "../../../../src/vue"
const intervalID = ref();
const balls = ref(["red", "green", "blue"])
const [parent, enable] = useAutoAnimate({ duration: 500 })
Expand All @@ -10,9 +12,14 @@ function toggle() {
isEnabled.value ? enable(false) : enable(true)
isEnabled.value = !isEnabled.value
}
setInterval(() => {
intervalID.value = setInterval(() => {
balls.value.push(balls.value.shift()!)
}, 600)
onBeforeUnmount(() => {
clearInterval(intervalID.value);
});
</script>

<template>
Expand Down

0 comments on commit 1fd0e52

Please sign in to comment.