2021-04-28 23:49:39 -04:00
|
|
|
import React, { useState, useRef, useContext, useCallback } from "react";
|
2020-05-25 01:34:22 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
type MapLoadingProgress = {
|
|
|
|
count: number;
|
|
|
|
total: number;
|
|
|
|
};
|
2020-05-25 01:34:22 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
type MapLoadingProgressUpdate = MapLoadingProgress & {
|
|
|
|
id: string;
|
|
|
|
};
|
2020-05-25 01:34:22 -04:00
|
|
|
|
2021-07-17 04:39:49 -04:00
|
|
|
type MapLoadingContextValue = {
|
2021-07-02 01:54:54 -04:00
|
|
|
isLoading: boolean;
|
|
|
|
assetLoadStart: (id: string) => void;
|
|
|
|
assetProgressUpdate: (update: MapLoadingProgressUpdate) => void;
|
2021-07-16 04:59:29 -04:00
|
|
|
loadingProgressRef: React.MutableRefObject<number>;
|
2021-07-02 01:54:54 -04:00
|
|
|
};
|
2020-05-25 01:34:22 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
const MapLoadingContext =
|
2021-07-17 04:39:49 -04:00
|
|
|
React.createContext<MapLoadingContextValue | undefined>(undefined);
|
2020-05-25 01:34:22 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
export function MapLoadingProvider({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode;
|
|
|
|
}) {
|
2021-04-28 23:49:39 -04:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
// Mapping from asset id to the count and total number of pieces loaded
|
2021-07-02 01:54:54 -04:00
|
|
|
const assetProgressRef = useRef<Record<string, MapLoadingProgress>>({});
|
2021-04-28 23:49:39 -04:00
|
|
|
// Loading progress of all assets between 0 and 1
|
2021-07-16 04:59:29 -04:00
|
|
|
const loadingProgressRef = useRef<number>(0);
|
2021-04-28 23:49:39 -04:00
|
|
|
|
|
|
|
const assetLoadStart = useCallback((id) => {
|
|
|
|
setIsLoading(true);
|
|
|
|
// Add asset at a 0% progress
|
|
|
|
assetProgressRef.current = {
|
|
|
|
...assetProgressRef.current,
|
|
|
|
[id]: { count: 0, total: 1 },
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const assetProgressUpdate = useCallback(({ id, count, total }) => {
|
|
|
|
assetProgressRef.current = {
|
|
|
|
...assetProgressRef.current,
|
|
|
|
[id]: { count, total },
|
|
|
|
};
|
|
|
|
// Update loading progress
|
|
|
|
let complete = 0;
|
|
|
|
const progresses = Object.values(assetProgressRef.current);
|
|
|
|
for (let progress of progresses) {
|
|
|
|
complete += progress.count / progress.total;
|
2020-06-27 23:37:04 -04:00
|
|
|
}
|
2021-04-28 23:49:39 -04:00
|
|
|
loadingProgressRef.current = complete / progresses.length;
|
|
|
|
// All loading is complete
|
|
|
|
if (loadingProgressRef.current === 1) {
|
|
|
|
setIsLoading(false);
|
|
|
|
assetProgressRef.current = {};
|
2020-06-27 22:28:58 -04:00
|
|
|
}
|
2021-04-28 23:49:39 -04:00
|
|
|
}, []);
|
2020-05-25 01:34:22 -04:00
|
|
|
|
|
|
|
const value = {
|
|
|
|
assetLoadStart,
|
|
|
|
isLoading,
|
2020-06-27 22:28:58 -04:00
|
|
|
assetProgressUpdate,
|
2020-06-27 23:37:04 -04:00
|
|
|
loadingProgressRef,
|
2020-05-25 01:34:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<MapLoadingContext.Provider value={value}>
|
|
|
|
{children}
|
|
|
|
</MapLoadingContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-05 21:32:38 -05:00
|
|
|
export function useMapLoading() {
|
|
|
|
const context = useContext(MapLoadingContext);
|
|
|
|
if (context === undefined) {
|
|
|
|
throw new Error("useMapLoading must be used within a MapLoadingProvider");
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2020-05-25 01:34:22 -04:00
|
|
|
export default MapLoadingContext;
|