reorg
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Michael Zhang 2024-05-15 14:17:01 -05:00
parent cb553b52bd
commit bf1a71a58b
13 changed files with 112 additions and 25 deletions

View file

@ -6,6 +6,7 @@
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "vite build", "build": "vite build",
"check": "tsc",
"generate-pwa-assets": "pwa-assets-generator" "generate-pwa-assets": "pwa-assets-generator"
}, },
"type": "module", "type": "module",

View file

@ -6,8 +6,8 @@ import { Readable } from "node:stream";
import * as ndjson from "ndjson"; import * as ndjson from "ndjson";
import { readdir, writeFile, stat } from "node:fs/promises"; import { readdir, writeFile, stat } from "node:fs/promises";
import { createWriteStream } from "node:fs"; import { createWriteStream } from "node:fs";
import { parseSm } from "../lib/parseSm"; import { parseSm } from "../lib/stepcharts/parseSm";
import { parseSimfile } from "../lib/parseSimfile"; import { parseSimfile } from "../lib/stepcharts/parseSimfile";
const rootDir = process.env.STEPCHARTS_DIR ?? "/tmp/stepcharts"; const rootDir = process.env.STEPCHARTS_DIR ?? "/tmp/stepcharts";
const stepchartsDir = join(rootDir, "prodStepcharts"); const stepchartsDir = join(rootDir, "prodStepcharts");

View file

