diff --git a/front/src/components/PhotoBox/index.tsx b/front/src/components/PhotoBox/index.tsx index ca9c059..2476f93 100644 --- a/front/src/components/PhotoBox/index.tsx +++ b/front/src/components/PhotoBox/index.tsx @@ -4,6 +4,7 @@ import heartHighlightIMG from 'assets/heart-highlight.png' import style from './index.scss' import { useQueueload } from 'utils/queue-load' +import { timeout } from 'new-vait' export type ImageInfo = { width: number @@ -45,8 +46,8 @@ export default (props: Props) => { const [loaded, setLoaded] = useState(false) - const thumb = useQueueload(photo.thumb) - const avatarThumb = useQueueload(avatar?.thumb) + const [,thumb] = useQueueload(photo.thumb) + const [,avatarThumb] = useQueueload(avatar?.thumb) const coverFrameEl = useRef(null) diff --git a/front/src/utils/queue-load.ts b/front/src/utils/queue-load.ts index 6cc6a09..1908fb3 100644 --- a/front/src/utils/queue-load.ts +++ b/front/src/utils/queue-load.ts @@ -9,20 +9,38 @@ type Load = { blobUrl: string; } +function searchCache(src: string | undefined): readonly [boolean, string] { + if (src === undefined) { + return [false, ''] + } else { + const task = global_cache.get(src) + if (task) { + return [false, task.blobUrl] + } else { + return [false, ''] + } + } +} + export function useQueueload(src: string | undefined) { - const [blobSrc, setBlobSrc] = useState('') + const [ cached, url ] = searchCache(src) + const [ loaded, setLoaded ] = useState(cached) + const [ blobSrc, setBlobSrc ] = useState(url) useEffect(() => { if (src) { let mounted = true globalQueueLoad(src).then(res => { - if (mounted) setBlobSrc(res.blobUrl) + if (mounted) { + setBlobSrc(res.blobUrl) + setLoaded(true) + } }) return () => { mounted = false } } }, [src]) - return blobSrc + return [loaded, blobSrc] as const } export const MAX_PARALLEL_NUMBER = 3 @@ -33,8 +51,8 @@ type LoadTask = { priority: number } -const [ globalQueueLoad, [getGlobalQueue, setGlobalQueue] ] = QueueLoad() -export { globalQueueLoad, getGlobalQueue, setGlobalQueue } +const [ globalQueueLoad, [getGlobalQueue, setGlobalQueue], global_cache ] = QueueLoad() +export { globalQueueLoad, getGlobalQueue, setGlobalQueue, global_cache } export function QueueLoad() { const [getQueue, setQueue] = Memo([]) @@ -146,5 +164,5 @@ export function QueueLoad() { } } - return [ load, [ getQueue, setQueue ] ] as const + return [ load, [ getQueue, setQueue ], cache ] as const }