Moved token request to DB and added sanity check to putToken
This commit is contained in:
parent
ae32068ccb
commit
1c143f5464
@ -7,7 +7,6 @@ import DatabaseContext from "./DatabaseContext";
|
||||
import DatabaseWorker from "worker-loader!../workers/DatabaseWorker"; // eslint-disable-line import/no-webpack-loader-syntax
|
||||
|
||||
import { tokens as defaultTokens } from "../tokens";
|
||||
import { logError } from "../helpers/logging";
|
||||
|
||||
const TokenDataContext = React.createContext();
|
||||
|
||||
@ -106,16 +105,7 @@ export function TokenDataProvider({ children }) {
|
||||
}
|
||||
|
||||
async function putToken(token) {
|
||||
try {
|
||||
await database.table("tokens").put(token);
|
||||
} catch {
|
||||
if (token?.file) {
|
||||
const { file, ...rest } = token;
|
||||
logError(`Unable to save token ${JSON.stringify(rest)}`);
|
||||
} else {
|
||||
logError(`Unable to save token ${JSON.stringify(token)}`);
|
||||
}
|
||||
}
|
||||
await database.table("tokens").put(token);
|
||||
setTokens((prevTokens) => {
|
||||
const newTokens = [...prevTokens];
|
||||
const i = newTokens.findIndex((t) => t.id === token.id);
|
||||
@ -157,6 +147,11 @@ export function TokenDataProvider({ children }) {
|
||||
return tokens.find((token) => token.id === tokenId);
|
||||
}
|
||||
|
||||
async function getTokenFromDB(tokenId) {
|
||||
let token = await database.table("tokens").get(tokenId);
|
||||
return token;
|
||||
}
|
||||
|
||||
const ownedTokens = tokens.filter((token) => token.owner === userId);
|
||||
|
||||
const tokensById = tokens.reduce((obj, token) => {
|
||||
@ -176,6 +171,7 @@ export function TokenDataProvider({ children }) {
|
||||
getToken,
|
||||
tokensById,
|
||||
tokensLoading,
|
||||
getTokenFromDB,
|
||||
};
|
||||
|
||||
return (
|
||||
|
@ -35,7 +35,9 @@ function NetworkedMapAndTokens({ session }) {
|
||||
isLoading,
|
||||
} = useContext(MapLoadingContext);
|
||||
|
||||
const { putToken, getToken, updateToken } = useContext(TokenDataContext);
|
||||
const { putToken, getToken, updateToken, getTokenFromDB } = useContext(
|
||||
TokenDataContext
|
||||
);
|
||||
const { putMap, updateMap, getMapFromDB, updateMapState } = useContext(
|
||||
MapDataContext
|
||||
);
|
||||
@ -311,7 +313,6 @@ function NetworkedMapAndTokens({ session }) {
|
||||
async function handlePeerData({ id, data, reply }) {
|
||||
if (id === "mapRequest") {
|
||||
const map = await getMapFromDB(data);
|
||||
|
||||
function replyWithMap(preview, resolution) {
|
||||
let response = {
|
||||
...map,
|
||||
@ -375,19 +376,23 @@ function NetworkedMapAndTokens({ session }) {
|
||||
|
||||
if (id === "mapResponse") {
|
||||
const newMap = data;
|
||||
setCurrentMap(newMap);
|
||||
await putMap(newMap);
|
||||
if (newMap?.id) {
|
||||
setCurrentMap(newMap);
|
||||
await putMap(newMap);
|
||||
}
|
||||
assetLoadFinish();
|
||||
}
|
||||
|
||||
if (id === "tokenRequest") {
|
||||
const token = getToken(data);
|
||||
const token = await getTokenFromDB(data);
|
||||
// Add a last used property for cache invalidation
|
||||
reply("tokenResponse", { ...token, lastUsed: Date.now() }, "token");
|
||||
}
|
||||
if (id === "tokenResponse") {
|
||||
const newToken = data;
|
||||
await putToken(newToken);
|
||||
if (newToken?.id) {
|
||||
await putToken(newToken);
|
||||
}
|
||||
assetLoadFinish();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user