69 lines
2 KiB
TypeScript
69 lines
2 KiB
TypeScript
import { APP_DATA_VERSION, dbStatusAtom, jotaiStore } from "../../src/globals";
|
|
import stepchartsUrl from "../../data/stepData.ndjson?url";
|
|
import { RpcProvider } from "worker-rpc";
|
|
import ndjsonStream from "../ndjsonStream";
|
|
|
|
async function init() {
|
|
const rpcProvider = new RpcProvider((message, transfer) =>
|
|
self.postMessage(message, undefined, transfer),
|
|
);
|
|
self.addEventListener("message", (evt) => rpcProvider.dispatch(evt.data));
|
|
|
|
rpcProvider.registerSignalHandler("start", async () => {
|
|
const result = await rpcProvider.rpc("db", {
|
|
cmd: "exec",
|
|
s: "SELECT version FROM _appDataVersion",
|
|
});
|
|
const appDataVersion = result[0].values[0][0];
|
|
if (appDataVersion === APP_DATA_VERSION) {
|
|
console.log(`data version is up to date! (${appDataVersion})`);
|
|
return;
|
|
}
|
|
|
|
console.log(
|
|
`outdated data version ${appDataVersion} < ${APP_DATA_VERSION}`,
|
|
);
|
|
await rpcProvider.rpc("db", {
|
|
cmd: "run",
|
|
s: "DELETE FROM charts",
|
|
});
|
|
|
|
const response = await fetch(stepchartsUrl);
|
|
const stream = ndjsonStream<ChartData>(response.body);
|
|
const reader = stream.getReader();
|
|
const iter = iterStream(reader);
|
|
|
|
// TODO: Actually stream this into the DB somehow?
|
|
// For slow connections where this download process actually takes quite a bit
|
|
const lol = [];
|
|
for await (const thing of iter) {
|
|
lol.push(thing);
|
|
}
|
|
console.log(lol);
|
|
console.log(`got ${lol.length} entries. inserting...`);
|
|
const h = await rpcProvider.rpc("db", {
|
|
cmd: "bulkInsert",
|
|
s: `
|
|
INSERT INTO charts
|
|
(artist, title, difficulty)
|
|
VALUES ($artist, $title, $difficulty)
|
|
`,
|
|
data: lol,
|
|
});
|
|
|
|
jotaiStore.set(dbStatusAtom, (prev) => ({
|
|
...prev,
|
|
lastUpdated: new Date().getTime(),
|
|
}));
|
|
});
|
|
}
|
|
|
|
async function* iterStream(reader: ReadableStreamReader<ChartData>) {
|
|
let result;
|
|
while (!result || !result.done) {
|
|
result = await reader.read();
|
|
if (result.value) yield result.value;
|
|
}
|
|
}
|
|
|
|
init();
|