2022-04-25 08:33:38 +00:00
|
|
|
import { EventHook } from "@plugos/plugos/hooks/event";
|
|
|
|
import { Plug } from "@plugos/plugos/plug";
|
2022-04-26 18:31:31 +00:00
|
|
|
|
2022-09-05 09:47:30 +00:00
|
|
|
import { AttachmentMeta, PageMeta } from "../types";
|
2022-04-26 17:04:36 +00:00
|
|
|
import { plugPrefix, trashPrefix } from "./constants";
|
2022-04-26 18:31:31 +00:00
|
|
|
import { SpacePrimitives } from "./space_primitives";
|
2022-04-08 15:46:09 +00:00
|
|
|
|
|
|
|
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
|
2022-04-26 17:04:36 +00:00
|
|
|
if (!pageName.startsWith(trashPrefix) && !pageName.startsWith(plugPrefix)) {
|
2022-04-13 12:46:52 +00:00
|
|
|
this.eventHook
|
|
|
|
.dispatchEvent("page:saved", pageName)
|
|
|
|
.then(() => {
|
2022-04-20 08:56:43 +00:00
|
|
|
return this.eventHook.dispatchEvent("page:index_text", {
|
2022-04-13 12:46:52 +00:00
|
|
|
name: pageName,
|
2022-04-20 08:56:43 +00:00
|
|
|
text,
|
2022-04-13 12:46:52 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error("Error dispatching page:saved event", e);
|
2022-04-08 15:46:09 +00:00
|
|
|
});
|
2022-04-13 12:46:52 +00:00
|
|
|
}
|
2022-04-08 15:46:09 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-09-05 09:47:30 +00:00
|
|
|
|
|
|
|
fetchAttachmentList(): Promise<{
|
|
|
|
attachments: Set<AttachmentMeta>;
|
|
|
|
nowTimestamp: number;
|
|
|
|
}> {
|
|
|
|
return this.wrapped.fetchAttachmentList();
|
|
|
|
}
|
|
|
|
|
|
|
|
readAttachment(
|
|
|
|
name: string
|
|
|
|
): Promise<{ buffer: ArrayBuffer; meta: AttachmentMeta }> {
|
|
|
|
return this.wrapped.readAttachment(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAttachmentMeta(name: string): Promise<AttachmentMeta> {
|
|
|
|
return this.wrapped.getAttachmentMeta(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async writeAttachment(
|
|
|
|
name: string,
|
|
|
|
blob: ArrayBuffer,
|
|
|
|
selfUpdate?: boolean | undefined,
|
|
|
|
lastModified?: number | undefined
|
|
|
|
): Promise<AttachmentMeta> {
|
|
|
|
let meta = await this.wrapped.writeAttachment(
|
|
|
|
name,
|
|
|
|
blob,
|
|
|
|
selfUpdate,
|
|
|
|
lastModified
|
|
|
|
);
|
|
|
|
await this.eventHook.dispatchEvent("attachment:saved", name);
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAttachment(name: string): Promise<void> {
|
|
|
|
await this.eventHook.dispatchEvent("attachment:deleted", name);
|
|
|
|
return this.wrapped.deleteAttachment(name);
|
|
|
|
}
|
2022-04-08 15:46:09 +00:00
|
|
|
}
|