Skip to content

Commit

Permalink
login faster cache servers
Browse files Browse the repository at this point in the history
  • Loading branch information
Manwe-777 committed Oct 8, 2023
1 parent 31c62b8 commit cde116f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
28 changes: 25 additions & 3 deletions public/tooldb-worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,13 +822,35 @@ const toolDb = new mtgatool_db_1.ToolDb({
server: false,
});
toolDb.on("init", (key) => console.warn("ToolDb initialized!", key));
constants_1.DEFAULT_PEERS.forEach((peer) => {
const networkModule = toolDb.network;
networkModule.findServer(peer);
toolDb.store.get("servers", (err, data) => {
let serversData = {};
if (err) {
console.error("Error getting servers from cache:", err);
}
else if (data) {
try {
serversData = JSON.parse(data);
}
catch (_e) {
console.error("Error parsing servers from cache:", _e);
}
}
console.log("Got servers from cache:", serversData);
constants_1.DEFAULT_PEERS.forEach((peer) => {
const networkModule = toolDb.network;
if (serversData[peer]) {
networkModule.connectTo(serversData[peer]);
}
else {
networkModule.findServer(peer);
}
});
});
toolDb.onConnect = () => {
const networkModule = toolDb.network;
console.warn("ToolDb connected!");
self.postMessage({ type: "CONNECTED" });
toolDb.store.put("servers", JSON.stringify(networkModule.serverPeerData), () => console.log("Saved servers to cache", networkModule.serverPeerData));
};
self.toolDb = toolDb;
self.globalData = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function App(props: AppProps) {
const toolDbWorkerRef = useRef<Worker | null>(null);

useEffect(() => {
toolDbWorkerRef.current = new Worker("tooldb-worker/index.js", {
toolDbWorkerRef.current = new Worker("../tooldb-worker/index.js", {
type: "module",
});
window.toolDbWorker = toolDbWorkerRef.current;
Expand Down
34 changes: 30 additions & 4 deletions tooldb-worker/toolDbWorker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-restricted-globals */

import { ToolDb, ToolDbNetwork } from "mtgatool-db";
import { ServerPeerData, ToolDb, ToolDbNetwork } from "mtgatool-db";

import { DEFAULT_PEERS } from "./constants";
import doFunction from "./doFunction";
Expand All @@ -27,14 +27,40 @@ const toolDb = new ToolDb({

toolDb.on("init", (key) => console.warn("ToolDb initialized!", key));

DEFAULT_PEERS.forEach((peer) => {
const networkModule = toolDb.network as ToolDbNetwork;
networkModule.findServer(peer);
// Try to conenct to servers from cache
toolDb.store.get("servers", (err, data) => {
let serversData: Record<string, ServerPeerData> = {};
if (err) {
console.error("Error getting servers from cache:", err);
} else if (data) {
try {
serversData = JSON.parse(data);
} catch (_e) {
console.error("Error parsing servers from cache:", _e);
}
}

console.log("Got servers from cache:", serversData);

DEFAULT_PEERS.forEach((peer) => {
const networkModule = toolDb.network as ToolDbNetwork;
if (serversData[peer]) {
networkModule.connectTo(serversData[peer]);
} else {
networkModule.findServer(peer);
}
});
});

toolDb.onConnect = () => {
const networkModule = toolDb.network as ToolDbNetwork;
console.warn("ToolDb connected!");
self.postMessage({ type: "CONNECTED" });
toolDb.store.put(
"servers",
JSON.stringify(networkModule.serverPeerData),
() => console.log("Saved servers to cache", networkModule.serverPeerData)
);
};

self.toolDb = toolDb;
Expand Down

0 comments on commit cde116f

Please sign in to comment.