style: run prettier
This commit is contained in:
@ -5,7 +5,7 @@ export const hazards: Hazard[] = $state([]);
|
||||
|
||||
export async function fetchHazards() {
|
||||
if (!location.available) return;
|
||||
if (!await hasCapability("hazards")) return;
|
||||
if (!(await hasCapability("hazards"))) return;
|
||||
const newHazards = await getHazards(
|
||||
{ lat: location.lat, lon: location.lng },
|
||||
100,
|
||||
|
||||
@ -56,15 +56,17 @@ export const db = await openDB<TCDB>("tc", 1, {
|
||||
if (!db.objectStoreNames.contains("changes")) {
|
||||
db.createObjectStore("changes", { keyPath: "id" });
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
|
||||
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");
|
||||
console.warn(
|
||||
"[STORES] [trySync] LNV server is not reachable, skipping sync",
|
||||
);
|
||||
return { success: false, message: "LNV server is not reachable" };
|
||||
}
|
||||
await syncStores();
|
||||
@ -75,26 +77,44 @@ 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);
|
||||
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 || "" };
|
||||
})));
|
||||
const stores = await db.getAll("stores").then(stores => stores.map(store => ({ id: store.id, modified_at: store.modified_at })));
|
||||
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 || "",
|
||||
};
|
||||
}),
|
||||
),
|
||||
);
|
||||
const stores = await db
|
||||
.getAll("stores")
|
||||
.then((stores) =>
|
||||
stores.map((store) => ({ id: store.id, modified_at: store.modified_at })),
|
||||
);
|
||||
const res = await authFetch(LNV_SERVER + "/stores/sync", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
changes,
|
||||
stores
|
||||
})
|
||||
})
|
||||
stores,
|
||||
}),
|
||||
});
|
||||
if (!res.ok) {
|
||||
console.error("[STORES] [syncStores] Failed to sync stores:", await res.text());
|
||||
console.error(
|
||||
"[STORES] [syncStores] Failed to sync stores:",
|
||||
await res.text(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
const data = await res.json() as { changes: Store[]; };
|
||||
const data = (await res.json()) as { changes: Store[] };
|
||||
const tx = db.transaction(["stores", "changes"], "readwrite");
|
||||
// Apply all changes the server sent us
|
||||
for (const store of data.changes) {
|
||||
@ -103,7 +123,9 @@ export async function syncStores() {
|
||||
} else {
|
||||
await tx.objectStore("stores").put(store);
|
||||
}
|
||||
eventTarget.dispatchEvent(new CustomEvent("store-updated", { detail: store }) );
|
||||
eventTarget.dispatchEvent(
|
||||
new CustomEvent("store-updated", { detail: store }),
|
||||
);
|
||||
}
|
||||
// Delete all changes
|
||||
await tx.objectStore("changes").clear();
|
||||
@ -118,19 +140,24 @@ async function createStore(info: StoreInfo, data: object) {
|
||||
type: info.type,
|
||||
owner_id: "", // TODO
|
||||
data: JSON.stringify(data),
|
||||
modified_at: new Date().toISOString()
|
||||
modified_at: new Date().toISOString(),
|
||||
};
|
||||
const tx = db.transaction(["stores", "changes"], "readwrite");
|
||||
await tx.objectStore("stores").add(store);
|
||||
await tx.objectStore("changes").add({ id, operation: "create" });
|
||||
await tx.done;
|
||||
eventTarget.dispatchEvent(new CustomEvent("store-updated", { detail: store }) );
|
||||
eventTarget.dispatchEvent(
|
||||
new CustomEvent("store-updated", { detail: store }),
|
||||
);
|
||||
await trySync();
|
||||
return store;
|
||||
}
|
||||
|
||||
export async function updateStore(info: StoreInfo, data: object | null) {
|
||||
const store = await db.getFromIndex("stores", "by-name-and-type", [info.name, info.type]);
|
||||
const store = await db.getFromIndex("stores", "by-name-and-type", [
|
||||
info.name,
|
||||
info.type,
|
||||
]);
|
||||
if (!store) {
|
||||
if (data === null) return;
|
||||
return await createStore(info, data);
|
||||
@ -142,7 +169,9 @@ export async function updateStore(info: StoreInfo, data: object | null) {
|
||||
await tx.objectStore("stores").put(store);
|
||||
await tx.objectStore("changes").add({ id: store.id, operation: "update" });
|
||||
await tx.done;
|
||||
eventTarget.dispatchEvent(new CustomEvent("store-updated", { detail: store }) );
|
||||
eventTarget.dispatchEvent(
|
||||
new CustomEvent("store-updated", { detail: store }),
|
||||
);
|
||||
await trySync();
|
||||
return store;
|
||||
}
|
||||
@ -166,19 +195,33 @@ export async function updateStore(info: StoreInfo, data: object | null) {
|
||||
// return state;
|
||||
// }
|
||||
|
||||
export function stores<T extends object>(type: StoreType): WrappedValue<StoreValue<T>[]> {
|
||||
export function stores<T extends object>(
|
||||
type: StoreType,
|
||||
): WrappedValue<StoreValue<T>[]> {
|
||||
const state = $state<StoreValue<T>[]>([]);
|
||||
eventTarget.addEventListener("store-updated", async (event) => {
|
||||
const customEvent = event as CustomEvent;
|
||||
const updatedStore = customEvent.detail as Store;
|
||||
if (updatedStore.type === type) {
|
||||
const stores = await db.getAllFromIndex("stores", "by-type", type);
|
||||
state.splice(0, state.length, ...(stores.map(store => ({ ...store, data: JSON.parse(store.data) as T })).filter(store => store.data !== null)));
|
||||
state.splice(
|
||||
0,
|
||||
state.length,
|
||||
...stores
|
||||
.map((store) => ({ ...store, data: JSON.parse(store.data) as T }))
|
||||
.filter((store) => store.data !== null),
|
||||
);
|
||||
}
|
||||
});
|
||||
(async () => {
|
||||
const stores = await db.getAllFromIndex("stores", "by-type", type);
|
||||
state.splice(0, state.length, ...(stores.map(store => ({ ...store, data: JSON.parse(store.data) as T })).filter(store => store.data !== null)));
|
||||
state.splice(
|
||||
0,
|
||||
state.length,
|
||||
...stores
|
||||
.map((store) => ({ ...store, data: JSON.parse(store.data) as T }))
|
||||
.filter((store) => store.data !== null),
|
||||
);
|
||||
})();
|
||||
return {
|
||||
get current() {
|
||||
@ -186,6 +229,6 @@ export function stores<T extends object>(type: StoreType): WrappedValue<StoreVal
|
||||
},
|
||||
set current(newValue: StoreValue<T>[]) {
|
||||
state.splice(0, state.length, ...newValue);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,5 +1,10 @@
|
||||
import { m } from "$lang/messages";
|
||||
import { stores, updateStore, type StoreValue, type WrappedValue } from "$lib/services/stores.svelte";
|
||||
import {
|
||||
stores,
|
||||
updateStore,
|
||||
type StoreValue,
|
||||
type WrappedValue,
|
||||
} from "$lib/services/stores.svelte";
|
||||
|
||||
/*
|
||||
Valhalla costing:
|
||||
@ -80,7 +85,8 @@ export const selectedVehicleIdx: StateValue<number | null> = $state({
|
||||
});
|
||||
export const selectedVehicle: () => Vehicle | null = () => {
|
||||
return (
|
||||
vehicles.current[selectedVehicleIdx.v !== null ? selectedVehicleIdx.v : 0]?.data ?? null
|
||||
vehicles.current[selectedVehicleIdx.v !== null ? selectedVehicleIdx.v : 0]
|
||||
?.data ?? null
|
||||
);
|
||||
};
|
||||
|
||||
@ -100,7 +106,9 @@ export function selectVehicle(vehicle: Vehicle | null) {
|
||||
if (vehicle == null) {
|
||||
selectedVehicleIdx.v = null;
|
||||
} else {
|
||||
selectedVehicleIdx.v = vehicles.current.findIndex((v) => v.name === vehicle.name);
|
||||
selectedVehicleIdx.v = vehicles.current.findIndex(
|
||||
(v) => v.name === vehicle.name,
|
||||
);
|
||||
if (selectedVehicleIdx.v === -1) {
|
||||
selectedVehicleIdx.v = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user