MCPcopy Create free account
hub / github.com/ColmapView/Colmapview.github.io / useMaskedThumbnail

Function useMaskedThumbnail

src/hooks/useMaskedThumbnail.ts:127–191  ·  view source on GitHub ↗
(
  imageFile: File | undefined,
  maskFile: File | undefined,
  imageName: string,
  enabled: boolean,
  inverseMask = false
)

Source from the content-addressed store, hash-verified

125}
126
127export function useMaskedThumbnail(
128 imageFile: File | undefined,
129 maskFile: File | undefined,
130 imageName: string,
131 enabled: boolean,
132 inverseMask = false
133): string | null {
134 const cacheKey = useMemo(
135 () => enabled && imageFile && maskFile
136 ? getMaskedThumbnailCacheKey(imageName, imageFile, maskFile, inverseMask)
137 : '',
138 [enabled, imageFile, imageName, maskFile, inverseMask]
139 );
140 const [state, setState] = useState<{ cacheKey: string; url: string | null }>(() => ({
141 cacheKey,
142 url: cacheKey ? maskedThumbnailCache.get(cacheKey) ?? null : null,
143 }));
144
145 useEffect(() => {
146 let cancelled = false;
147
148 if (!enabled || !imageFile || !maskFile || !cacheKey) {
149 return () => {
150 cancelled = true;
151 };
152 }
153
154 const cached = maskedThumbnailCache.get(cacheKey);
155 if (cached) {
156 return () => {
157 cancelled = true;
158 };
159 }
160
161 let load = maskedThumbnailLoads.get(cacheKey);
162 if (!load) {
163 const loadGeneration = maskedThumbnailCacheGeneration;
164 load = loadMaskedThumbnail(imageFile, maskFile, inverseMask).then((result) => {
165 maskedThumbnailLoads.delete(cacheKey);
166 if (loadGeneration !== maskedThumbnailCacheGeneration) {
167 if (result) URL.revokeObjectURL(result);
168 return null;
169 }
170 if (result) {
171 maskedThumbnailCache.set(cacheKey, result);
172 }
173 return result;
174 });
175 maskedThumbnailLoads.set(cacheKey, load);
176 }
177
178 load.then((result) => {
179 if (!cancelled) {
180 setState({ cacheKey, url: result });
181 }
182 });
183
184 return () => {

Callers 1

Calls 4

loadMaskedThumbnailFunction · 0.85
getMethod · 0.80
deleteMethod · 0.80

Tested by

no test coverage detected