Move to a delayed asset cleanup function to fix bugs with custom token group images

This commit is contained in:
Mitchell McCaffrey 2021-06-05 14:40:44 +10:00
parent 20351ff097
commit 5347e30b98

View File

@ -4,6 +4,8 @@ import { encode } from "@msgpack/msgpack";
import { useDatabase } from "./DatabaseContext"; import { useDatabase } from "./DatabaseContext";
import useDebounce from "../hooks/useDebounce";
import { omit } from "../helpers/shared"; import { omit } from "../helpers/shared";
/** /**
@ -125,19 +127,26 @@ export const AssetURLsUpdaterContext = React.createContext();
export function AssetURLsProvider({ children }) { export function AssetURLsProvider({ children }) {
const [assetURLs, setAssetURLs] = useState({}); const [assetURLs, setAssetURLs] = useState({});
// Clean up asset URLs every minute
const debouncedAssetURLs = useDebounce(assetURLs, 60 * 1000);
// Revoke url when no more references // Revoke url when no more references
useEffect(() => { useEffect(() => {
let urlsToCleanup = []; setAssetURLs((prevURLs) => {
for (let url of Object.values(assetURLs)) { let urlsToCleanup = [];
if (url.references <= 0) { for (let url of Object.values(prevURLs)) {
URL.revokeObjectURL(url.url); if (url.references <= 0) {
urlsToCleanup.push(url.id); URL.revokeObjectURL(url.url);
urlsToCleanup.push(url.id);
}
} }
} if (urlsToCleanup.length > 0) {
if (urlsToCleanup.length > 0) { return omit(prevURLs, urlsToCleanup);
setAssetURLs((prevURLs) => omit(prevURLs, urlsToCleanup)); } else {
} return prevURLs;
}, [assetURLs]); }
});
}, [debouncedAssetURLs]);
return ( return (
<AssetURLsStateContext.Provider value={assetURLs}> <AssetURLsStateContext.Provider value={assetURLs}>