1
0
silverbullet/common/spaces/evented_space_primitives.ts

194 lines
5.4 KiB
TypeScript
Raw Normal View History

2023-08-20 15:51:00 +00:00
import { FileMeta } from "$sb/types.ts";
import { EventHook } from "../../plugos/hooks/event.ts";
2022-04-26 18:31:31 +00:00
import type { SpacePrimitives } from "./space_primitives.ts";
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)
*/
export class EventedSpacePrimitives implements SpacePrimitives {
// Avoid file listing while performing another fetch (read, write, meta) operation
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,
) {
}
2023-08-27 09:02:24 +00:00
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, let's just skip this in terms of event triggering: that's ok
2023-08-27 12:13:18 +00:00
return newFileList;
}
try {
this.alreadyFetching = true;
const deletedFiles = new Set<string>(Object.keys(this.spaceSnapshot));
for (const meta of newFileList) {
const oldHash = this.spaceSnapshot[meta.name];
const newHash = meta.lastModified;
// Update in snapshot
this.spaceSnapshot[meta.name] = newHash;
// Check what happened to the file
if (
(
// New file scenario
!oldHash && !this.initialFileListLoad
) || (
// Changed file scenario
oldHash &&
oldHash !== newHash
)
) {
await this.dispatchEvent(
"file:changed",
meta.name,
false,
oldHash,
newHash,
);
}
// Page found, not deleted
deletedFiles.delete(meta.name);
2023-08-27 09:02:24 +00:00
}
for (const deletedFile of deletedFiles) {
delete this.spaceSnapshot[deletedFile];
await this.dispatchEvent("file:deleted", deletedFile);
if (deletedFile.endsWith(".md")) {
const pageName = deletedFile.substring(0, deletedFile.length - 3);
await this.dispatchEvent("page:deleted", pageName);
}
2023-08-27 09:02:24 +00:00
}
await this.dispatchEvent("file:listed", newFileList);
this.initialFileListLoad = false;
return newFileList;
} finally {
this.alreadyFetching = false;
}
}
2023-08-27 09:02:24 +00:00
async readFile(
2022-09-12 12:50:37 +00:00
name: string,
): Promise<{ data: Uint8Array; meta: FileMeta }> {
try {
// Fetching mutex
this.alreadyFetching = true;
// Fetch file
const data = await this.wrapped.readFile(name);
this.triggerEventsAndCache(name, data.meta.lastModified);
return data;
} finally {
this.alreadyFetching = false;
}
}
2022-09-12 12:50:37 +00:00
async writeFile(
name: string,
data: Uint8Array,
2023-01-13 14:41:29 +00:00
selfUpdate?: boolean,
meta?: FileMeta,
2022-09-12 12:50:37 +00:00
): Promise<FileMeta> {
try {
this.alreadyFetching = true;
const newMeta = await this.wrapped.writeFile(
name,
data,
selfUpdate,
meta,
);
if (!selfUpdate) {
await this.dispatchEvent(
"file:changed",
name,
true,
undefined,
newMeta.lastModified,
);
}
this.spaceSnapshot[name] = newMeta.lastModified;
if (name.endsWith(".md")) {
// Let's trigger some page-specific events
const pageName = name.substring(0, name.length - 3);
let text = "";
const decoder = new TextDecoder("utf-8");
text = decoder.decode(data);
await this.dispatchEvent("page:saved", pageName, newMeta);
await this.dispatchEvent("page:index_text", {
name: pageName,
text,
});
}
return newMeta;
} finally {
this.alreadyFetching = false;
}
}
2023-08-27 12:13:18 +00:00
triggerEventsAndCache(name: string, newHash: number) {
const oldHash = this.spaceSnapshot[name];
2023-11-12 09:33:27 +00:00
if (oldHash && newHash && oldHash !== newHash) {
2023-08-27 12:13:18 +00:00
// Page changed since last cached metadata, trigger event
2023-11-12 09:33:27 +00:00
this.dispatchEvent("file:changed", name, false, oldHash, newHash);
2023-08-27 12:13:18 +00:00
}
this.spaceSnapshot[name] = newHash;
return;
}
2023-08-27 09:02:24 +00:00
async getFileMeta(name: string): Promise<FileMeta> {
try {
this.alreadyFetching = true;
2023-08-27 09:02:24 +00:00
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);
2023-08-27 09:02:24 +00:00
if (e.message === "Not found") {
await this.dispatchEvent("file:deleted", name);
2023-08-27 09:02:24 +00:00
if (name.endsWith(".md")) {
const pageName = name.substring(0, name.length - 3);
await this.dispatchEvent("page:deleted", pageName);
}
}
throw e;
} finally {
this.alreadyFetching = false;
2023-08-27 09:02:24 +00:00
}
}
2022-09-12 12:50:37 +00:00
async deleteFile(name: string): Promise<void> {
try {
this.alreadyFetching = true;
if (name.endsWith(".md")) {
const pageName = name.substring(0, name.length - 3);
await this.dispatchEvent("page:deleted", pageName);
}
// await this.getPageMeta(name); // Check if page exists, if not throws Error
await this.wrapped.deleteFile(name);
delete this.spaceSnapshot[name];
await this.dispatchEvent("file:deleted", name);
} finally {
this.alreadyFetching = false;
2023-08-27 09:02:24 +00:00
}
}
}