Moved map asset loading progress to use refs to stop re-renders

Moved map loading overlay to use animation frames for a smooth progress bar
This commit is contained in:
Mitchell McCaffrey
2020-06-28 13:37:04 +10:00
parent bf022e2686
commit a81031e84e
4 changed files with 82 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState, useRef } from "react";
import { omit, isEmpty } from "../helpers/shared";
const MapLoadingContext = React.createContext();
@@ -14,36 +14,36 @@ export function MapLoadingProvider({ children }) {
setLoadingAssetCount((prevLoadingAssets) => prevLoadingAssets - 1);
}
const [assetProgress, setAssetProgress] = useState({});
const assetProgressRef = useRef({});
const loadingProgressRef = useRef(null);
function assetProgressUpdate({ id, count, total }) {
if (count === total) {
setAssetProgress(omit(assetProgress, [id]));
assetProgressRef.current = omit(assetProgressRef.current, [id]);
} else {
setAssetProgress((prevAssetProgress) => ({
...prevAssetProgress,
assetProgressRef.current = {
...assetProgressRef.current,
[id]: { count, total },
}));
};
}
if (!isEmpty(assetProgressRef.current)) {
let total = 0;
let count = 0;
for (let progress of Object.values(assetProgressRef.current)) {
total += progress.total;
count += progress.count;
}
loadingProgressRef.current = count / total;
}
}
const isLoading = loadingAssetCount > 0;
let loadingProgress = null;
if (!isEmpty(assetProgress)) {
let total = 0;
let count = 0;
for (let progress of Object.values(assetProgress)) {
total += progress.total;
count += progress.count;
}
loadingProgress = count / total;
}
const value = {
assetLoadStart,
assetLoadFinish,
isLoading,
assetProgressUpdate,
loadingProgress,
loadingProgressRef,
};
return (