1
0
silverbullet/common/spaces/evented_space_primitives.ts
Zef Hemel 6ebf8e7f15 * Refactored server to use spaces
* Other cleanup
2022-04-08 17:46:09 +02:00

66 lines
1.8 KiB
TypeScript

import { SpacePrimitives } from "./space_primitives";
import { EventHook } from "../../plugos/hooks/event";
import { PageMeta } from "../types";
import { Plug } from "../../plugos/plug";
export class EventedSpacePrimitives implements SpacePrimitives {
constructor(private wrapped: SpacePrimitives, private eventHook: EventHook) {}
fetchPageList(): Promise<{ pages: Set<PageMeta>; nowTimestamp: number }> {
return this.wrapped.fetchPageList();
}
proxySyscall(plug: Plug<any>, name: string, args: any[]): Promise<any> {
return this.wrapped.proxySyscall(plug, name, args);
}
invokeFunction(
plug: Plug<any>,
env: string,
name: string,
args: any[]
): Promise<any> {
return this.wrapped.invokeFunction(plug, env, name, args);
}
readPage(pageName: string): Promise<{ text: string; meta: PageMeta }> {
return this.wrapped.readPage(pageName);
}
async writePage(
pageName: string,
text: string,
selfUpdate: boolean,
lastModified?: number
): Promise<PageMeta> {
const newPageMeta = await this.wrapped.writePage(
pageName,
text,
selfUpdate,
lastModified
);
// This can happen async
this.eventHook
.dispatchEvent("page:saved", pageName)
.then(() => {
return this.eventHook.dispatchEvent("page:index", {
name: pageName,
text: text,
});
})
.catch((e) => {
console.error("Error dispatching page:saved event", e);
});
return newPageMeta;
}
getPageMeta(pageName: string): Promise<PageMeta> {
return this.wrapped.getPageMeta(pageName);
}
async deletePage(pageName: string): Promise<void> {
await this.eventHook.dispatchEvent("page:deleted", pageName);
return this.wrapped.deletePage(pageName);
}
}