Remove worker loading from map and token data contexts

This commit is contained in:
Mitchell McCaffrey 2021-06-11 15:38:23 +10:00
parent 159627072d
commit 52fad1171a
2 changed files with 6 additions and 29 deletions

View File

@ -1,5 +1,4 @@
import React, { useEffect, useState, useContext, useCallback } from "react";
import { decode } from "@msgpack/msgpack";
import { useAuth } from "./AuthContext";
import { useDatabase } from "./DatabaseContext";
@ -19,7 +18,7 @@ const defaultMapState = {
};
export function MapDataProvider({ children }) {
const { database, databaseStatus, worker } = useDatabase();
const { database, databaseStatus } = useDatabase();
const { userId } = useAuth();
const [maps, setMaps] = useState([]);
@ -34,18 +33,7 @@ export function MapDataProvider({ children }) {
}
async function loadMaps() {
let storedMaps = [];
// Try to load maps with worker, fallback to database if failed
const packedMaps = await worker.loadData("maps");
// let packedMaps;
if (packedMaps) {
storedMaps = decode(packedMaps);
} else {
console.warn("Unable to load maps with worker, loading may be slow");
await database.table("maps").each((map) => {
storedMaps.push(map);
});
}
const storedMaps = await database.table("maps").toArray();
setMaps(storedMaps);
const storedStates = await database.table("states").toArray();
setMapStates(storedStates);
@ -56,7 +44,7 @@ export function MapDataProvider({ children }) {
}
loadMaps();
}, [userId, database, databaseStatus, worker]);
}, [userId, database, databaseStatus]);
const getMap = useCallback(
async (mapId) => {

View File

@ -1,5 +1,4 @@
import React, { useEffect, useState, useContext, useCallback } from "react";
import { decode } from "@msgpack/msgpack";
import { useAuth } from "./AuthContext";
import { useDatabase } from "./DatabaseContext";
@ -10,7 +9,7 @@ import { removeGroupsItems } from "../helpers/group";
const TokenDataContext = React.createContext();
export function TokenDataProvider({ children }) {
const { database, databaseStatus, worker } = useDatabase();
const { database, databaseStatus } = useDatabase();
const { userId } = useAuth();
const [tokens, setTokens] = useState([]);
@ -23,17 +22,7 @@ export function TokenDataProvider({ children }) {
}
async function loadTokens() {
let storedTokens = [];
// Try to load tokens with worker, fallback to database if failed
const packedTokens = await worker.loadData("tokens");
if (packedTokens) {
storedTokens = decode(packedTokens);
} else {
console.warn("Unable to load tokens with worker, loading may be slow");
await database.table("tokens").each((token) => {
storedTokens.push(token);
});
}
const storedTokens = await database.table("tokens").toArray();
setTokens(storedTokens);
const group = await database.table("groups").get("tokens");
const storedGroups = group.items;
@ -42,7 +31,7 @@ export function TokenDataProvider({ children }) {
}
loadTokens();
}, [userId, database, databaseStatus, worker]);
}, [userId, database, databaseStatus]);
const getToken = useCallback(
async (tokenId) => {