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