From 243d5c64372924e68d345ba95b14eb8c288c475b Mon Sep 17 00:00:00 2001 From: Jannik Date: Sat, 27 Sep 2025 19:46:37 +0200 Subject: [PATCH] feat(stores): sync automatically --- src/lib/services/lnv.ts | 5 +++++ src/lib/services/stores.svelte.ts | 17 ++++++++++++++++- src/main.ts | 3 +++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/lib/services/lnv.ts b/src/lib/services/lnv.ts index 6cdd1bd..ec2a7b7 100644 --- a/src/lib/services/lnv.ts +++ b/src/lib/services/lnv.ts @@ -275,3 +275,8 @@ export async function postHazard(location: WorldLocation, type: string) { } return await res.json(); } + +export async function ping() { + const res = await fetch(LNV_SERVER + "/ping").catch(() => ({ ok: false })); + return res.ok; +} diff --git a/src/lib/services/stores.svelte.ts b/src/lib/services/stores.svelte.ts index 1af4446..7c821c4 100644 --- a/src/lib/services/stores.svelte.ts +++ b/src/lib/services/stores.svelte.ts @@ -1,7 +1,7 @@ // import { getStores, putStore } from "./lnv"; import { openDB, type DBSchema } from "idb"; -import { authFetch } from "./lnv"; +import { authFetch, hasCapability, ping } from "./lnv"; import { LNV_SERVER } from "./hosts"; export interface Store { @@ -61,7 +61,20 @@ export const db = await openDB("tc", 1, { 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() { + if(!(await hasCapability("stores"))) { + return; + } const changes = await Promise.all(await db.getAll("changes").then(changes => changes.map(async change => { 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 || "" }; @@ -112,6 +125,7 @@ async function createStore(info: StoreInfo, data: object) { await tx.objectStore("changes").add({ id, operation: "create" }); await tx.done; eventTarget.dispatchEvent(new CustomEvent("store-updated", { detail: store }) ); + await trySync(); 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.done; eventTarget.dispatchEvent(new CustomEvent("store-updated", { detail: store }) ); + await trySync(); return store; } diff --git a/src/main.ts b/src/main.ts index 568f0bf..76eaf89 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import { mount } from "svelte"; import "./app.css"; import App from "./App.svelte"; +import { trySync } from "$lib/services/stores.svelte"; if (location.href.includes("/oidc")) { const url = new URL(location.href); @@ -22,4 +23,6 @@ const app = mount(App, { target: document.getElementById("app")!, }); +await trySync(); + export default app;