2023-08-20 15:51:00 +00:00
|
|
|
import { FileMeta } from "$sb/types.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { EventHook } from "../../plugos/hooks/event.ts";
|
2022-04-26 18:31:31 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
import type { SpacePrimitives } from "./space_primitives.ts";
|
2022-04-08 15:46:09 +00:00
|
|
|
|
2023-08-27 09:02:24 +00:00
|
|
|
/**
|
|
|
|
* Events exposed:
|
2023-08-27 12:13:18 +00:00
|
|
|
* - file:changed (string, localUpdate: boolean)
|
2023-08-27 09:02:24 +00:00
|
|
|
* - file:deleted (string)
|
|
|
|
* - file:listed (FileMeta[])
|
|
|
|
* - page:saved (string, FileMeta)
|
|
|
|
* - page:deleted (string)
|
|
|
|
*/
|
2022-04-08 15:46:09 +00:00
|
|
|
export class EventedSpacePrimitives implements SpacePrimitives {
|
2023-08-27 12:13:18 +00:00
|
|
|
alreadyFetching = false;
|
2023-08-27 09:02:24 +00:00
|
|
|
initialFileListLoad = true;
|
|
|
|
|
2023-08-27 12:13:18 +00:00
|
|
|
spaceSnapshot: Record<string, number> = {};
|
2023-08-27 09:02:24 +00:00
|
|
|
constructor(
|
|
|
|
private wrapped: SpacePrimitives,
|
|
|
|
private eventHook: EventHook,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
dispatchEvent(name: string, ...args: any[]): Promise<any[]> {
|
2023-08-27 12:13:18 +00:00
|
|
|
return this.eventHook.dispatchEvent(name, ...args);
|
2023-08-27 09:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fetchFileList(): Promise<FileMeta[]> {
|
|
|
|
const newFileList = await this.wrapped.fetchFileList();
|
2023-08-27 12:13:18 +00:00
|
|
|
if (this.alreadyFetching) {
|
|
|
|
// Avoid race conditions
|
|
|
|
return newFileList;
|
|
|
|
}
|
|
|
|
// console.log("HEREEREEEREEREE");
|
|
|
|
this.alreadyFetching = true;
|
|
|
|
const deletedFiles = new Set<string>(Object.keys(this.spaceSnapshot));
|
2023-08-27 09:02:24 +00:00
|
|
|
for (const meta of newFileList) {
|
2023-08-27 12:13:18 +00:00
|
|
|
const oldHash = this.spaceSnapshot[meta.name];
|
|
|
|
const newHash = meta.lastModified;
|
2023-08-27 09:02:24 +00:00
|
|
|
if (
|
|
|
|
(
|
|
|
|
// New file scenario
|
2023-08-27 12:13:18 +00:00
|
|
|
!oldHash && !this.initialFileListLoad
|
2023-08-27 09:02:24 +00:00
|
|
|
) || (
|
|
|
|
// Changed file scenario
|
2023-08-27 12:13:18 +00:00
|
|
|
oldHash &&
|
|
|
|
oldHash !== newHash
|
2023-08-27 09:02:24 +00:00
|
|
|
)
|
|
|
|
) {
|
2023-08-27 12:13:18 +00:00
|
|
|
this.dispatchEvent("file:changed", meta.name);
|
2023-08-27 09:02:24 +00:00
|
|
|
}
|
|
|
|
// Page found, not deleted
|
|
|
|
deletedFiles.delete(meta.name);
|
|
|
|
|
2023-08-27 12:13:18 +00:00
|
|
|
// Update in snapshot
|
|
|
|
this.spaceSnapshot[meta.name] = newHash;
|
2023-08-27 09:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const deletedFile of deletedFiles) {
|
2023-08-27 12:13:18 +00:00
|
|
|
delete this.spaceSnapshot[deletedFile];
|
2023-08-27 09:02:24 +00:00
|
|
|
this.dispatchEvent("file:deleted", deletedFile);
|
2022-04-08 15:46:09 +00:00
|
|
|
|
2023-08-27 09:02:24 +00:00
|
|
|
if (deletedFile.endsWith(".md")) {
|
|
|
|
const pageName = deletedFile.substring(0, deletedFile.length - 3);
|
|
|
|
await this.dispatchEvent("page:deleted", pageName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 12:13:18 +00:00
|
|
|
this.dispatchEvent("file:listed", newFileList);
|
|
|
|
this.alreadyFetching = false;
|
2023-08-27 09:02:24 +00:00
|
|
|
this.initialFileListLoad = false;
|
2023-08-27 12:13:18 +00:00
|
|
|
return newFileList;
|
2022-04-08 15:46:09 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 09:02:24 +00:00
|
|
|
async readFile(
|
2022-09-12 12:50:37 +00:00
|
|
|
name: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
2023-08-27 09:02:24 +00:00
|
|
|
const data = await this.wrapped.readFile(name);
|
2023-08-27 12:13:18 +00:00
|
|
|
this.triggerEventsAndCache(name, data.meta.lastModified);
|
|
|
|
return data;
|
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
|
|
|
);
|
2023-08-27 09:02:24 +00:00
|
|
|
if (!selfUpdate) {
|
2023-08-27 12:13:18 +00:00
|
|
|
this.dispatchEvent("file:changed", name, true);
|
2023-08-27 09:02:24 +00:00
|
|
|
}
|
2023-08-27 12:13:18 +00:00
|
|
|
this.spaceSnapshot[name] = newMeta.lastModified;
|
2023-08-27 09:02:24 +00:00
|
|
|
|
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
|
|
|
|
2023-08-27 09:02:24 +00:00
|
|
|
this.dispatchEvent("page:saved", pageName, newMeta)
|
2022-04-13 12:46:52 +00:00
|
|
|
.then(() => {
|
2023-08-27 09:02:24 +00:00
|
|
|
return this.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-08-29 19:17:29 +00:00
|
|
|
// if (name.startsWith("_plug/") && name.endsWith(".plug.js")) {
|
|
|
|
// await this.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
|
|
|
|
2023-08-27 12:13:18 +00:00
|
|
|
triggerEventsAndCache(name: string, newHash: number) {
|
|
|
|
const oldHash = this.spaceSnapshot[name];
|
|
|
|
if (oldHash && oldHash !== newHash) {
|
|
|
|
// Page changed since last cached metadata, trigger event
|
|
|
|
this.dispatchEvent("file:changed", name);
|
|
|
|
}
|
|
|
|
this.spaceSnapshot[name] = newHash;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-27 09:02:24 +00:00
|
|
|
async getFileMeta(name: string): Promise<FileMeta> {
|
|
|
|
try {
|
|
|
|
const newMeta = await this.wrapped.getFileMeta(name);
|
2023-08-27 12:13:18 +00:00
|
|
|
this.triggerEventsAndCache(name, newMeta.lastModified);
|
|
|
|
return newMeta;
|
2023-08-27 09:02:24 +00:00
|
|
|
} catch (e: any) {
|
|
|
|
console.log("Checking error", e, name);
|
|
|
|
if (e.message === "Not found") {
|
|
|
|
this.dispatchEvent("file:deleted", name);
|
|
|
|
if (name.endsWith(".md")) {
|
|
|
|
const pageName = name.substring(0, name.length - 3);
|
|
|
|
await this.dispatchEvent("page:deleted", pageName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
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);
|
2023-08-27 09:02:24 +00:00
|
|
|
await this.dispatchEvent("page:deleted", pageName);
|
|
|
|
}
|
|
|
|
// await this.getPageMeta(name); // Check if page exists, if not throws Error
|
|
|
|
await this.wrapped.deleteFile(name);
|
2023-08-27 12:13:18 +00:00
|
|
|
delete this.spaceSnapshot[name];
|
2023-08-27 09:02:24 +00:00
|
|
|
this.dispatchEvent("file:deleted", name);
|
|
|
|
}
|
2022-04-08 15:46:09 +00:00
|
|
|
}
|