|
| 1 | +import { useCallback, useEffect, useRef, type ImgHTMLAttributes, type SyntheticEvent } from "react" |
| 2 | + |
| 3 | +type ImageTracker = { |
| 4 | + pending: number |
| 5 | + start: () => () => void |
| 6 | + wait: () => Promise<void> |
| 7 | +} |
| 8 | + |
| 9 | +const IMAGE_TRACKER_KEY = "__frameScript_ImageTracker" |
| 10 | + |
| 11 | +const getImageTracker = (): ImageTracker => { |
| 12 | + const g = globalThis as unknown as Record<string, unknown> |
| 13 | + const existing = g[IMAGE_TRACKER_KEY] as ImageTracker | undefined |
| 14 | + if (existing) return existing |
| 15 | + |
| 16 | + let pending = 0 |
| 17 | + const waiters = new Set<() => void>() |
| 18 | + |
| 19 | + const notifyIfReady = () => { |
| 20 | + if (pending !== 0) return |
| 21 | + for (const resolve of Array.from(waiters)) { |
| 22 | + resolve() |
| 23 | + } |
| 24 | + waiters.clear() |
| 25 | + } |
| 26 | + |
| 27 | + const tracker: ImageTracker = { |
| 28 | + get pending() { |
| 29 | + return pending |
| 30 | + }, |
| 31 | + start: () => { |
| 32 | + pending += 1 |
| 33 | + let done = false |
| 34 | + return () => { |
| 35 | + if (done) return |
| 36 | + done = true |
| 37 | + pending = Math.max(0, pending - 1) |
| 38 | + notifyIfReady() |
| 39 | + } |
| 40 | + }, |
| 41 | + wait: () => { |
| 42 | + if (pending === 0) return Promise.resolve() |
| 43 | + return new Promise<void>((resolve) => { |
| 44 | + waiters.add(resolve) |
| 45 | + }) |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + g[IMAGE_TRACKER_KEY] = tracker |
| 50 | + return tracker |
| 51 | +} |
| 52 | + |
| 53 | +const waitForAnimationTick = () => |
| 54 | + new Promise<void>((resolve) => { |
| 55 | + if (typeof window === "undefined" || typeof window.requestAnimationFrame !== "function") { |
| 56 | + setTimeout(resolve, 0) |
| 57 | + return |
| 58 | + } |
| 59 | + window.requestAnimationFrame(() => resolve()) |
| 60 | + }) |
| 61 | + |
| 62 | +const installImageApi = () => { |
| 63 | + if (typeof window === "undefined") return |
| 64 | + const tracker = getImageTracker() |
| 65 | + const waitImagesReady = async () => { |
| 66 | + while (true) { |
| 67 | + if (tracker.pending === 0) { |
| 68 | + await waitForAnimationTick() |
| 69 | + if (tracker.pending === 0) return |
| 70 | + } |
| 71 | + await tracker.wait() |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + ;(window as any).__frameScript = { |
| 76 | + ...(window as any).__frameScript, |
| 77 | + waitImagesReady, |
| 78 | + getImagesPending: () => tracker.pending, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +if (typeof window !== "undefined") { |
| 83 | + installImageApi() |
| 84 | +} |
| 85 | + |
| 86 | +const useImagePending = () => { |
| 87 | + const loadIdRef = useRef(0) |
| 88 | + const pendingFinishRef = useRef<(() => void) | null>(null) |
| 89 | + |
| 90 | + const beginPending = useCallback(() => { |
| 91 | + loadIdRef.current += 1 |
| 92 | + if (!pendingFinishRef.current) { |
| 93 | + pendingFinishRef.current = getImageTracker().start() |
| 94 | + } |
| 95 | + return loadIdRef.current |
| 96 | + }, []) |
| 97 | + |
| 98 | + const endPending = useCallback(() => { |
| 99 | + if (pendingFinishRef.current) { |
| 100 | + pendingFinishRef.current() |
| 101 | + pendingFinishRef.current = null |
| 102 | + } |
| 103 | + }, []) |
| 104 | + |
| 105 | + useEffect(() => () => endPending(), [endPending]) |
| 106 | + |
| 107 | + return { beginPending, endPending, loadIdRef } |
| 108 | +} |
| 109 | + |
| 110 | +export type ImgProps = Omit<ImgHTMLAttributes<HTMLImageElement>, "src"> & { |
| 111 | + src: string |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Image component that waits for decode before rendering in headless mode. |
| 116 | + * |
| 117 | + * デコード完了まで待機する <Img> です。 |
| 118 | + */ |
| 119 | +export const Img = ({ src, onLoad, onError, ...props }: ImgProps) => { |
| 120 | + const imgRef = useRef<HTMLImageElement | null>(null) |
| 121 | + const { beginPending, endPending, loadIdRef } = useImagePending() |
| 122 | + |
| 123 | + useEffect(() => { |
| 124 | + const img = imgRef.current |
| 125 | + if (!img || !src) return |
| 126 | + |
| 127 | + const loadId = beginPending() |
| 128 | + let done = false |
| 129 | + |
| 130 | + const finalize = () => { |
| 131 | + if (done) return |
| 132 | + done = true |
| 133 | + if (loadId === loadIdRef.current) { |
| 134 | + endPending() |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + const handleLoad = (event: Event) => { |
| 139 | + onLoad?.(event as unknown as SyntheticEvent<HTMLImageElement>) |
| 140 | + const decode = typeof img.decode === "function" ? img.decode() : Promise.resolve() |
| 141 | + decode.catch(() => {}).finally(finalize) |
| 142 | + } |
| 143 | + |
| 144 | + const handleError = (event: Event) => { |
| 145 | + onError?.(event as unknown as SyntheticEvent<HTMLImageElement>) |
| 146 | + finalize() |
| 147 | + } |
| 148 | + |
| 149 | + if (img.complete && img.naturalWidth > 0) { |
| 150 | + const decode = typeof img.decode === "function" ? img.decode() : Promise.resolve() |
| 151 | + decode.catch(() => {}).finally(finalize) |
| 152 | + } else { |
| 153 | + img.addEventListener("load", handleLoad) |
| 154 | + img.addEventListener("error", handleError) |
| 155 | + } |
| 156 | + |
| 157 | + return () => { |
| 158 | + img.removeEventListener("load", handleLoad) |
| 159 | + img.removeEventListener("error", handleError) |
| 160 | + if (loadId === loadIdRef.current) { |
| 161 | + endPending() |
| 162 | + } |
| 163 | + } |
| 164 | + }, [src, onLoad, onError, beginPending, endPending]) |
| 165 | + |
| 166 | + return <img ref={imgRef} src={src} {...props} /> |
| 167 | +} |
0 commit comments