112 lines
3.2 KiB
JavaScript
112 lines
3.2 KiB
JavaScript
const SATELLITE_CACHE_DB_NAME = "planet-earth-catalog-cache";
|
|
const SATELLITE_CACHE_DB_VERSION = 1;
|
|
const SATELLITE_CACHE_STORE = "snapshots";
|
|
|
|
let dbPromise = null;
|
|
|
|
function openSatelliteCacheDb() {
|
|
if (typeof indexedDB === "undefined") {
|
|
return Promise.resolve(null);
|
|
}
|
|
if (dbPromise) {
|
|
return dbPromise;
|
|
}
|
|
dbPromise = new Promise((resolve) => {
|
|
try {
|
|
const request = indexedDB.open(SATELLITE_CACHE_DB_NAME, SATELLITE_CACHE_DB_VERSION);
|
|
request.onupgradeneeded = () => {
|
|
const db = request.result;
|
|
if (!db.objectStoreNames.contains(SATELLITE_CACHE_STORE)) {
|
|
db.createObjectStore(SATELLITE_CACHE_STORE);
|
|
}
|
|
};
|
|
request.onsuccess = () => resolve(request.result);
|
|
request.onerror = () => {
|
|
dbPromise = null;
|
|
resolve(null);
|
|
};
|
|
} catch {
|
|
dbPromise = null;
|
|
resolve(null);
|
|
}
|
|
});
|
|
return dbPromise;
|
|
}
|
|
|
|
export async function resolveSatelliteCacheOrigin() {
|
|
if (typeof window === "undefined") return "local";
|
|
try {
|
|
const getConfig = window.planetDesktop?.getConfig;
|
|
if (typeof getConfig === "function") {
|
|
const config = await getConfig();
|
|
if (config?.backendUrl) {
|
|
return String(config.backendUrl).replace(/\/+$/, "");
|
|
}
|
|
}
|
|
} catch {
|
|
// Fall back to window.location.origin when desktop config is unavailable.
|
|
}
|
|
return window.location.origin || "local";
|
|
}
|
|
|
|
export function buildSatelliteCacheKey({ origin = "local", apiPath = "", limit = null } = {}) {
|
|
const normalizedOrigin = String(origin || "local").replace(/\/+$/, "");
|
|
const normalizedLimit = limit === null || limit === undefined ? "all" : String(limit);
|
|
return `satellites:v1:${normalizedOrigin}:${apiPath}:limit=${normalizedLimit}`;
|
|
}
|
|
|
|
export async function readSatelliteSnapshot(cacheKey) {
|
|
if (!cacheKey) return null;
|
|
const db = await openSatelliteCacheDb();
|
|
if (!db) return null;
|
|
|
|
return new Promise((resolve) => {
|
|
try {
|
|
const tx = db.transaction(SATELLITE_CACHE_STORE, "readonly");
|
|
const store = tx.objectStore(SATELLITE_CACHE_STORE);
|
|
const req = store.get(cacheKey);
|
|
req.onsuccess = () => {
|
|
const value = req.result;
|
|
if (value && typeof value === "object" && value.payload) {
|
|
resolve({
|
|
etag: value.etag ?? null,
|
|
payload: value.payload,
|
|
savedAt: Number(value.savedAt) || 0,
|
|
});
|
|
return;
|
|
}
|
|
resolve(null);
|
|
};
|
|
req.onerror = () => resolve(null);
|
|
} catch {
|
|
resolve(null);
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function writeSatelliteSnapshot(cacheKey, { etag = null, payload } = {}) {
|
|
if (!cacheKey || !payload) return;
|
|
const db = await openSatelliteCacheDb();
|
|
if (!db) return;
|
|
|
|
await new Promise((resolve) => {
|
|
try {
|
|
const tx = db.transaction(SATELLITE_CACHE_STORE, "readwrite");
|
|
const store = tx.objectStore(SATELLITE_CACHE_STORE);
|
|
store.put(
|
|
{
|
|
etag: etag ?? null,
|
|
payload,
|
|
savedAt: Date.now(),
|
|
},
|
|
cacheKey,
|
|
);
|
|
tx.oncomplete = () => resolve();
|
|
tx.onerror = () => resolve();
|
|
tx.onabort = () => resolve();
|
|
} catch {
|
|
resolve();
|
|
}
|
|
});
|
|
}
|