Spaces:
Sleeping
Sleeping
Create useObjectUrlCache.ts
Browse files
web/src/lib/useObjectUrlCache.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useEffect, useMemo, useRef } from "react";
|
| 2 |
+
|
| 3 |
+
function fingerprint(f: File) {
|
| 4 |
+
return `${f.name}::${f.size}::${f.lastModified}`;
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
export function useObjectUrlCache(files: File[]) {
|
| 8 |
+
const cacheRef = useRef<Map<string, string>>(new Map());
|
| 9 |
+
|
| 10 |
+
const keys = useMemo(() => {
|
| 11 |
+
return files.map((f) => fingerprint(f));
|
| 12 |
+
}, [files]);
|
| 13 |
+
|
| 14 |
+
// revoke removed keys
|
| 15 |
+
useEffect(() => {
|
| 16 |
+
const need = new Set(keys);
|
| 17 |
+
for (const [key, url] of cacheRef.current.entries()) {
|
| 18 |
+
if (!need.has(key)) {
|
| 19 |
+
try {
|
| 20 |
+
URL.revokeObjectURL(url);
|
| 21 |
+
} catch {}
|
| 22 |
+
cacheRef.current.delete(key);
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
}, [keys]);
|
| 26 |
+
|
| 27 |
+
// unmount: revoke all
|
| 28 |
+
useEffect(() => {
|
| 29 |
+
return () => {
|
| 30 |
+
for (const url of cacheRef.current.values()) {
|
| 31 |
+
try {
|
| 32 |
+
URL.revokeObjectURL(url);
|
| 33 |
+
} catch {}
|
| 34 |
+
}
|
| 35 |
+
cacheRef.current.clear();
|
| 36 |
+
};
|
| 37 |
+
}, []);
|
| 38 |
+
|
| 39 |
+
const getOrCreate = (file: File) => {
|
| 40 |
+
const key = fingerprint(file);
|
| 41 |
+
const existed = cacheRef.current.get(key);
|
| 42 |
+
if (existed) return existed;
|
| 43 |
+
|
| 44 |
+
const url = URL.createObjectURL(file);
|
| 45 |
+
cacheRef.current.set(key, url);
|
| 46 |
+
return url;
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
return { getOrCreate };
|
| 50 |
+
}
|