core&ui: cache uicontext.domain

This commit is contained in:
undefined committed 2026-06-18 12:59:55 +00:00
1 parent 43fb00e3c7
commit e39a5fa87f
8 files changed
+71 -10

No files matched your search

+2 -1
View File
@@ -1,7 +1,7 @@
import { openDB as _open } from 'idb';
import { alert } from 'vj/components/dialog';
export const openDB = _open('hydro', 1, {
export const openDB = _open('hydro', 2, {
upgrade(db, oldVersion) {
if (oldVersion < 1) {
const solutionStore = db.createObjectStore('solutions', { keyPath: 'id' });
@@ -17,6 +17,7 @@ export const openDB = _open('hydro', 1, {
if (/^0\.\d+$/.test(key)) localStorage.removeItem(key);
}
}
if (oldVersion < 2) db.createObjectStore('domain-info', { keyPath: 'id' });
},
blocked(currentVersion, blockedVersion) {
console.error('IDB Blocked by version', blockedVersion, 'want', currentVersion);
+40 -1
View File
@@ -1,13 +1,52 @@
import 'streamsaver/examples/zip-stream';
import { request } from './base';
import { openDB } from './db';
export async function api(method: string, args: Record<string, any>, projection: any) {
export async function api(method: string, args: Record<string, any>, projection?: any) {
const res = await request.post(`/d/${UiContext.domainId}/api/${encodeURIComponent(method)}`, { args, projection });
if (res.error) throw new Error(res.error);
return res;
}
interface DomainInfoCache {
id: string;
version: number;
domain: Record<string, any>;
}
const domainInfoCacheStore = 'domain-info';
let domainInfoPromise: Promise<Record<string, any>> | null = null;
async function readDomainInfoCache(domainId: string) {
return await (await openDB).get(domainInfoCacheStore, domainId) as DomainInfoCache | null;
}
async function writeDomainInfoCache(cache: DomainInfoCache) {
await (await openDB).put(domainInfoCacheStore, cache);
}
async function loadDomainInfo() {
let cached: DomainInfoCache | null = null;
try {
cached = await readDomainInfoCache(UiContext.domainId);
} catch (e) { }
if (cached && cached.version >= UiContext.domainVersion) return cached.domain;
const res = await api('domain.current', {});
const { domain } = res;
if (!domain) throw new Error('Failed to load domain info');
writeDomainInfoCache({ id: UiContext.domainId, version: UiContext.domainVersion, domain }).catch(() => { });
return domain;
}
export function getDomainInfo() {
domainInfoPromise ||= loadDomainInfo().catch((e) => {
domainInfoPromise = null;
throw e;
});
return domainInfoPromise;
}
export function getAvailableLangs(langsList?: string[]) {
const prefixes = new Set(Object.keys(window.LANGS).filter((i) => i.includes('.')).map((i) => i.split('.')[0]));
const Langs = {};