refactor: improve internal type definitions of `<NuxtLink>` (#9869)

This commit is contained in:
Alex Liu 2023-01-10 22:33:21 +08:00 committed by GitHub
parent 6c32a6087b
commit 9de94c21cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 4 deletions

View File

@ -180,7 +180,7 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
if (shouldPrefetch) {
const nuxtApp = useNuxtApp()
let idleId: number
let unobserve: Function | null = null
let unobserve: (() => void)| null = null
onMounted(() => {
const observer = useObserver()
onNuxtReady(() => {
@ -268,8 +268,10 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
export default defineNuxtLink({ componentName: 'NuxtLink' })
// --- Prefetching utils ---
type CallbackFn = () => void
type ObserveFn = (element: Element, callback: CallbackFn) => () => void
function useObserver () {
function useObserver (): { observe: ObserveFn } | undefined {
if (process.server) { return }
const nuxtApp = useNuxtApp()
@ -278,10 +280,10 @@ function useObserver () {
}
let observer: IntersectionObserver | null = null
type CallbackFn = () => void
const callbacks = new Map<Element, CallbackFn>()
const observe = (element: Element, callback: CallbackFn) => {
const observe: ObserveFn = (element, callback) => {
if (!observer) {
observer = new IntersectionObserver((entries) => {
for (const entry of entries) {