57 lines
3.7 KiB
JavaScript
57 lines
3.7 KiB
JavaScript
// Binary drafts and save journals never contain tokens or signed URLs.
|
|
function coveredRevision(revision, saved) {
|
|
if (typeof revision !== 'string' || typeof saved !== 'string' || !revision || !saved) return false;
|
|
if (revision === saved) return true;
|
|
const before = typeof revision === 'string' && /^(\d{1,20}):/.exec(revision);
|
|
const after = typeof saved === 'string' && /^(\d{1,20}):/.exec(saved);
|
|
// Revision counters only order changes within one binding. Equal counters with
|
|
// different visibility state may represent a later change and must survive.
|
|
return !!before && !!after && BigInt(before[1]) < BigInt(after[1]);
|
|
}
|
|
export class DraftStore {
|
|
constructor(indexed = globalThis.indexedDB) { this.indexed = indexed; this.acknowledged = new Map(); }
|
|
async database() {
|
|
if (!this.indexed) throw new Error('IndexedDB unavailable');
|
|
this.db ??= new Promise((resolve, reject) => {
|
|
const request = this.indexed.open('vm.cad', 1);
|
|
request.onupgradeneeded = () => request.result.createObjectStore('records', { keyPath: 'key' });
|
|
request.onsuccess = () => resolve(request.result); request.onerror = () => reject(request.error);
|
|
request.onblocked = () => reject(new Error('IndexedDB blocked'));
|
|
}); return this.db;
|
|
}
|
|
async transaction(mode, run) {
|
|
const db = await this.database();
|
|
return new Promise((resolve, reject) => {
|
|
const tx = db.transaction('records', mode); let result;
|
|
run(tx.objectStore('records'), (value) => { result = value; });
|
|
tx.oncomplete = () => resolve(result); tx.onerror = () => reject(tx.error); tx.onabort = () => reject(tx.error || new Error('Draft transaction aborted'));
|
|
});
|
|
}
|
|
key(user, project, session, kind) { return JSON.stringify([user, project, session, kind]); }
|
|
put(user, project, session, kind, value) {
|
|
return this.transaction('readwrite', (store) => {
|
|
const saved = value.binding && this.acknowledged.get(JSON.stringify([user, project, value.binding]));
|
|
if (kind === 'draft' && saved && coveredRevision(value.revision, saved)) return;
|
|
store.put({ key: this.key(user, project, session, kind), user, project, session, kind, updatedAt: Date.now(), ...value });
|
|
});
|
|
}
|
|
async list(user, project, kind) {
|
|
return this.transaction('readonly', (store, done) => { const req = store.getAll(); req.onsuccess = () => done(req.result.filter((r) => r.user === user && r.project === project && (!kind || r.kind === kind)).sort((a, b) => b.updatedAt - a.updatedAt)); });
|
|
}
|
|
remove(key, snapshotId) {
|
|
return this.transaction('readwrite', (store) => { const request = store.get(key); request.onsuccess = () => { if (request.result && (!snapshotId || request.result.snapshotId === snapshotId)) store.delete(key); }; });
|
|
}
|
|
rebindDraft(key, snapshotId, baseline) {
|
|
return this.transaction('readwrite', (store) => { const request = store.get(key); request.onsuccess = () => {
|
|
const record = request.result;
|
|
if (record?.kind === 'draft' && record.snapshotId === snapshotId) store.put({ ...record, ...baseline });
|
|
}; });
|
|
}
|
|
clearAcknowledged(user, project, snapshotId, baseline) {
|
|
if (baseline?.binding && baseline.revision) this.acknowledged.set(JSON.stringify([user, project, baseline.binding]), baseline.revision);
|
|
return this.transaction('readwrite', (store) => { const request = store.openCursor(); request.onsuccess = () => { const cursor = request.result; if (!cursor) return;
|
|
const r = cursor.value;
|
|
if (r.user === user && r.project === project && (r.snapshotId === snapshotId || (r.kind === 'draft' && baseline?.binding && r.binding === baseline.binding && coveredRevision(r.revision, baseline.revision)))) cursor.delete(); cursor.continue(); }; });
|
|
}
|
|
}
|