Restructure asset manifest to split replication id and assets

This commit is contained in:
Mitchell McCaffrey 2021-03-23 10:24:01 +11:00
parent 75f4b97135
commit c77db06bce

View File

@ -66,7 +66,7 @@ function NetworkedMapAndTokens({ session }) {
); );
async function loadAssetManifestFromMap(map, mapState) { async function loadAssetManifestFromMap(map, mapState) {
const assets = { mapId: map.id }; const assets = {};
if (map.type === "file") { if (map.type === "file") {
const { id, lastModified, owner } = map; const { id, lastModified, owner } = map;
assets[`map-${id}`] = { type: "map", id, lastModified, owner }; assets[`map-${id}`] = { type: "map", id, lastModified, owner };
@ -85,7 +85,7 @@ function NetworkedMapAndTokens({ session }) {
assets[`token-${id}`] = { type: "token", id, lastModified, owner }; assets[`token-${id}`] = { type: "token", id, lastModified, owner };
} }
} }
setAssetManifest(assets, true, true); setAssetManifest({ mapId: map.id, assets }, true, true);
} }
function compareAssets(a, b) { function compareAssets(a, b) {
@ -101,17 +101,24 @@ function NetworkedMapAndTokens({ session }) {
} }
function addAssetIfNeeded(asset) { function addAssetIfNeeded(asset) {
setAssetManifest((prevAssets) => { setAssetManifest((prevManifest) => {
const id = asset.type === "map" ? `map-${asset.id}` : `token-${asset.id}`; if (prevManifest?.assets) {
const exists = id in prevAssets; const id =
const needsUpdate = exists && assetNeedsUpdate(prevAssets[id], asset); asset.type === "map" ? `map-${asset.id}` : `token-${asset.id}`;
if (!exists || needsUpdate) { const exists = id in prevManifest.assets;
return { const needsUpdate =
...prevAssets, exists && assetNeedsUpdate(prevManifest.assets[id], asset);
[id]: asset, if (!exists || needsUpdate) {
}; return {
...prevManifest,
assets: {
...prevManifest.assets,
[id]: asset,
},
};
}
} }
return prevAssets; return prevManifest;
}); });
} }
@ -124,7 +131,7 @@ function NetworkedMapAndTokens({ session }) {
} }
async function requestAssetsIfNeeded() { async function requestAssetsIfNeeded() {
for (let asset of Object.values(assetManifest)) { for (let asset of Object.values(assetManifest.assets)) {
if ( if (
asset.owner === userId || asset.owner === userId ||
requestingAssetsRef.current.has(asset.id) requestingAssetsRef.current.has(asset.id)
@ -145,8 +152,6 @@ function NetworkedMapAndTokens({ session }) {
const cachedMap = await getMapFromDB(asset.id); const cachedMap = await getMapFromDB(asset.id);
if (cachedMap && cachedMap.lastModified === asset.lastModified) { if (cachedMap && cachedMap.lastModified === asset.lastModified) {
requestingAssetsRef.current.delete(asset.id); requestingAssetsRef.current.delete(asset.id);
} else if (cachedMap && cachedMap.lastModified > asset.lastModified) {
requestingAssetsRef.current.delete(asset.id);
} else { } else {
session.sendTo(owner.sessionId, "mapRequest", asset.id); session.sendTo(owner.sessionId, "mapRequest", asset.id);
} }