style: run prettier
This commit is contained in:
@ -5,7 +5,7 @@ export const hazards: Hazard[] = $state([]);
|
|||||||
|
|
||||||
export async function fetchHazards() {
|
export async function fetchHazards() {
|
||||||
if (!location.available) return;
|
if (!location.available) return;
|
||||||
if (!await hasCapability("hazards")) return;
|
if (!(await hasCapability("hazards"))) return;
|
||||||
const newHazards = await getHazards(
|
const newHazards = await getHazards(
|
||||||
{ lat: location.lat, lon: location.lng },
|
{ lat: location.lat, lon: location.lng },
|
||||||
100,
|
100,
|
||||||
|
|||||||
@ -46,25 +46,27 @@ interface TCDB extends DBSchema {
|
|||||||
|
|
||||||
export const db = await openDB<TCDB>("tc", 1, {
|
export const db = await openDB<TCDB>("tc", 1, {
|
||||||
upgrade(db, _oldVersion, _newVersion, _transaction, _event) {
|
upgrade(db, _oldVersion, _newVersion, _transaction, _event) {
|
||||||
if(!db.objectStoreNames.contains("stores")) {
|
if (!db.objectStoreNames.contains("stores")) {
|
||||||
const store = db.createObjectStore("stores", { keyPath: "id" });
|
const store = db.createObjectStore("stores", { keyPath: "id" });
|
||||||
store.createIndex("by-type", "type");
|
store.createIndex("by-type", "type");
|
||||||
store.createIndex("by-owner", "owner_id");
|
store.createIndex("by-owner", "owner_id");
|
||||||
store.createIndex("by-type-and-owner", ["type", "owner_id"]);
|
store.createIndex("by-type-and-owner", ["type", "owner_id"]);
|
||||||
store.createIndex("by-name-and-type", ["name", "type"]);
|
store.createIndex("by-name-and-type", ["name", "type"]);
|
||||||
}
|
}
|
||||||
if(!db.objectStoreNames.contains("changes")) {
|
if (!db.objectStoreNames.contains("changes")) {
|
||||||
db.createObjectStore("changes", { keyPath: "id" });
|
db.createObjectStore("changes", { keyPath: "id" });
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
const eventTarget = new EventTarget();
|
const eventTarget = new EventTarget();
|
||||||
|
|
||||||
export async function trySync() {
|
export async function trySync() {
|
||||||
const pingResult = await ping();
|
const pingResult = await ping();
|
||||||
if(!pingResult) {
|
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" };
|
return { success: false, message: "LNV server is not reachable" };
|
||||||
}
|
}
|
||||||
await syncStores();
|
await syncStores();
|
||||||
@ -72,38 +74,58 @@ export async function trySync() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function syncStores() {
|
export async function syncStores() {
|
||||||
if(!(await hasCapability("stores"))) {
|
if (!(await hasCapability("stores"))) {
|
||||||
return;
|
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,
|
||||||
const stores = await db.getAll("stores").then(stores => stores.map(store => ({ id: store.id, modified_at: store.modified_at })));
|
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", {
|
const res = await authFetch(LNV_SERVER + "/stores/sync", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
changes,
|
changes,
|
||||||
stores
|
stores,
|
||||||
})
|
}),
|
||||||
})
|
});
|
||||||
if(!res.ok) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
const data = await res.json() as { changes: Store[]; };
|
const data = (await res.json()) as { changes: Store[] };
|
||||||
const tx = db.transaction(["stores", "changes"], "readwrite");
|
const tx = db.transaction(["stores", "changes"], "readwrite");
|
||||||
// Apply all changes the server sent us
|
// Apply all changes the server sent us
|
||||||
for(const store of data.changes) {
|
for (const store of data.changes) {
|
||||||
if(store.data === null) {
|
if (store.data === null) {
|
||||||
await tx.objectStore("stores").delete(store.id);
|
await tx.objectStore("stores").delete(store.id);
|
||||||
} else {
|
} else {
|
||||||
await tx.objectStore("stores").put(store);
|
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
|
// Delete all changes
|
||||||
await tx.objectStore("changes").clear();
|
await tx.objectStore("changes").clear();
|
||||||
@ -118,21 +140,26 @@ async function createStore(info: StoreInfo, data: object) {
|
|||||||
type: info.type,
|
type: info.type,
|
||||||
owner_id: "", // TODO
|
owner_id: "", // TODO
|
||||||
data: JSON.stringify(data),
|
data: JSON.stringify(data),
|
||||||
modified_at: new Date().toISOString()
|
modified_at: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
const tx = db.transaction(["stores", "changes"], "readwrite");
|
const tx = db.transaction(["stores", "changes"], "readwrite");
|
||||||
await tx.objectStore("stores").add(store);
|
await tx.objectStore("stores").add(store);
|
||||||
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();
|
await trySync();
|
||||||
return store;
|
return store;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateStore(info: StoreInfo, data: object | null) {
|
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 (!store) {
|
||||||
if(data === null) return;
|
if (data === null) return;
|
||||||
return await createStore(info, data);
|
return await createStore(info, data);
|
||||||
}
|
}
|
||||||
// Update the store data
|
// Update the store data
|
||||||
@ -142,7 +169,9 @@ export async function updateStore(info: StoreInfo, data: object | null) {
|
|||||||
await tx.objectStore("stores").put(store);
|
await tx.objectStore("stores").put(store);
|
||||||
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();
|
await trySync();
|
||||||
return store;
|
return store;
|
||||||
}
|
}
|
||||||
@ -166,19 +195,33 @@ export async function updateStore(info: StoreInfo, data: object | null) {
|
|||||||
// return state;
|
// 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>[]>([]);
|
const state = $state<StoreValue<T>[]>([]);
|
||||||
eventTarget.addEventListener("store-updated", async (event) => {
|
eventTarget.addEventListener("store-updated", async (event) => {
|
||||||
const customEvent = event as CustomEvent;
|
const customEvent = event as CustomEvent;
|
||||||
const updatedStore = customEvent.detail as Store;
|
const updatedStore = customEvent.detail as Store;
|
||||||
if(updatedStore.type === type) {
|
if (updatedStore.type === type) {
|
||||||
const stores = await db.getAllFromIndex("stores", "by-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 () => {
|
(async () => {
|
||||||
const stores = await db.getAllFromIndex("stores", "by-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),
|
||||||
|
);
|
||||||
})();
|
})();
|
||||||
return {
|
return {
|
||||||
get current() {
|
get current() {
|
||||||
@ -186,6 +229,6 @@ export function stores<T extends object>(type: StoreType): WrappedValue<StoreVal
|
|||||||
},
|
},
|
||||||
set current(newValue: StoreValue<T>[]) {
|
set current(newValue: StoreValue<T>[]) {
|
||||||
state.splice(0, state.length, ...newValue);
|
state.splice(0, state.length, ...newValue);
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,10 @@
|
|||||||
import { m } from "$lang/messages";
|
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:
|
Valhalla costing:
|
||||||
@ -80,7 +85,8 @@ export const selectedVehicleIdx: StateValue<number | null> = $state({
|
|||||||
});
|
});
|
||||||
export const selectedVehicle: () => Vehicle | null = () => {
|
export const selectedVehicle: () => Vehicle | null = () => {
|
||||||
return (
|
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) {
|
if (vehicle == null) {
|
||||||
selectedVehicleIdx.v = null;
|
selectedVehicleIdx.v = null;
|
||||||
} else {
|
} 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) {
|
if (selectedVehicleIdx.v === -1) {
|
||||||
selectedVehicleIdx.v = null;
|
selectedVehicleIdx.v = null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user