@ -20,6 +20,7 @@ import Scores from "./pages/Scores";
import Settings from "./pages/Settings"; import Settings from "./pages/Settings";
import ImportChartWorker from "./importCharts?worker"; import ImportChartWorker from "./importCharts?worker";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { CHART_STORE_CREATION_EVENT } from "./globals";
const queryClient = new QueryClient(); const queryClient = new QueryClient();
@ -76,6 +77,12 @@ export function AppWrapper() {
export default function App() { export default function App() {
useEffect(() => { useEffect(() => {
const worker = new ImportChartWorker(); const worker = new ImportChartWorker();
worker.onmessage = (evt) => {
console.log("got event from web worker", evt);
if (evt.kind === "chartStoreCreate") {
document.dispatchEvent(CHART_STORE_CREATION_EVENT);
}
};
}); });
return ( return (

View file

@ -1 +0,0 @@
export const APP_DATA_VERSION = 1;

4
src/globals.ts Normal file
View file

@ -0,0 +1,4 @@
// export const APP_DATA_VERSION = 1;
export const APP_DATA_VERSION =
Math.floor(new Date().getTime() / 1000) % 1000000;
export const CHART_STORE_CREATION_EVENT = new Event("chartStoreCreate");

View file

@ -1,5 +1,5 @@
import stepchartsUrl from "../data/stepData.ndjson?url"; import stepchartsUrl from "../data/stepData.ndjson?url";
import { APP_DATA_VERSION } from "./constants"; import { APP_DATA_VERSION, CHART_STORE_CREATION_EVENT } from "./globals";
import ndjsonStream from "can-ndjson-stream"; import ndjsonStream from "can-ndjson-stream";
export async function importCharts() { export async function importCharts() {
@ -7,6 +7,8 @@ export async function importCharts() {
db: { result: db }, db: { result: db },
refetchCharts, refetchCharts,
} = await openDb(); } = await openDb();
console.log("refetch", refetchCharts);
console.log("db", db); console.log("db", db);
if (refetchCharts) { if (refetchCharts) {
@ -62,16 +64,8 @@ async function* iterStream(reader: ReadableStreamReader) {
} }
} }
function openDb() { function migrateToVersion1(db: IDBOpenDBRequest) {
return new Promise((resolve) => { try {
const db = indexedDB.open("ddrDb", APP_DATA_VERSION);
let refetchCharts = false;
db.onupgradeneeded = (evt) => {
console.log("UPGRADE", evt.oldVersion, evt.newVersion);
refetchCharts = true;
{
const store = db.result.createObjectStore("chartStore", { const store = db.result.createObjectStore("chartStore", {
// TODO: Try to use the Konami ID here // TODO: Try to use the Konami ID here
autoIncrement: true, autoIncrement: true,
@ -79,12 +73,42 @@ function openDb() {
store.createIndex("title", "title", { unique: false }); store.createIndex("title", "title", { unique: false });
store.createIndex("artist", "artist", { unique: false }); store.createIndex("artist", "artist", { unique: false });
store.createIndex("mode", "mode", { unique: false }); store.createIndex("mode", "mode", { unique: false });
console.log("created object store");
self.postMessage({ kind: "chartStoreCreate" });
} catch (e) {}
} }
};
db.onsuccess = () => { function openDb() {
return new Promise((resolve) => {
console.log("opening db...");
const db = indexedDB.open("ddrDb", APP_DATA_VERSION);
let refetchCharts = false;
db.addEventListener("error", (evt) => {
console.log("ERROR", evt);
});
db.addEventListener("blocked", (evt) => {
console.log("BLOCKED", evt);
});
db.addEventListener("upgradeneeded", (evt) => {
console.log("IDB need upgrade", evt.oldVersion, "to", evt.newVersion);
refetchCharts = true;
migrateToVersion1(db);
console.log("done upgrading");
});
db.addEventListener("success", (evt) => {
console.log("IDB success", db.result.version, evt);
resolve({ db, refetchCharts }); resolve({ db, refetchCharts });
}; });
}); });
} }
await importCharts(); addEventListener("message", (evt) => {
console.log("message!", evt);
importCharts();
});

View file

@ -1,23 +1,73 @@
import { useQuery, useQueryClient } from "@tanstack/react-query"; import { useQuery, useQueryClient } from "@tanstack/react-query";
import { APP_DATA_VERSION } from "../constants"; import { APP_DATA_VERSION, CHART_STORE_CREATION_EVENT } from "../globals";
function openDb(): Promise<IDBOpenDBRequest> {
return new Promise((resolve) => {
const req = indexedDB.open("ddrDb", APP_DATA_VERSION);
req.onsuccess = (evt) => resolve(req);
});
}
async function openStore(): Promise<IDBObjectStore> {
const db = await openDb();
const tx = db.result.transaction("chartStore", "readonly");
return tx.objectStore("chartStore");
}
async function fetchCharts() { async function fetchCharts() {
const db = indexedDB.open("ddrDb", APP_DATA_VERSION); let store = null;
return []; try {
store = await openStore();
} catch (e) {
console.log("could not open store", e.message);
}
if (!store) {
await new Promise<void>((resolve) => {
document.addEventListener("chartStoreCreate", (evt) => {
console.log("SHIET");
resolve();
});
});
store = await openStore();
}
const entries = await new Promise<any[]>((resolve) => {
const req = store.getAll(undefined, 100);
req.onsuccess = (evt) => resolve(req.result);
});
console.log("entries", entries);
return entries;
} }
export default function Charts() { export default function Charts() {
const queryClient = useQueryClient();
const fetchChartsQuery = useQuery({ const fetchChartsQuery = useQuery({
queryKey: ["fetchCharts"], queryKey: ["fetchCharts"],
queryFn: fetchCharts, queryFn: fetchCharts,
}); });
let inner = undefined;
if (fetchChartsQuery.isSuccess) {
inner = (
<div>
{fetchChartsQuery.data.map((chart) => (
// <div>{JSON.stringify(Object.keys(chart))}</div>
<div>{JSON.stringify(chart.title)}</div>
))}
</div>
);
}
return ( return (
<> <>
<h1>Charts</h1> <h1>Charts</h1>
{JSON.stringify(fetchChartsQuery)} {fetchChartsQuery.status}
{fetchChartsQuery.error?.message}
{inner}
</> </>
); );
} }

View file

@ -3,6 +3,8 @@
"jsx": "react-jsx", "jsx": "react-jsx",
"module": "ESNext", "module": "ESNext",
"target": "ESNext", "target": "ESNext",
"moduleResolution": "Node" "moduleResolution": "Node",
"skipLibCheck": true,
"noEmit": true
} }
} }