mirror of
https://github.com/nuxt/nuxt.git
synced 2025-03-03 22:55:42 +00:00
chore: revert export of useObserver
This commit is contained in:
parent
f3f594f7f9
commit
784c34fc91
@ -14,7 +14,6 @@ import { onNuxtReady } from '../composables/ready'
|
||||
import { navigateTo, resolveRouteObject, useRouter } from '../composables/router'
|
||||
import { useNuxtApp, useRuntimeConfig } from '../nuxt'
|
||||
import { cancelIdleCallback, requestIdleCallback } from '../compat/idle-callback'
|
||||
import { useIntersectionObserver } from '../utils'
|
||||
|
||||
// @ts-expect-error virtual file
|
||||
import { nuxtLinkDefaults } from '#build/nuxt.config.mjs'
|
||||
@ -308,7 +307,7 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
|
||||
let idleId: number
|
||||
let unobserve: (() => void) | null = null
|
||||
onMounted(() => {
|
||||
const observer = useIntersectionObserver()
|
||||
const observer = useObserver()
|
||||
onNuxtReady(() => {
|
||||
idleId = requestIdleCallback(() => {
|
||||
if (el?.value?.tagName) {
|
||||
@ -446,7 +445,48 @@ function applyTrailingSlashBehavior (to: string, trailingSlash: NuxtLinkOptions[
|
||||
}
|
||||
|
||||
// --- Prefetching utils ---
|
||||
type CallbackFn = () => void
|
||||
type ObserveFn = (element: Element, callback: CallbackFn) => () => void
|
||||
|
||||
function useObserver (): { observe: ObserveFn } | undefined {
|
||||
if (import.meta.server) { return { observe: () => () => {} } }
|
||||
|
||||
const nuxtApp = useNuxtApp()
|
||||
if (nuxtApp._observer) {
|
||||
return nuxtApp._observer
|
||||
}
|
||||
|
||||
let observer: IntersectionObserver | null = null
|
||||
const callbacks = new Map<Element, CallbackFn>()
|
||||
|
||||
const observe: ObserveFn = (element, callback) => {
|
||||
if (!observer) {
|
||||
observer = new IntersectionObserver((entries) => {
|
||||
for (const entry of entries) {
|
||||
const callback = callbacks.get(entry.target)
|
||||
const isVisible = entry.isIntersecting || entry.intersectionRatio > 0
|
||||
if (isVisible && callback) { callback() }
|
||||
}
|
||||
})
|
||||
}
|
||||
callbacks.set(element, callback)
|
||||
observer.observe(element)
|
||||
return () => {
|
||||
callbacks.delete(element)
|
||||
observer!.unobserve(element)
|
||||
if (callbacks.size === 0) {
|
||||
observer!.disconnect()
|
||||
observer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const _observer = nuxtApp._observer = {
|
||||
observe,
|
||||
}
|
||||
|
||||
return _observer
|
||||
}
|
||||
function isSlowConnection () {
|
||||
if (import.meta.server) { return }
|
||||
|
||||
|
@ -1,50 +1,4 @@
|
||||
import defu from 'defu'
|
||||
import { useNuxtApp } from './nuxt'
|
||||
|
||||
/** @since 3.9.0 */
|
||||
export function toArray<T> (value: T | T[]): T[] {
|
||||
return Array.isArray(value) ? value : [value]
|
||||
}
|
||||
|
||||
export type CallbackFn = () => void
|
||||
export type ObserveFn = (element: Element, callback: CallbackFn) => () => void
|
||||
|
||||
export function useIntersectionObserver (options?: Partial<IntersectionObserverInit>): { observe: ObserveFn } {
|
||||
if (import.meta.server) { return { observe: () => () => {} } }
|
||||
|
||||
const nuxtApp = useNuxtApp()
|
||||
if (nuxtApp._observer) {
|
||||
return nuxtApp._observer
|
||||
}
|
||||
|
||||
let observer: IntersectionObserver | null = null
|
||||
const callbacks = new Map<Element, CallbackFn>()
|
||||
|
||||
const observe: ObserveFn = (element, callback) => {
|
||||
if (!observer) {
|
||||
observer = new IntersectionObserver((entries) => {
|
||||
for (const entry of entries) {
|
||||
const callback = callbacks.get(entry.target)
|
||||
const isVisible = entry.isIntersecting || entry.intersectionRatio > 0
|
||||
if (isVisible && callback) { callback() }
|
||||
}
|
||||
}, defu(options, { root: null, rootMargin: '0px', threshold: 0 }))
|
||||
}
|
||||
callbacks.set(element, callback)
|
||||
observer.observe(element)
|
||||
return () => {
|
||||
callbacks.delete(element)
|
||||
observer!.unobserve(element)
|
||||
if (callbacks.size === 0) {
|
||||
observer!.disconnect()
|
||||
observer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const _observer = nuxtApp._observer = {
|
||||
observe,
|
||||
}
|
||||
|
||||
return _observer
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user