2022-10-10 12:50:21 +00:00
|
|
|
import { EventHook } from "../../plugos/hooks/event.ts";
|
2022-04-26 18:31:31 +00:00
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
import { FileMeta } from "../types.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import type { SpacePrimitives } from "./space_primitives.ts";
|
2022-04-08 15:46:09 +00:00
|
|
|
|
|
|
|
export class EventedSpacePrimitives implements SpacePrimitives {
|
|
|
|
constructor(private wrapped: SpacePrimitives, private eventHook: EventHook) {}
|
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
fetchFileList(): Promise<FileMeta[]> {
|
|
|
|
return this.wrapped.fetchFileList();
|
2022-04-08 15:46:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
readFile(
|
|
|
|
name: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
|
|
|
return this.wrapped.readFile(name);
|
2022-04-08 15:46:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
async writeFile(
|
|
|
|
name: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
data: Uint8Array,
|
2023-01-13 14:41:29 +00:00
|
|
|
selfUpdate?: boolean,
|
2023-07-02 09:25:32 +00:00
|
|
|
meta?: FileMeta,
|
2022-09-12 12:50:37 +00:00
|
|
|
): Promise<FileMeta> {
|
|
|
|
const newMeta = await this.wrapped.writeFile(
|
|
|
|
name,
|
|
|
|
data,
|
2022-10-12 09:47:13 +00:00
|
|
|
selfUpdate,
|
2023-07-02 09:25:32 +00:00
|
|
|
meta,
|
2022-04-08 15:46:09 +00:00
|
|
|
);
|
|
|
|
// This can happen async
|
2022-09-12 12:50:37 +00:00
|
|
|
if (name.endsWith(".md")) {
|
|
|
|
const pageName = name.substring(0, name.length - 3);
|
|
|
|
let text = "";
|
2023-05-23 18:53:53 +00:00
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
text = decoder.decode(data);
|
2022-09-12 12:50:37 +00:00
|
|
|
|
2022-04-13 12:46:52 +00:00
|
|
|
this.eventHook
|
2023-06-13 18:47:05 +00:00
|
|
|
.dispatchEvent("page:saved", pageName, newMeta)
|
2022-04-13 12:46:52 +00:00
|
|
|
.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
|
|
|
}
|
2023-07-02 09:25:32 +00:00
|
|
|
if (name.startsWith("_plug/") && name.endsWith(".plug.js")) {
|
2023-05-23 18:53:53 +00:00
|
|
|
await this.eventHook.dispatchEvent("plug:changed", name);
|
|
|
|
}
|
2022-09-12 12:50:37 +00:00
|
|
|
return newMeta;
|
2022-04-08 15:46:09 +00:00
|
|
|
}
|
2022-09-05 09:47:30 +00:00
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
getFileMeta(name: string): Promise<FileMeta> {
|
|
|
|
return this.wrapped.getFileMeta(name);
|
2022-09-05 09:47:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
async deleteFile(name: string): Promise<void> {
|
|
|
|
if (name.endsWith(".md")) {
|
|
|
|
const pageName = name.substring(0, name.length - 3);
|
|
|
|
await this.eventHook.dispatchEvent("page:deleted", pageName);
|
|
|
|
}
|
|
|
|
return this.wrapped.deleteFile(name);
|
2022-09-05 09:47:30 +00:00
|
|
|
}
|
2022-04-08 15:46:09 +00:00
|
|
|
}
|