2022-04-06 13:39:20 +00:00
|
|
|
import { Space } from "./space";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { PageMeta } from "../../common/types";
|
|
|
|
import Dexie, { Table } from "dexie";
|
|
|
|
import { Plug } from "../../plugos/plug";
|
|
|
|
|
|
|
|
type Page = {
|
|
|
|
name: string;
|
|
|
|
text: string;
|
|
|
|
meta: PageMeta;
|
|
|
|
};
|
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
export class IndexedDBSpace implements Space {
|
2022-04-05 15:02:17 +00:00
|
|
|
private pageTable: Table<Page, string>;
|
|
|
|
|
|
|
|
constructor(dbName: string) {
|
|
|
|
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-06 13:39:20 +00:00
|
|
|
async fetchPageList(): Promise<Set<PageMeta>> {
|
2022-04-05 15:02:17 +00:00
|
|
|
let allPages = await this.pageTable.toArray();
|
|
|
|
let set = new Set(allPages.map((p) => p.meta));
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
lastModified: lastModified ? lastModified : new Date().getTime(),
|
|
|
|
};
|
2022-04-05 15:02:17 +00:00
|
|
|
await this.pageTable.put({
|
|
|
|
name,
|
|
|
|
text,
|
|
|
|
meta,
|
|
|
|
});
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
}
|