Skip to content

Commit 46de77a

Browse files
committed
chore: optimize intersection observer usage
1 parent 526e23c commit 46de77a

5 files changed

Lines changed: 24 additions & 24 deletions

File tree

app/components/Comment/CommentArea.vue

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,15 @@ function pushComment(data: Comments) {
6868
6969
const commentsArea = ref<HTMLDivElement | null>(null)
7070
71-
const ob = useIntersectionObserver(
72-
commentsArea,
73-
async ([{ isIntersecting }]) => {
74-
if (isIntersecting) {
75-
await nextTick()
76-
init(props.id)
77-
ob.stop()
78-
}
71+
const ob = useIntersectionObserver(commentsArea, async (entries) => {
72+
if (entries.length === 0) return
73+
const isIntersecting = entries[0]!.isIntersecting
74+
if (isIntersecting) {
75+
await nextTick()
76+
init(props.id)
77+
ob.stop()
7978
}
80-
)
79+
})
8180
</script>
8281

8382
<style scoped lang="sass">

app/components/DeferLoad.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const error = ref(false)
2323
const imgRef = ref<HTMLImageElement | null>(null)
2424
2525
const ob = useIntersectionObserver(imgRef, async (entries) => {
26-
const isIntersecting = entries[0]?.isIntersecting
26+
if (entries.length === 0) return
27+
const isIntersecting = entries[0]!.isIntersecting
2728
if (isIntersecting) {
2829
await nextTick()
2930
loadImage()

app/components/ErrorPage.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ const props = defineProps<{
7474
}>()
7575
const msg = ref('')
7676
function randomMsg(): void {
77-
const newValue = msgList[Math.floor(Math.random() * msgList.length)]!
78-
if (newValue !== msg.value) {
79-
msg.value = newValue
80-
} else {
81-
randomMsg()
77+
let index = Math.floor(Math.random() * msgList.length)
78+
if (msgList[index] === msg.value) {
79+
index++
8280
}
81+
msg.value = msgList[index % msgList.length]!
8382
}
8483
8584
effect(() => {

app/components/ShowMore.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ const props = defineProps<{
1919
loading: boolean
2020
}>()
2121
22-
useIntersectionObserver(elRef, async ([{ isIntersecting }]) => {
22+
useIntersectionObserver(elRef, async (entries) => {
23+
if (entries.length === 0) return
24+
const isIntersecting = entries[0]!.isIntersecting
2325
if (isIntersecting) {
2426
await nextTick()
2527
props.method()

app/pages/artworks/[id].vue

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,14 @@ function addObserver(elementRef: Ref, cb: () => any) {
200200
await nextTick()
201201
if (illust.value?.illustId) {
202202
unWatch()
203-
const ob = useIntersectionObserver(
204-
elementRef.value,
205-
([{ isIntersecting }]) => {
206-
if (isIntersecting) {
207-
cb()
208-
ob.stop()
209-
}
203+
const ob = useIntersectionObserver(elementRef.value, (entries) => {
204+
if (entries.length === 0) return
205+
const isIntersecting = entries[0]!.isIntersecting
206+
if (isIntersecting) {
207+
cb()
208+
ob.stop()
210209
}
211-
)
210+
})
212211
}
213212
})
214213
}

0 commit comments

Comments
 (0)