Implement asset clear cache and remove cache when upgrading to 1.9

This commit is contained in:
Mitchell McCaffrey 2021-04-30 09:55:05 +10:00
parent 04cf5687dd
commit f844199172
2 changed files with 22 additions and 10 deletions

View File

@ -559,7 +559,7 @@ const versions = {
});
});
},
// v1.9.0 - Move tokens to use more defaults and add token outline
// v1.9.0 - Move tokens to use more defaults and add token outline to tokens
28(v) {
v.stores({}).upgrade((tx) => {
tx.table("tokens")
@ -576,7 +576,7 @@ const versions = {
});
});
},
// v1.9.0 - Move tokens to use more defaults and add token outline
// v1.9.0 - Move tokens to use more defaults and add token outline to token states
29(v) {
v.stores({}).upgrade((tx) => {
tx.table("states")
@ -612,6 +612,20 @@ const versions = {
});
});
},
// v1.9.0 - Remove maps not owned by user as cache is now done on the asset level
30(v) {
v.stores({}).upgrade(async (tx) => {
const userId = (await tx.table("user").get("userId")).value;
tx.table("maps").where("owner").notEqual(userId).delete();
});
},
// v1.9.0 - Remove tokens not owned by user as cache is now done on the asset level
31(v) {
v.stores({}).upgrade(async (tx) => {
const userId = (await tx.table("user").get("userId")).value;
tx.table("tokens").where("owner").notEqual(userId).delete();
});
},
};
const latestVersion = 29;

View File

@ -66,26 +66,24 @@ function SettingsModal({ isOpen, onRequestClose }) {
setIsLoading(true);
// Clear saved settings
localStorage.clear();
// Clear map cache
await database.table("maps").where("owner").notEqual(userId).delete();
// Find all other peoples tokens who aren't benig used in a map state and delete them
const tokens = await database
.table("tokens")
const assets = await database
.table("assets")
.where("owner")
.notEqual(userId)
.toArray();
const states = await database.table("states").toArray();
for (let token of tokens) {
for (let asset of assets) {
let inUse = false;
for (let state of states) {
for (let tokenState of Object.values(state.tokens)) {
if (token.id === tokenState.tokenId) {
if (tokenState.type === "file" && asset.id === tokenState.file) {
inUse = true;
}
}
}
if (!inUse) {
database.table("tokens").delete(token.id);
await database.table("assets").delete(asset.id);
}
}
window.location.reload();