Remove worker loading from map and token data contexts
This commit is contained in:
parent
159627072d
commit
52fad1171a
@ -1,5 +1,4 @@
|
|||||||
import React, { useEffect, useState, useContext, useCallback } from "react";
|
import React, { useEffect, useState, useContext, useCallback } from "react";
|
||||||
import { decode } from "@msgpack/msgpack";
|
|
||||||
|
|
||||||
import { useAuth } from "./AuthContext";
|
import { useAuth } from "./AuthContext";
|
||||||
import { useDatabase } from "./DatabaseContext";
|
import { useDatabase } from "./DatabaseContext";
|
||||||
@ -19,7 +18,7 @@ const defaultMapState = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function MapDataProvider({ children }) {
|
export function MapDataProvider({ children }) {
|
||||||
const { database, databaseStatus, worker } = useDatabase();
|
const { database, databaseStatus } = useDatabase();
|
||||||
const { userId } = useAuth();
|
const { userId } = useAuth();
|
||||||
|
|
||||||
const [maps, setMaps] = useState([]);
|
const [maps, setMaps] = useState([]);
|
||||||
@ -34,18 +33,7 @@ export function MapDataProvider({ children }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadMaps() {
|
async function loadMaps() {
|
||||||
let storedMaps = [];
|
const storedMaps = await database.table("maps").toArray();
|
||||||
// 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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
setMaps(storedMaps);
|
setMaps(storedMaps);
|
||||||
const storedStates = await database.table("states").toArray();
|
const storedStates = await database.table("states").toArray();
|
||||||
setMapStates(storedStates);
|
setMapStates(storedStates);
|
||||||
@ -56,7 +44,7 @@ export function MapDataProvider({ children }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadMaps();
|
loadMaps();
|
||||||
}, [userId, database, databaseStatus, worker]);
|
}, [userId, database, databaseStatus]);
|
||||||
|
|
||||||
const getMap = useCallback(
|
const getMap = useCallback(
|
||||||
async (mapId) => {
|
async (mapId) => {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import React, { useEffect, useState, useContext, useCallback } from "react";
|
import React, { useEffect, useState, useContext, useCallback } from "react";
|
||||||
import { decode } from "@msgpack/msgpack";
|
|
||||||
|
|
||||||
import { useAuth } from "./AuthContext";
|
import { useAuth } from "./AuthContext";
|
||||||
import { useDatabase } from "./DatabaseContext";
|
import { useDatabase } from "./DatabaseContext";
|
||||||
@ -10,7 +9,7 @@ import { removeGroupsItems } from "../helpers/group";
|
|||||||
const TokenDataContext = React.createContext();
|
const TokenDataContext = React.createContext();
|
||||||
|
|
||||||
export function TokenDataProvider({ children }) {
|
export function TokenDataProvider({ children }) {
|
||||||
const { database, databaseStatus, worker } = useDatabase();
|
const { database, databaseStatus } = useDatabase();
|
||||||
const { userId } = useAuth();
|
const { userId } = useAuth();
|
||||||
|
|
||||||
const [tokens, setTokens] = useState([]);
|
const [tokens, setTokens] = useState([]);
|
||||||
@ -23,17 +22,7 @@ export function TokenDataProvider({ children }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadTokens() {
|
async function loadTokens() {
|
||||||
let storedTokens = [];
|
const storedTokens = await database.table("tokens").toArray();
|
||||||
// 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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
setTokens(storedTokens);
|
setTokens(storedTokens);
|
||||||
const group = await database.table("groups").get("tokens");
|
const group = await database.table("groups").get("tokens");
|
||||||
const storedGroups = group.items;
|
const storedGroups = group.items;
|
||||||
@ -42,7 +31,7 @@ export function TokenDataProvider({ children }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadTokens();
|
loadTokens();
|
||||||
}, [userId, database, databaseStatus, worker]);
|
}, [userId, database, databaseStatus]);
|
||||||
|
|
||||||
const getToken = useCallback(
|
const getToken = useCallback(
|
||||||
async (tokenId) => {
|
async (tokenId) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user