Added multi-threaded initial map and token loading
This commit is contained in:
parent
8160a98c49
commit
aaa793ad71
@ -14,6 +14,7 @@
|
|||||||
"@testing-library/user-event": "^12.2.2",
|
"@testing-library/user-event": "^12.2.2",
|
||||||
"ammo.js": "kripken/ammo.js#aab297a4164779c3a9d8dc8d9da26958de3cb778",
|
"ammo.js": "kripken/ammo.js#aab297a4164779c3a9d8dc8d9da26958de3cb778",
|
||||||
"case": "^1.6.3",
|
"case": "^1.6.3",
|
||||||
|
"comlink": "^4.3.0",
|
||||||
"dexie": "^3.0.3",
|
"dexie": "^3.0.3",
|
||||||
"err-code": "^2.0.3",
|
"err-code": "^2.0.3",
|
||||||
"fake-indexeddb": "^3.1.2",
|
"fake-indexeddb": "^3.1.2",
|
||||||
@ -71,5 +72,8 @@
|
|||||||
"last 1 firefox version",
|
"last 1 firefox version",
|
||||||
"last 1 safari version"
|
"last 1 safari version"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"worker-loader": "^3.0.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
import React, { useEffect, useState, useContext } from "react";
|
import React, { useEffect, useState, useContext } from "react";
|
||||||
|
import * as Comlink from "comlink";
|
||||||
|
|
||||||
import AuthContext from "./AuthContext";
|
import AuthContext from "./AuthContext";
|
||||||
import DatabaseContext from "./DatabaseContext";
|
import DatabaseContext from "./DatabaseContext";
|
||||||
|
|
||||||
|
import DatabaseWorker from "worker-loader!../workers/DatabaseWorker"; // eslint-disable-line import/no-webpack-loader-syntax
|
||||||
|
|
||||||
import { maps as defaultMaps } from "../maps";
|
import { maps as defaultMaps } from "../maps";
|
||||||
|
|
||||||
const MapDataContext = React.createContext();
|
const MapDataContext = React.createContext();
|
||||||
@ -29,6 +32,8 @@ export function MapDataProvider({ children }) {
|
|||||||
|
|
||||||
const [maps, setMaps] = useState([]);
|
const [maps, setMaps] = useState([]);
|
||||||
const [mapStates, setMapStates] = useState([]);
|
const [mapStates, setMapStates] = useState([]);
|
||||||
|
const [mapsLoading, setMapsLoading] = useState(true);
|
||||||
|
|
||||||
// Load maps from the database and ensure state is properly setup
|
// Load maps from the database and ensure state is properly setup
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!userId || !database || databaseStatus === "loading") {
|
if (!userId || !database || databaseStatus === "loading") {
|
||||||
@ -60,15 +65,16 @@ export function MapDataProvider({ children }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadMaps() {
|
async function loadMaps() {
|
||||||
let storedMaps = [];
|
const worker = Comlink.wrap(new DatabaseWorker());
|
||||||
// Use a cursor instead of toArray to prevent IPC max size error
|
await worker.loadData("maps");
|
||||||
await database.table("maps").each((map) => storedMaps.push(map));
|
const storedMaps = await worker.data;
|
||||||
const sortedMaps = storedMaps.sort((a, b) => b.created - a.created);
|
const sortedMaps = storedMaps.sort((a, b) => b.created - a.created);
|
||||||
const defaultMapsWithIds = await getDefaultMaps();
|
const defaultMapsWithIds = await getDefaultMaps();
|
||||||
const allMaps = [...sortedMaps, ...defaultMapsWithIds];
|
const allMaps = [...sortedMaps, ...defaultMapsWithIds];
|
||||||
setMaps(allMaps);
|
setMaps(allMaps);
|
||||||
const storedStates = await database.table("states").toArray();
|
const storedStates = await database.table("states").toArray();
|
||||||
setMapStates(storedStates);
|
setMapStates(storedStates);
|
||||||
|
setMapsLoading(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadMaps();
|
loadMaps();
|
||||||
@ -137,8 +143,10 @@ export function MapDataProvider({ children }) {
|
|||||||
try {
|
try {
|
||||||
await database.table("maps").update(id, update);
|
await database.table("maps").update(id, update);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// if (error.name !== "QuotaExceededError") {
|
||||||
const map = (await getMapFromDB(id)) || {};
|
const map = (await getMapFromDB(id)) || {};
|
||||||
await database.table("maps").put({ ...map, id, ...update });
|
await database.table("maps").put({ ...map, id, ...update });
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
setMaps((prevMaps) => {
|
setMaps((prevMaps) => {
|
||||||
const newMaps = [...prevMaps];
|
const newMaps = [...prevMaps];
|
||||||
@ -247,6 +255,7 @@ export function MapDataProvider({ children }) {
|
|||||||
putMap,
|
putMap,
|
||||||
getMap,
|
getMap,
|
||||||
getMapFromDB,
|
getMapFromDB,
|
||||||
|
mapsLoading,
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
<MapDataContext.Provider value={value}>{children}</MapDataContext.Provider>
|
<MapDataContext.Provider value={value}>{children}</MapDataContext.Provider>
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
import React, { useEffect, useState, useContext } from "react";
|
import React, { useEffect, useState, useContext } from "react";
|
||||||
|
import * as Comlink from "comlink";
|
||||||
|
|
||||||
import AuthContext from "./AuthContext";
|
import AuthContext from "./AuthContext";
|
||||||
import DatabaseContext from "./DatabaseContext";
|
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 { tokens as defaultTokens } from "../tokens";
|
||||||
|
|
||||||
const TokenDataContext = React.createContext();
|
const TokenDataContext = React.createContext();
|
||||||
@ -14,6 +17,7 @@ export function TokenDataProvider({ children }) {
|
|||||||
const { userId } = useContext(AuthContext);
|
const { userId } = useContext(AuthContext);
|
||||||
|
|
||||||
const [tokens, setTokens] = useState([]);
|
const [tokens, setTokens] = useState([]);
|
||||||
|
const [tokensLoading, setTokensLoading] = useState(true);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!userId || !database || databaseStatus === "loading") {
|
if (!userId || !database || databaseStatus === "loading") {
|
||||||
@ -33,13 +37,14 @@ export function TokenDataProvider({ children }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadTokens() {
|
async function loadTokens() {
|
||||||
let storedTokens = [];
|
const worker = Comlink.wrap(new DatabaseWorker());
|
||||||
// Use a cursor instead of toArray to prevent IPC max size error
|
await worker.loadData("tokens");
|
||||||
await database.table("tokens").each((token) => storedTokens.push(token));
|
const storedTokens = await worker.data;
|
||||||
const sortedTokens = storedTokens.sort((a, b) => b.created - a.created);
|
const sortedTokens = storedTokens.sort((a, b) => b.created - a.created);
|
||||||
const defaultTokensWithIds = getDefaultTokes();
|
const defaultTokensWithIds = getDefaultTokes();
|
||||||
const allTokens = [...sortedTokens, ...defaultTokensWithIds];
|
const allTokens = [...sortedTokens, ...defaultTokensWithIds];
|
||||||
setTokens(allTokens);
|
setTokens(allTokens);
|
||||||
|
setTokensLoading(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadTokens();
|
loadTokens();
|
||||||
@ -160,6 +165,7 @@ export function TokenDataProvider({ children }) {
|
|||||||
putToken,
|
putToken,
|
||||||
getToken,
|
getToken,
|
||||||
tokensById,
|
tokensById,
|
||||||
|
tokensLoading,
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -53,6 +53,7 @@ function SelectMapModal({
|
|||||||
resetMap,
|
resetMap,
|
||||||
updateMap,
|
updateMap,
|
||||||
updateMaps,
|
updateMaps,
|
||||||
|
mapsLoading,
|
||||||
} = useContext(MapDataContext);
|
} = useContext(MapDataContext);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -388,7 +389,7 @@ function SelectMapModal({
|
|||||||
</Button>
|
</Button>
|
||||||
</Flex>
|
</Flex>
|
||||||
</ImageDrop>
|
</ImageDrop>
|
||||||
{imageLoading && <LoadingOverlay bg="overlay" />}
|
{(imageLoading || mapsLoading) && <LoadingOverlay bg="overlay" />}
|
||||||
<EditMapModal
|
<EditMapModal
|
||||||
isOpen={isEditModalOpen}
|
isOpen={isEditModalOpen}
|
||||||
onDone={() => setIsEditModalOpen(false)}
|
onDone={() => setIsEditModalOpen(false)}
|
||||||
|
@ -10,6 +10,7 @@ import ConfirmModal from "./ConfirmModal";
|
|||||||
import Modal from "../components/Modal";
|
import Modal from "../components/Modal";
|
||||||
import ImageDrop from "../components/ImageDrop";
|
import ImageDrop from "../components/ImageDrop";
|
||||||
import TokenTiles from "../components/token/TokenTiles";
|
import TokenTiles from "../components/token/TokenTiles";
|
||||||
|
import LoadingOverlay from "../components/LoadingOverlay";
|
||||||
|
|
||||||
import blobToBuffer from "../helpers/blobToBuffer";
|
import blobToBuffer from "../helpers/blobToBuffer";
|
||||||
import useKeyboard from "../helpers/useKeyboard";
|
import useKeyboard from "../helpers/useKeyboard";
|
||||||
@ -20,9 +21,13 @@ import AuthContext from "../contexts/AuthContext";
|
|||||||
|
|
||||||
function SelectTokensModal({ isOpen, onRequestClose }) {
|
function SelectTokensModal({ isOpen, onRequestClose }) {
|
||||||
const { userId } = useContext(AuthContext);
|
const { userId } = useContext(AuthContext);
|
||||||
const { ownedTokens, addToken, removeTokens, updateTokens } = useContext(
|
const {
|
||||||
TokenDataContext
|
ownedTokens,
|
||||||
);
|
addToken,
|
||||||
|
removeTokens,
|
||||||
|
updateTokens,
|
||||||
|
tokensLoading,
|
||||||
|
} = useContext(TokenDataContext);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search
|
* Search
|
||||||
@ -256,6 +261,7 @@ function SelectTokensModal({ isOpen, onRequestClose }) {
|
|||||||
</Button>
|
</Button>
|
||||||
</Flex>
|
</Flex>
|
||||||
</ImageDrop>
|
</ImageDrop>
|
||||||
|
{tokensLoading && <LoadingOverlay bg="overlay" />}
|
||||||
<EditTokenModal
|
<EditTokenModal
|
||||||
isOpen={isEditModalOpen}
|
isOpen={isEditModalOpen}
|
||||||
onDone={() => setIsEditModalOpen(false)}
|
onDone={() => setIsEditModalOpen(false)}
|
||||||
|
16
src/workers/DatabaseWorker.js
Normal file
16
src/workers/DatabaseWorker.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import * as Comlink from "comlink";
|
||||||
|
|
||||||
|
import { getDatabase } from "../database";
|
||||||
|
|
||||||
|
// Worker to load large amounts of database data on a separate thread
|
||||||
|
let obj = {
|
||||||
|
data: [],
|
||||||
|
async loadData(table) {
|
||||||
|
let db = getDatabase({});
|
||||||
|
this.data = [];
|
||||||
|
// Use a cursor instead of toArray to prevent IPC max size error
|
||||||
|
await db.table(table).each((map) => this.data.push(map));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
Comlink.expose(obj);
|
13
yarn.lock
13
yarn.lock
@ -3808,6 +3808,11 @@ combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
|
|||||||
dependencies:
|
dependencies:
|
||||||
delayed-stream "~1.0.0"
|
delayed-stream "~1.0.0"
|
||||||
|
|
||||||
|
comlink@^4.3.0:
|
||||||
|
version "4.3.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/comlink/-/comlink-4.3.0.tgz#80b3366baccd87897dab3638ebfcfae28b2f87c7"
|
||||||
|
integrity sha512-mu4KKKNuW8TvkfpW/H88HBPeILubBS6T94BdD1VWBXNXfiyqVtwUCVNO1GeNOBTsIswzsMjWlycYr+77F5b84g==
|
||||||
|
|
||||||
commander@^2.20.0:
|
commander@^2.20.0:
|
||||||
version "2.20.3"
|
version "2.20.3"
|
||||||
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
|
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
|
||||||
@ -12720,6 +12725,14 @@ worker-farm@^1.7.0:
|
|||||||
dependencies:
|
dependencies:
|
||||||
errno "~0.1.7"
|
errno "~0.1.7"
|
||||||
|
|
||||||
|
worker-loader@^3.0.5:
|
||||||
|
version "3.0.5"
|
||||||
|
resolved "https://registry.yarnpkg.com/worker-loader/-/worker-loader-3.0.5.tgz#6e13a583c4120ba419eece8e4f2e098b014311bf"
|
||||||
|
integrity sha512-cOh4UqTtvT8eHpyuuTK2C66Fg/G5Pb7g11bwtKm7uyD0vj2hCGY1APlSzVD75V9ciYZt44VPbFPiSFTSLxkQ+w==
|
||||||
|
dependencies:
|
||||||
|
loader-utils "^2.0.0"
|
||||||
|
schema-utils "^3.0.0"
|
||||||
|
|
||||||
worker-rpc@^0.1.0:
|
worker-rpc@^0.1.0:
|
||||||
version "0.1.1"
|
version "0.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/worker-rpc/-/worker-rpc-0.1.1.tgz#cb565bd6d7071a8f16660686051e969ad32f54d5"
|
resolved "https://registry.yarnpkg.com/worker-rpc/-/worker-rpc-0.1.1.tgz#cb565bd6d7071a8f16660686051e969ad32f54d5"
|
||||||
|
Loading…
Reference in New Issue
Block a user