mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 22:40:01 -04:00
Cookie saving to idb, I'm pretty sure this should be bug free
This commit is contained in:
parent
0cc7115c48
commit
300d1d6d2b
2 changed files with 35 additions and 10 deletions
|
@ -3,7 +3,7 @@ import { ScramjetFrame } from "./frame";
|
||||||
import { $scramjet, loadCodecs } from "../scramjet";
|
import { $scramjet, loadCodecs } from "../scramjet";
|
||||||
|
|
||||||
export class ScramjetController {
|
export class ScramjetController {
|
||||||
private store: IDBDatabase;
|
private db: IDBDatabase;
|
||||||
|
|
||||||
constructor(config: Partial<ScramjetConfig>) {
|
constructor(config: Partial<ScramjetConfig>) {
|
||||||
// sane ish defaults
|
// sane ish defaults
|
||||||
|
@ -86,14 +86,17 @@ export class ScramjetController {
|
||||||
|
|
||||||
return new Promise<IDBDatabase>((resolve, reject) => {
|
return new Promise<IDBDatabase>((resolve, reject) => {
|
||||||
db.onsuccess = async () => {
|
db.onsuccess = async () => {
|
||||||
this.store = db.result;
|
this.db = db.result;
|
||||||
await this.#saveConfig();
|
await this.#saveConfig();
|
||||||
resolve(db.result);
|
resolve(db.result);
|
||||||
};
|
};
|
||||||
db.onupgradeneeded = () => {
|
db.onupgradeneeded = () => {
|
||||||
const db2 = db.result;
|
const res = db.result;
|
||||||
if (!db2.objectStoreNames.contains("config")) {
|
if (!res.objectStoreNames.contains("config")) {
|
||||||
db2.createObjectStore("config");
|
res.createObjectStore("config");
|
||||||
|
}
|
||||||
|
if (!res.objectStoreNames.contains("cookies")) {
|
||||||
|
res.createObjectStore("cookies");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
db.onerror = () => reject(db.error);
|
db.onerror = () => reject(db.error);
|
||||||
|
@ -101,12 +104,12 @@ export class ScramjetController {
|
||||||
}
|
}
|
||||||
|
|
||||||
async #saveConfig() {
|
async #saveConfig() {
|
||||||
if (!this.store) {
|
if (!this.db) {
|
||||||
console.error("Store not ready!");
|
console.error("Store not ready!");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const tx = this.store.transaction("config", "readwrite");
|
const tx = this.db.transaction("config", "readwrite");
|
||||||
const store = tx.objectStore("config");
|
const store = tx.objectStore("config");
|
||||||
const req = store.put($scramjet.config, "config");
|
const req = store.put($scramjet.config, "config");
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,23 @@ export class ScramjetServiceWorker extends EventTarget {
|
||||||
super();
|
super();
|
||||||
this.client = new $scramjet.shared.util.BareClient();
|
this.client = new $scramjet.shared.util.BareClient();
|
||||||
|
|
||||||
addEventListener("message", ({ data }: { data: MessageC2W }) => {
|
const db = indexedDB.open("$scramjet", 1);
|
||||||
|
|
||||||
|
db.onsuccess = () => {
|
||||||
|
const res = db.result;
|
||||||
|
const tx = res.transaction("cookies", "readonly");
|
||||||
|
const store = tx.objectStore("cookies");
|
||||||
|
const cookies = store.get("cookies");
|
||||||
|
|
||||||
|
cookies.onsuccess = () => {
|
||||||
|
if (cookies.result) {
|
||||||
|
this.cookieStore.load(cookies.result);
|
||||||
|
dbg.log("Loaded cookies from IDB!");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
addEventListener("message", async ({ data }: { data: MessageC2W }) => {
|
||||||
if (!("scramjet$type" in data)) return;
|
if (!("scramjet$type" in data)) return;
|
||||||
|
|
||||||
if (data.scramjet$type === "registerServiceWorker") {
|
if (data.scramjet$type === "registerServiceWorker") {
|
||||||
|
@ -29,7 +45,14 @@ export class ScramjetServiceWorker extends EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.scramjet$type === "cookie") {
|
if (data.scramjet$type === "cookie") {
|
||||||
this.cookieStore.setCookies([data.cookie], new URL(data.url));
|
await this.cookieStore.setCookies([data.cookie], new URL(data.url));
|
||||||
|
const db = indexedDB.open("$scramjet", 1);
|
||||||
|
db.onsuccess = () => {
|
||||||
|
const res = db.result;
|
||||||
|
const tx = res.transaction("cookies", "readwrite");
|
||||||
|
const store = tx.objectStore("cookies");
|
||||||
|
store.put(this.cookieStore.dump(), "cookies");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -54,7 +77,6 @@ export class ScramjetServiceWorker extends EventTarget {
|
||||||
|
|
||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
|
|
||||||
config.onerror = () => reject(config.error);
|
config.onerror = () => reject(config.error);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue