Skip to content

Commit 072a39a

Browse files
committed
pref: use IntersectionObserver API instead of scroll event
1 parent b3fd52a commit 072a39a

File tree

2 files changed

+34
-48
lines changed

2 files changed

+34
-48
lines changed

src/components/IllestWaveform.vue

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { ref, onMounted, watchEffect, onUnmounted } from 'vue'
33
import type { Ref } from 'vue'
44
import { Wave, AudioController } from '../modules'
55
import { formatSecond } from '../utils/format-time'
6-
import {
7-
lazyLoader,
8-
registerScrollHander,
9-
canelScrollHander,
10-
} from '../utils/lazy-load'
6+
import { lazyLoader, unobserve } from '../utils/lazy-load'
117
128
type CanvasLineCap = 'butt' | 'round' | 'square'
139
@@ -50,23 +46,14 @@ const __illestWaveformRef__ = ref<HTMLElement | null>(null)
5046
onMounted(async () => {
5147
if (props.lazy) {
5248
lazyLoader(__illestWaveformRef__.value as HTMLElement, lazyLoadHandler)
53-
registerScrollHander(
54-
__illestWaveformRef__.value as HTMLElement,
55-
lazyLoadHandler
56-
)
5749
watchEffect(async () => {
5850
if (renderTrigger.value) await init()
5951
})
6052
} else await init()
6153
})
6254
6355
onUnmounted(() => {
64-
if (props.lazy)
65-
canelScrollHander(
66-
__illestWaveformRef__.value as HTMLElement,
67-
lazyLoadHandler
68-
)
69-
56+
if (props.lazy) unobserve()
7057
audioController && audioController.pause()
7158
})
7259

src/utils/lazy-load.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
// Registers a scroll event listener on the document that checks if the given
2-
// element is in view and calls the provided handler if it is.
3-
function registerScrollHander(el: HTMLElement, handler: () => void) {
4-
const throttledHandler = throttle(() => lazyLoader(el, handler), 500)
5-
document.addEventListener('scroll', () => throttledHandler())
6-
}
1+
class LazyLoader {
2+
private el: HTMLElement
3+
private handler: () => void
4+
private intersectionObserver!: IntersectionObserver
5+
6+
constructor(el: HTMLElement, handler: () => void) {
7+
this.el = el
8+
this.handler = handler
9+
}
10+
11+
public observe() {
12+
const { el, handler } = this
13+
14+
const cb = (entries: IntersectionObserverEntry[]) => {
15+
const entry = entries[0]
16+
if (entry.intersectionRatio > 0) handler()
17+
}
718

8-
// Removes the scroll event listener on the document that was previously added
9-
// with registerScrollHandler.
10-
function canelScrollHander(el: HTMLElement, handler: () => void) {
11-
document.removeEventListener('scroll', () => lazyLoader(el, handler))
19+
this.intersectionObserver = new IntersectionObserver(cb)
20+
this.intersectionObserver.observe(el)
21+
}
22+
23+
public unobserve() {
24+
this.intersectionObserver.unobserve(this.el)
25+
}
1226
}
1327

14-
// Checks if the given element is in view, and if it is, calls the provided handler.
15-
function lazyLoader(el: HTMLElement, handler: () => void): void {
16-
const windowHeight: number = window.innerHeight
17-
const windowOffY: number = window.scrollY
18-
const elDistanceToTop: number =
19-
window.pageYOffset + el.getBoundingClientRect().top
20-
21-
if (
22-
elDistanceToTop >= windowOffY - windowHeight / 4 &&
23-
elDistanceToTop - windowOffY - windowHeight < windowHeight / 4
24-
)
25-
handler()
28+
let lz: LazyLoader
29+
30+
function lazyLoader(el: HTMLElement, handler: () => void) {
31+
lz = new LazyLoader(el, handler)
32+
lz.observe()
2633
}
2734

28-
// Throttle function that delays the execution of the given function until a certain time has passed since the last time it was called.
29-
function throttle(func: () => void, delay: number): () => void {
30-
let timeout: NodeJS.Timeout | undefined
31-
return () => {
32-
if (!timeout)
33-
timeout = setTimeout(() => {
34-
func()
35-
timeout = undefined
36-
}, delay)
37-
}
35+
function unobserve() {
36+
lz.unobserve()
3837
}
3938

40-
export { registerScrollHander, canelScrollHander, lazyLoader }
39+
export { lazyLoader, unobserve }

0 commit comments

Comments
 (0)