feat(stores): sync automatically
This commit is contained in:
@ -275,3 +275,8 @@ export async function postHazard(location: WorldLocation, type: string) {
|
|||||||
}
|
}
|
||||||
return await res.json();
|
return await res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function ping() {
|
||||||
|
const res = await fetch(LNV_SERVER + "/ping").catch(() => ({ ok: false }));
|
||||||
|
return res.ok;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
// import { getStores, putStore } from "./lnv";
|
// import { getStores, putStore } from "./lnv";
|
||||||
|
|
||||||
import { openDB, type DBSchema } from "idb";
|
import { openDB, type DBSchema } from "idb";
|
||||||
import { authFetch } from "./lnv";
|
import { authFetch, hasCapability, ping } from "./lnv";
|
||||||
import { LNV_SERVER } from "./hosts";
|
import { LNV_SERVER } from "./hosts";
|
||||||
|
|
||||||
export interface Store {
|
export interface Store {
|
||||||
@ -61,7 +61,20 @@ export const db = await openDB<TCDB>("tc", 1, {
|
|||||||
|
|
||||||
const eventTarget = new EventTarget();
|
const eventTarget = new EventTarget();
|
||||||
|
|
||||||
|
export async function trySync() {
|
||||||
|
const pingResult = await ping();
|
||||||
|
if(!pingResult) {
|
||||||
|
console.warn("[STORES] [trySync] LNV server is not reachable, skipping sync");
|
||||||
|
return { success: false, message: "LNV server is not reachable" };
|
||||||
|
}
|
||||||
|
await syncStores();
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
|
||||||
export async function syncStores() {
|
export async function syncStores() {
|
||||||
|
if(!(await hasCapability("stores"))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const changes = await Promise.all(await db.getAll("changes").then(changes => changes.map(async change => {
|
const changes = await Promise.all(await db.getAll("changes").then(changes => changes.map(async change => {
|
||||||
const storeData = await db.get("stores", change.id);
|
const storeData = await db.get("stores", change.id);
|
||||||
return { id: change.id, operation: change.operation, data: storeData?.data || null, modified_at: storeData?.modified_at || new Date().toISOString(), type: storeData?.type || "location", name: storeData?.name || "" };
|
return { id: change.id, operation: change.operation, data: storeData?.data || null, modified_at: storeData?.modified_at || new Date().toISOString(), type: storeData?.type || "location", name: storeData?.name || "" };
|
||||||
@ -112,6 +125,7 @@ async function createStore(info: StoreInfo, data: object) {
|
|||||||
await tx.objectStore("changes").add({ id, operation: "create" });
|
await tx.objectStore("changes").add({ id, operation: "create" });
|
||||||
await tx.done;
|
await tx.done;
|
||||||
eventTarget.dispatchEvent(new CustomEvent("store-updated", { detail: store }) );
|
eventTarget.dispatchEvent(new CustomEvent("store-updated", { detail: store }) );
|
||||||
|
await trySync();
|
||||||
return store;
|
return store;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,6 +143,7 @@ export async function updateStore(info: StoreInfo, data: object | null) {
|
|||||||
await tx.objectStore("changes").add({ id: store.id, operation: "update" });
|
await tx.objectStore("changes").add({ id: store.id, operation: "update" });
|
||||||
await tx.done;
|
await tx.done;
|
||||||
eventTarget.dispatchEvent(new CustomEvent("store-updated", { detail: store }) );
|
eventTarget.dispatchEvent(new CustomEvent("store-updated", { detail: store }) );
|
||||||
|
await trySync();
|
||||||
return store;
|
return store;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { mount } from "svelte";
|
import { mount } from "svelte";
|
||||||
import "./app.css";
|
import "./app.css";
|
||||||
import App from "./App.svelte";
|
import App from "./App.svelte";
|
||||||
|
import { trySync } from "$lib/services/stores.svelte";
|
||||||
|
|
||||||
if (location.href.includes("/oidc")) {
|
if (location.href.includes("/oidc")) {
|
||||||
const url = new URL(location.href);
|
const url = new URL(location.href);
|
||||||
@ -22,4 +23,6 @@ const app = mount(App, {
|
|||||||
target: document.getElementById("app")!,
|
target: document.getElementById("app")!,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await trySync();
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
Reference in New Issue
Block a user