2022-04-07 13:21:30 +00:00
|
|
|
import { SpacePrimitives } from "./space_primitives";
|
2022-04-08 15:46:09 +00:00
|
|
|
import { PageMeta } from "../types";
|
2022-04-05 15:02:17 +00:00
|
|
|
import Dexie, { Table } from "dexie";
|
2022-04-25 08:33:38 +00:00
|
|
|
import { Plug } from "@plugos/plugos/plug";
|
2022-04-05 15:02:17 +00:00
|
|
|
|
|
|
|
type Page = {
|
|
|
|
name: string;
|
|
|
|
text: string;
|
|
|
|
meta: PageMeta;
|
|
|
|
};
|
|
|
|
|
2022-04-07 13:21:30 +00:00
|
|
|
export class IndexedDBSpacePrimitives implements SpacePrimitives {
|
2022-04-05 15:02:17 +00:00
|
|
|
private pageTable: Table<Page, string>;
|
|
|
|
|
2022-04-07 12:04:50 +00:00
|
|
|
constructor(dbName: string, readonly timeSkew: number = 0) {
|
2022-04-05 15:02:17 +00:00
|
|
|
const db = new Dexie(dbName);
|
|
|
|
db.version(1).stores({
|
|
|
|
page: "name",
|
|
|
|
});
|
|
|
|
this.pageTable = db.table("page");
|
|
|
|
}
|
|
|
|
|
|
|
|
async deletePage(name: string): Promise<void> {
|
|
|
|
return this.pageTable.delete(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPageMeta(name: string): Promise<PageMeta> {
|
|
|
|
let entry = await this.pageTable.get(name);
|
|
|
|
if (entry) {
|
|
|
|
return entry.meta;
|
|
|
|
} else {
|
2022-04-06 13:39:20 +00:00
|
|
|
throw Error(`Page not found`);
|
2022-04-05 15:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
invokeFunction(
|
|
|
|
plug: Plug<any>,
|
|
|
|
env: string,
|
|
|
|
name: string,
|
|
|
|
args: any[]
|
|
|
|
): Promise<any> {
|
|
|
|
return plug.invoke(name, args);
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:04:50 +00:00
|
|
|
async fetchPageList(): Promise<{
|
|
|
|
pages: Set<PageMeta>;
|
|
|
|
nowTimestamp: number;
|
|
|
|
}> {
|
2022-04-05 15:02:17 +00:00
|
|
|
let allPages = await this.pageTable.toArray();
|
2022-04-07 12:04:50 +00:00
|
|
|
return {
|
|
|
|
pages: new Set(allPages.map((p) => p.meta)),
|
|
|
|
nowTimestamp: Date.now() + this.timeSkew,
|
|
|
|
};
|
2022-04-05 15:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proxySyscall(plug: Plug<any>, name: string, args: any[]): Promise<any> {
|
|
|
|
return plug.syscall(name, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
async readPage(name: string): Promise<{ text: string; meta: PageMeta }> {
|
|
|
|
let page = await this.pageTable.get(name);
|
|
|
|
if (page) {
|
2022-04-06 13:39:20 +00:00
|
|
|
return page;
|
2022-04-05 15:02:17 +00:00
|
|
|
} else {
|
2022-04-06 13:39:20 +00:00
|
|
|
throw new Error("Page not found");
|
2022-04-05 15:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async writePage(
|
|
|
|
name: string,
|
|
|
|
text: string,
|
|
|
|
selfUpdate?: boolean,
|
2022-04-06 13:39:20 +00:00
|
|
|
lastModified?: number
|
2022-04-05 15:02:17 +00:00
|
|
|
): Promise<PageMeta> {
|
2022-04-06 13:39:20 +00:00
|
|
|
let meta = {
|
|
|
|
name,
|
2022-04-07 12:04:50 +00:00
|
|
|
lastModified: lastModified ? lastModified : Date.now() + this.timeSkew,
|
2022-04-06 13:39:20 +00:00
|
|
|
};
|
2022-04-05 15:02:17 +00:00
|
|
|
await this.pageTable.put({
|
|
|
|
name,
|
|
|
|
text,
|
|
|
|
meta,
|
|
|
|
});
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
}
|