From 784c34fc9192013e025770bb05ae965173431c4e Mon Sep 17 00:00:00 2001 From: tbitw2549 Date: Fri, 16 Aug 2024 18:14:14 +0300 Subject: [PATCH] chore: revert export of useObserver --- packages/nuxt/src/app/components/nuxt-link.ts | 44 +++++++++++++++++- packages/nuxt/src/app/utils.ts | 46 ------------------- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-link.ts b/packages/nuxt/src/app/components/nuxt-link.ts index 0307d29c64..1a041bd1f7 100644 --- a/packages/nuxt/src/app/components/nuxt-link.ts +++ b/packages/nuxt/src/app/components/nuxt-link.ts @@ -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() + + 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 } diff --git a/packages/nuxt/src/app/utils.ts b/packages/nuxt/src/app/utils.ts index f889ba1239..72b096120b 100644 --- a/packages/nuxt/src/app/utils.ts +++ b/packages/nuxt/src/app/utils.ts @@ -1,50 +1,4 @@ -import defu from 'defu' -import { useNuxtApp } from './nuxt' - /** @since 3.9.0 */ export function toArray (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): { 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() - - 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 -}