Skip to content

Commit

Permalink
⚡ Improve many episodes and webtoons loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Jun 17, 2024
1 parent de49b94 commit c6830c8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
46 changes: 46 additions & 0 deletions components/utils/VisibilityObserver.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script setup lang="ts">
import {ref, onMounted, onUnmounted} from "vue";
const emit = defineEmits(["on-display"]);
const element = ref(null);
let hasIntersected = false;
function handleIntersect(entries: any[]){
entries.forEach(entry => {
if (entry.isIntersecting && !hasIntersected){
hasIntersected = true;
emit("on-display");
if (observer && element.value){
observer.unobserve(element.value);
}
}
});
}
let observer: any;
onMounted(() => {
observer = new IntersectionObserver(handleIntersect, {
threshold: 0,
});
if (element.value)
observer.observe(element.value);
});
onUnmounted(() => {
if (element.value)
observer.unobserve(element.value);
});
</script>

<template>
<div ref="element">
<slot/>
</div>
</template>

<style scoped>
</style>
13 changes: 11 additions & 2 deletions components/webtoons/episodes/EpisodeList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {getEpisodes, getWebtoon} from "~/utils/api";
import EpisodeItem from "~/components/webtoons/episodes/EpisodeItem.vue";
import {Skeleton} from "~/components/ui/skeleton";
import EpisodeSkeleton from "~/components/webtoons/episodes/EpisodeSkeleton.vue";
import VisibilityObserver from "~/components/utils/VisibilityObserver.vue";
const title = ref("");
const episodes = ref<any[]>([]);
const maxIndex = ref<number>(30);
const id = useRoute().params.id as any as number;
Expand All @@ -22,6 +24,10 @@ function loadTitle(){
});
}
function increaseMaxIndex(){
maxIndex.value += 30;
}
onMounted(() => {
loadTitle();
loadEpisodes();
Expand All @@ -42,8 +48,11 @@ onMounted(() => {
<Separator/>
</div>
</div>
<div v-for="episode in episodes" :key="episode.id">
<EpisodeItem :episode="episode"/>
<div v-for="episode in episodes.slice(0, maxIndex)" :key="episode.id">
<EpisodeItem v-if="episodes.indexOf(episode) < maxIndex - 1" :episode="episode"/>
<VisibilityObserver v-else @on-display="increaseMaxIndex">
<EpisodeItem :episode="episode"/>
</VisibilityObserver>
<Separator/>
</div>
</div>
Expand Down
13 changes: 11 additions & 2 deletions components/webtoons/webtoons/WebtoonList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import {ref, computed, onMounted} from "vue";
import {getWebtoons} from "~/utils/api";
import WebtoonItem from "~/components/webtoons/webtoons/WebtoonItem.vue";
import WebtoonSkeleton from "~/components/webtoons/webtoons/WebtoonSkeleton.vue";
import VisibilityObserver from "~/components/utils/VisibilityObserver.vue";
const search = ref("");
const webtoons = ref<any[]>([]);
const maxIndex = ref<number>(30);
function clearSearch(){
search.value = "";
}
function increaseMaxIndex(){
maxIndex.value += 30;
}
onMounted(async() => {
const response = await getWebtoons();
webtoons.value = response.data;
Expand Down Expand Up @@ -52,8 +58,11 @@ const filteredWebtoons = computed(() => {
<Separator/>
</div>
</div>
<div v-for="webtoon in filteredWebtoons" :key="webtoon.id">
<WebtoonItem :webtoon="webtoon"/>
<div v-for="webtoon in filteredWebtoons.slice(0, maxIndex)" :key="webtoon.id">
<WebtoonItem v-if="filteredWebtoons.indexOf(webtoon) < maxIndex - 1" :webtoon="webtoon"/>
<VisibilityObserver v-else @on-display="increaseMaxIndex">
<WebtoonItem :webtoon="webtoon"/>
</VisibilityObserver>
<Separator/>
</div>
</div>
Expand Down

0 comments on commit c6830c8

Please sign in to comment.