Publish Forma Engine 0.3.0 source with documentation and CI
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { type Project, validateProject } from "../engine/schema.ts";
|
||||
function db(): Promise<IDBDatabase> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = indexedDB.open("forma-projects", 1);
|
||||
req.onupgradeneeded = () => req.result.createObjectStore("projects");
|
||||
req.onsuccess = () => resolve(req.result);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
}
|
||||
export async function loadDraft() {
|
||||
const d = await db();
|
||||
return new Promise<Project | null>((resolve, reject) => {
|
||||
const r = d.transaction("projects").objectStore("projects").get("active");
|
||||
r.onsuccess = () => {
|
||||
d.close();
|
||||
if (r.result) {
|
||||
try {
|
||||
validateProject(r.result);
|
||||
resolve(r.result);
|
||||
} catch {
|
||||
resolve(null);
|
||||
}
|
||||
} else resolve(null);
|
||||
};
|
||||
r.onerror = () => reject(r.error);
|
||||
});
|
||||
}
|
||||
export async function saveDraft(p: Project) {
|
||||
const d = await db();
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const tx = d.transaction("projects", "readwrite");
|
||||
tx.objectStore("projects").put(p, "active");
|
||||
tx.oncomplete = () => {
|
||||
d.close();
|
||||
resolve();
|
||||
};
|
||||
tx.onerror = () => reject(tx.error);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user