2024-05-15 17:57:16 +00:00
|
|
|
import stepchartsUrl from "../data/stepData.ndjson?url";
|
2024-05-15 19:28:21 +00:00
|
|
|
import ndjsonStream from "../lib/ndjsonStream";
|
2024-05-15 19:17:01 +00:00
|
|
|
import { APP_DATA_VERSION, CHART_STORE_CREATION_EVENT } from "./globals";
|
2024-05-15 17:57:16 +00:00
|
|
|
|
|
|
|
export async function importCharts() {
|
|
|
|
const {
|
|
|
|
db: { result: db },
|
|
|
|
refetchCharts,
|
|
|
|
} = await openDb();
|
2024-05-15 19:17:01 +00:00
|
|
|
|
|
|
|
console.log("refetch", refetchCharts);
|
2024-05-15 17:57:16 +00:00
|
|
|
console.log("db", db);
|
|
|
|
|
|
|
|
if (refetchCharts) {
|
|
|
|
const response = await fetch(stepchartsUrl);
|
|
|
|
const reader = ndjsonStream(response.body).getReader();
|
|
|
|
const iter = iterStream(reader);
|
|
|
|
|
|
|
|
await addToDb(db, iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addToDb(db: IDBDatabase, iter: AsyncGenerator<any>): Promise<void> {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
function openTransaction() {
|
|
|
|
const tx = db.transaction("chartStore", "readwrite");
|
|
|
|
return tx.objectStore("chartStore");
|
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
let batch = [];
|
|
|
|
for await (const obj of iter) {
|
|
|
|
if (batch.length >= 100) {
|
|
|
|
const store = openTransaction();
|
|
|
|
for (const obj of batch) {
|
|
|
|
const req = store.add(obj);
|
|
|
|
req.onsuccess = (evt) => {
|
|
|
|
console.log("Inserted", evt.target.result);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
batch = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.push(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
const store = openTransaction();
|
|
|
|
for (const obj of batch) {
|
|
|
|
const req = store.add(obj);
|
|
|
|
req.onsuccess = (evt) => {
|
|
|
|
console.log("Inserted", evt.target.result);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
batch = [];
|
|
|
|
})();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function* iterStream(reader: ReadableStreamReader) {
|
|
|
|
let result;
|
|
|
|
while (!result || !result.done) {
|
|
|
|
result = await reader.read();
|
|
|
|
yield result.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 19:17:01 +00:00
|
|
|
function migrateToVersion1(db: IDBOpenDBRequest) {
|
|
|
|
try {
|
|
|
|
const store = db.result.createObjectStore("chartStore", {
|
|
|
|
// TODO: Try to use the Konami ID here
|
|
|
|
autoIncrement: true,
|
|
|
|
});
|
|
|
|
store.createIndex("title", "title", { unique: false });
|
|
|
|
store.createIndex("artist", "artist", { unique: false });
|
|
|
|
store.createIndex("mode", "mode", { unique: false });
|
|
|
|
console.log("created object store");
|
|
|
|
self.postMessage({ kind: "chartStoreCreate" });
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
2024-05-15 17:57:16 +00:00
|
|
|
function openDb() {
|
|
|
|
return new Promise((resolve) => {
|
2024-05-15 19:17:01 +00:00
|
|
|
console.log("opening db...");
|
2024-05-15 17:57:16 +00:00
|
|
|
const db = indexedDB.open("ddrDb", APP_DATA_VERSION);
|
2024-05-15 19:17:01 +00:00
|
|
|
|
2024-05-15 17:57:16 +00:00
|
|
|
let refetchCharts = false;
|
|
|
|
|
2024-05-15 19:17:01 +00:00
|
|
|
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);
|
2024-05-15 17:57:16 +00:00
|
|
|
refetchCharts = true;
|
2024-05-15 19:17:01 +00:00
|
|
|
migrateToVersion1(db);
|
2024-05-15 17:57:16 +00:00
|
|
|
|
2024-05-15 19:17:01 +00:00
|
|
|
console.log("done upgrading");
|
|
|
|
});
|
|
|
|
|
|
|
|
db.addEventListener("success", (evt) => {
|
|
|
|
console.log("IDB success", db.result.version, evt);
|
2024-05-15 17:57:16 +00:00
|
|
|
resolve({ db, refetchCharts });
|
2024-05-15 19:17:01 +00:00
|
|
|
});
|
2024-05-15 17:57:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-15 19:17:01 +00:00
|
|
|
addEventListener("message", (evt) => {
|
|
|
|
console.log("message!", evt);
|
|
|
|
importCharts();
|
|
|
|
});
|