mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-07 17:32:31 +00:00
feat: provide exported function
This commit is contained in:
parent
624188a7cf
commit
f039dfd09f
@ -1,3 +1,64 @@
|
||||
import type {
|
||||
AllowedComponentProps,
|
||||
AnchorHTMLAttributes,
|
||||
ComputedRef,
|
||||
DefineComponent,
|
||||
InjectionKey, PropType,
|
||||
VNodeProps
|
||||
} from 'vue'
|
||||
import { computed, defineComponent, h, inject, onBeforeUnmount, onMounted, provide, ref, resolveComponent } from 'vue'
|
||||
import type { RouteLocation, RouteLocationRaw, Router, RouterLinkProps } from '#vue-router'
|
||||
import { hasProtocol, joinURL, parseQuery, parseURL, withTrailingSlash, withoutTrailingSlash } from 'ufo'
|
||||
import { preloadRouteComponents } from './composables/preload'
|
||||
import { onNuxtReady } from './composables/ready'
|
||||
import { navigateTo, useRouter } from './composables/router'
|
||||
import { useNuxtApp, useRuntimeConfig } from './nuxt'
|
||||
import { cancelIdleCallback, requestIdleCallback } from './idle-callback'
|
||||
|
||||
export function toArray<T> (value: T | T[]): T[] {
|
||||
return Array.isArray(value) ? value : [value]
|
||||
}
|
||||
|
||||
export type CallbackFn = () => void
|
||||
type ObserveFn = (element: Element, callback: CallbackFn) => () => void
|
||||
|
||||
function useObserver (): { observe: ObserveFn } | undefined {
|
||||
if (import.meta.server) { return }
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user