Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat: 切换图片时中断上一张图片的加载

Timothy Yina745c49d356b
diff --git a/app/components/masonry/PhotoItem.client.vue b/app/components/masonry/PhotoItem.client.vue
index 77dd4afc..47d61e2d 100644
--- a/app/components/masonry/PhotoItem.client.vue
+++ b/app/components/masonry/PhotoItem.client.vue
@@ -23,6 +23,10 @@ const mainImageRef = ref<HTMLImageElement>()
const isVisible = ref(false)
const containerWidth = ref(0)
+// Observers
+const resizeObserverRef = ref<ResizeObserver | null>(null)
+const intersectionObserverRef = ref<IntersectionObserver | null>(null)
+
// Computed
const containerHeight = computed(() => {
// Get the actual container width to calculate proper aspect ratio
@@ -142,12 +146,8 @@ onMounted(() => {
containerWidth.value = photoRef.value.offsetWidth
}
})
-
resizeObserver.observe(photoRef.value)
-
- onUnmounted(() => {
- resizeObserver.disconnect()
- })
+ resizeObserverRef.value = resizeObserver
}
})
@@ -199,11 +199,7 @@ onMounted(() => {
)
observer.observe(photoRef.value)
-
- // Cleanup observer on unmount
- onUnmounted(() => {
- observer.disconnect()
- })
+ intersectionObserverRef.value = observer
}
// Check if main image is already loaded from cache
@@ -212,6 +208,16 @@ onMounted(() => {
}
})
})
+
+// Cleanup observers on unmount
+onUnmounted(() => {
+ if (resizeObserverRef.value) {
+ resizeObserverRef.value.disconnect()
+ }
+ if (intersectionObserverRef.value) {
+ intersectionObserverRef.value.disconnect()
+ }
+})
</script>
<template>
diff --git a/app/components/photo/ProgressiveImage.vue b/app/components/photo/ProgressiveImage.vue
index ce0d755e..4adedcc8 100644
--- a/app/components/photo/ProgressiveImage.vue
+++ b/app/components/photo/ProgressiveImage.vue
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { WebGLImageViewer } from '@chronoframe/webgl-image'
import type { LoadingIndicatorRef } from './LoadingIndicator.vue'
+import { ImageLoaderManager } from '~/libs/image-loader-manager'
interface Props {
src: string
@@ -39,6 +40,7 @@ const currentSrc = ref<string | null>()
// 使用 WebGLImageViewer 的引用
const webglViewerRef = ref()
+const loaderManagerRef = ref<ImageLoaderManager | null>(null)
const showThumbnail = computed(() => {
return props.thumbnailSrc && (!highResRendered.value || hasError.value)
@@ -54,7 +56,7 @@ const showWebGLViewer = computed(() => {
})
const loadImage = () => {
- useImageLoader(
+ loaderManagerRef.value = useImageLoader(
props.src,
props.isCurrentImage,
highResLoaded.value,
@@ -73,7 +75,11 @@ const loadImage = () => {
watch(
() => props.isCurrentImage,
(isCurrent, wasCurrent) => {
- if (isCurrent && !wasCurrent && !highResLoaded.value && !hasError.value) {
+ if (!isCurrent && wasCurrent) {
+ // 当图片不再是当前图片时,中断加载
+ loaderManagerRef.value?.cleanup()
+ loaderManagerRef.value = null
+ } else if (isCurrent && !wasCurrent && !highResLoaded.value && !hasError.value) {
// 当图片变为当前图片且尚未加载高分辨率图片时,触发加载
loadImage()
}
@@ -86,6 +92,10 @@ watch(
() => props.src,
(newSrc, oldSrc) => {
if (newSrc !== oldSrc) {
+ // 中断之前的加载
+ loaderManagerRef.value?.cleanup()
+ loaderManagerRef.value = null
+
// 重置状态
highResLoaded.value = false
highResRendered.value = false
@@ -113,6 +123,12 @@ const handleZoomChange = (originalScale: number, relativeScale: number) => {
props.onZoomChange(isZoomed)
}
}
+
+// 组件卸载时清理
+onUnmounted(() => {
+ loaderManagerRef.value?.cleanup()
+ loaderManagerRef.value = null
+})
</script>
<template>
diff --git a/app/composables/useImageLoader.ts b/app/composables/useImageLoader.ts
index 86a4263b..ffa70c99 100644
--- a/app/composables/useImageLoader.ts
+++ b/app/composables/useImageLoader.ts
@@ -14,11 +14,12 @@ export const useImageLoader = (
updateError?: (error: boolean) => void,
updateHighResImageRendered?: (isRendered: boolean) => void
) => {
- if (highResLoaded || !isCurrentImage || error) return
+ if (highResLoaded || !isCurrentImage || error) return null
const loaderManager = new ImageLoaderManager()
const cleanup = () => {
+ loaderManager.cleanup()
updateBlobSrc?.(null)
updateHighResImageRendered?.(false)
updateHighResLoaded?.(false)
diff --git a/package.json b/package.json
index 6e5265eb..76cf37dc 100644
--- a/package.json
+++ b/package.json
@@ -58,6 +58,9 @@
},
"packageManager": "[email protected]+sha512.1f79bc245a66eb0b07c5d4d83131240774642caaa86ef7d0434ab47c0d16f66b04e21e0c086eb61e62c77efc4d7f7ec071afad3796af64892fae66509173893a",
"devDependencies": {
+ "@iconify-json/carbon": "^1.2.13",
+ "@iconify-json/material-symbols": "^1.2.33",
+ "@iconify-json/streamline": "^1.2.5",
"@iconify-json/tabler": "^1.2.22",
"@nuxt/eslint": "1.9.0",
"@nuxt/test-utils": "3.19.2",
@@ -65,6 +68,7 @@
"@types/heic-convert": "^2.1.0",
"concurrently": "^9.2.0",
"consola": "^3.4.2",
+ "dayjs": "^1.11.15",
"drizzle-kit": "^0.31.4",
"eslint": "^9.33.0",
"eslint-config-prettier": "^10.1.8",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 713fe900..70651359 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -111,6 +111,15 @@ importers:
specifier: ^4.32.0
version: 4.32.0(@cloudflare/[email protected])
devDependencies:
+ '@iconify-json/carbon':
+ specifier: ^1.2.13
+ version: 1.2.13
+ '@iconify-json/material-symbols':
+ specifier: ^1.2.33
+ version: 1.2.33
+ '@iconify-json/streamline':
+ specifier: ^1.2.5
+ version: 1.2.5
'@iconify-json/tabler':
specifier: ^1.2.22
version: 1.2.22
@@ -132,6 +141,9 @@ importers:
consola:
specifier: ^3.4.2
version: 3.4.2
+ dayjs:
+ specifier: ^1.11.15
+ version: 1.11.15
drizzle-kit:
specifier: ^0.31.4
version: 0.31.4
@@ -1277,6 +1289,15 @@ packages:
resolution: {integrity: sha512-bV0Tgo9K4hfPCek+aMAn81RppFKv2ySDQeMoSZuvTASywNTnVJCArCZE2FWqpvIatKu7VMRLWlR1EazvVhDyhQ==}
engines: {node: '>=18.18'}
+ '@iconify-json/[email protected]':
+ resolution: {integrity: sha512-vXrFFKkfYaZSwf4WfnRQJQMnpEzxlPlupwZZlS1WvOAuNnWahsUudHycaMpoBzDqBfniH4KQ2b5qnw0VMKoJug==}
+
+ '@iconify-json/[email protected]':
+ resolution: {integrity: sha512-Bs0X1+/vpJydW63olrGh60zkR8/Y70sI14AIWaP7Z6YQXukzWANH4q3I0sIPklbIn1oL6uwLvh0zQyd6Vh79LQ==}
+
+ '@iconify-json/[email protected]':
+ resolution: {integrity: sha512-u6l9BOJoIIPjjDXWl6D/hPDzeBk5WiaEHZ+U9SbkQ14N9hgotaYyIZVMfgF175CG1TTS06j8k15D3FM2OYaFIw==}
+
'@iconify-json/[email protected]':
resolution: {integrity: sha512-KmCtTzZyz7dtszdQGJ5wbWqko6exhI38e4aCQqWZE1Z2wGUqBsMXrRWawXkAp6/XNBebt54h0lcxFCaTYBwFJg==}
@@ -4014,8 +4035,8 @@ packages:
resolution: {integrity: sha512-KDDNiET7KAKf6yzL3RaPWq5aV7ql9QTt5fIDYv+4eOegDmnEQGjwkKYADDystsKtPjt7QZerpVbhC96o3BIyqQ==}
- resolution: {integrity: sha512-E8fIdSxUlyqSA8XYGnNa3IkIzxtEmFjI+JU/6ic0P1zmSqyL6HyG5jHnpPjRguDNiaHLpfvHKWFiohNsJLqcJQ==}
+ resolution: {integrity: sha512-MC+DfnSWiM9APs7fpiurHGCoeIx0Gdl6QZBy+5lu8MbYKN5FZEXqOgrundfibdfhGZ15o9hzmZ2xJjZnbvgKXQ==}
resolution: {integrity: sha512-xzWNQ6jk/+NtdfLyXEipbX55dmDSeteLFt/ayF+wZUU5bzKgmrDOxmInUTbyVRp46YwnJdkDA1KhB7WIXFofJw==}
@@ -8871,6 +8892,18 @@ snapshots:
'@humanwhocodes/[email protected]': {}
+ '@iconify-json/[email protected]':
+ dependencies:
+ '@iconify/types': 2.0.0
+
+ '@iconify-json/[email protected]':
+ dependencies:
+ '@iconify/types': 2.0.0
+
+ '@iconify-json/[email protected]':
+ dependencies:
+ '@iconify/types': 2.0.0
+
'@iconify-json/[email protected]':
dependencies:
'@iconify/types': 2.0.0
@@ -12212,11 +12245,11 @@ snapshots:
dependencies:
'@nuxt/kit': 3.18.1([email protected])
- dayjs: 1.11.14
+ dayjs: 1.11.15
transitivePeerDependencies:
- magicast
optionalDependencies: