From 5347e30b98177096746d6b1ee39bad05c8a94b7a Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Sat, 5 Jun 2021 14:40:44 +1000 Subject: [PATCH] Move to a delayed asset cleanup function to fix bugs with custom token group images --- src/contexts/AssetsContext.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/contexts/AssetsContext.js b/src/contexts/AssetsContext.js index f6a9780..c327f0e 100644 --- a/src/contexts/AssetsContext.js +++ b/src/contexts/AssetsContext.js @@ -4,6 +4,8 @@ import { encode } from "@msgpack/msgpack"; import { useDatabase } from "./DatabaseContext"; +import useDebounce from "../hooks/useDebounce"; + import { omit } from "../helpers/shared"; /** @@ -125,19 +127,26 @@ export const AssetURLsUpdaterContext = React.createContext(); export function AssetURLsProvider({ children }) { const [assetURLs, setAssetURLs] = useState({}); + // Clean up asset URLs every minute + const debouncedAssetURLs = useDebounce(assetURLs, 60 * 1000); + // Revoke url when no more references useEffect(() => { - let urlsToCleanup = []; - for (let url of Object.values(assetURLs)) { - if (url.references <= 0) { - URL.revokeObjectURL(url.url); - urlsToCleanup.push(url.id); + setAssetURLs((prevURLs) => { + let urlsToCleanup = []; + for (let url of Object.values(prevURLs)) { + if (url.references <= 0) { + URL.revokeObjectURL(url.url); + urlsToCleanup.push(url.id); + } } - } - if (urlsToCleanup.length > 0) { - setAssetURLs((prevURLs) => omit(prevURLs, urlsToCleanup)); - } - }, [assetURLs]); + if (urlsToCleanup.length > 0) { + return omit(prevURLs, urlsToCleanup); + } else { + return prevURLs; + } + }); + }, [debouncedAssetURLs]); return (