silverbullet/common/space.ts

223 lines
6.1 KiB
TypeScript
Raw Normal View History

2024-07-30 23:33:33 +08:00
import type { SpacePrimitives } from "$common/spaces/space_primitives.ts";
import { plugPrefix } from "$common/spaces/constants.ts";
2023-08-30 03:17:29 +08:00
2024-07-30 23:33:33 +08:00
import type { AttachmentMeta, FileMeta, PageMeta } from "../plug-api/types.ts";
import type { EventHook } from "./hooks/event.ts";
import { safeRun } from "../lib/async.ts";
2022-04-05 23:02:17 +08:00
const pageWatchInterval = 5000;
2023-08-27 17:02:24 +08:00
export class Space {
// We do watch files in the background to detect changes
// This set of pages should only ever contain 1 page
2022-04-07 21:21:30 +08:00
watchedPages = new Set<string>();
watchInterval?: number;
2023-08-27 17:02:24 +08:00
// private initialPageListLoad = true;
2022-04-07 21:21:30 +08:00
private saving = false;
2023-05-29 16:26:56 +08:00
constructor(
readonly spacePrimitives: SpacePrimitives,
eventHook: EventHook,
2023-05-29 16:26:56 +08:00
) {
2023-08-27 17:02:24 +08:00
eventHook.addLocalListener("page:deleted", (pageName: string) => {
if (this.watchedPages.has(pageName)) {
// Stop watching deleted pages already
this.watchedPages.delete(pageName);
2022-04-07 21:21:30 +08:00
}
});
2024-01-21 03:57:05 +08:00
setTimeout(() => {
// Next tick, to ensure that the space is initialized
this.updatePageList().catch(console.error);
});
2023-08-27 17:02:24 +08:00
}
2022-04-27 01:04:36 +08:00
2023-12-22 22:55:50 +08:00
public async updatePageList() {
// The only reason to do this is to trigger events
await this.fetchPageList();
2022-04-07 21:21:30 +08:00
}
2022-10-16 01:02:56 +08:00
async deletePage(name: string): Promise<void> {
2022-04-07 21:21:30 +08:00
await this.getPageMeta(name); // Check if page exists, if not throws Error
2023-01-13 22:41:29 +08:00
await this.spacePrimitives.deleteFile(`${name}.md`);
2022-04-07 21:21:30 +08:00
}
async getPageMeta(name: string): Promise<PageMeta> {
2023-08-27 17:02:24 +08:00
return fileMetaToPageMeta(
2023-01-13 22:41:29 +08:00
await this.spacePrimitives.getFileMeta(`${name}.md`),
2022-09-12 20:50:37 +08:00
);
2022-04-07 21:21:30 +08:00
}
2022-04-05 23:02:17 +08:00
async listPlugs(): Promise<FileMeta[]> {
const files = await this.deduplicatedFileList();
2023-01-13 22:41:29 +08:00
return files
.filter((fileMeta) =>
fileMeta.name.startsWith(plugPrefix) &&
fileMeta.name.endsWith(".plug.js")
);
2022-04-07 21:21:30 +08:00
}
async readPage(name: string): Promise<{ text: string; meta: PageMeta }> {
const pageData = await this.spacePrimitives.readFile(`${name}.md`);
2022-09-12 20:50:37 +08:00
return {
text: new TextDecoder().decode(pageData.data),
2023-08-27 17:02:24 +08:00
meta: fileMetaToPageMeta(pageData.meta),
2022-09-12 20:50:37 +08:00
};
2022-04-07 21:21:30 +08:00
}
async writePage(
name: string,
text: string,
2022-10-11 00:19:08 +08:00
selfUpdate?: boolean,
2022-04-07 21:21:30 +08:00
): Promise<PageMeta> {
try {
this.saving = true;
const pageMeta = fileMetaToPageMeta(
2023-01-13 22:41:29 +08:00
await this.spacePrimitives.writeFile(
`${name}.md`,
new TextEncoder().encode(text),
2023-01-13 22:41:29 +08:00
selfUpdate,
),
2022-04-07 21:21:30 +08:00
);
// Note: we don't do very elaborate cache invalidation work here, quite quickly the cache will be flushed anyway
return pageMeta;
2022-04-07 21:21:30 +08:00
} finally {
this.saving = false;
}
}
2023-08-15 13:56:29 +08:00
// We're listing all pages that don't start with a _
2023-08-27 17:02:24 +08:00
isListedPage(fileMeta: FileMeta): boolean {
return fileMeta.name.endsWith(".md") && !fileMeta.name.startsWith("_");
2023-08-15 13:56:29 +08:00
}
2022-09-12 20:50:37 +08:00
async fetchPageList(): Promise<PageMeta[]> {
return (await this.deduplicatedFileList())
2023-08-27 17:02:24 +08:00
.filter(this.isListedPage)
2022-09-12 20:50:37 +08:00
.map(fileMetaToPageMeta);
2022-04-07 21:21:30 +08:00
}
2022-09-12 20:50:37 +08:00
async fetchAttachmentList(): Promise<AttachmentMeta[]> {
2024-05-28 02:33:41 +08:00
return (await this.deduplicatedFileList()).flatMap((fileMeta) =>
!this.isListedPage(fileMeta) &&
!fileMeta.name.endsWith(".plug.js")
? [fileMetaToAttachmentMeta(fileMeta)]
: []
2022-09-12 20:50:37 +08:00
);
}
2022-09-12 20:50:37 +08:00
async deduplicatedFileList(): Promise<FileMeta[]> {
const files = await this.spacePrimitives.fetchFileList();
const fileMap = new Map<string, FileMeta>();
for (const file of files) {
if (fileMap.has(file.name)) {
const existing = fileMap.get(file.name)!;
if (existing.lastModified < file.lastModified) {
fileMap.set(file.name, file);
}
} else {
fileMap.set(file.name, file);
}
}
return [...fileMap.values()];
}
/**
* Reads an attachment
* @param name path of the attachment
* @returns
*/
2024-05-28 02:33:41 +08:00
async readAttachment(
2022-09-05 22:15:01 +08:00
name: string,
): Promise<{ data: Uint8Array; meta: AttachmentMeta }> {
2024-05-28 02:33:41 +08:00
const file = await this.spacePrimitives.readFile(name);
return { data: file.data, meta: fileMetaToAttachmentMeta(file.meta) };
}
2022-09-12 20:50:37 +08:00
2024-05-28 02:33:41 +08:00
async getAttachmentMeta(name: string): Promise<AttachmentMeta> {
return fileMetaToAttachmentMeta(
await this.spacePrimitives.getFileMeta(name),
);
}
2022-09-12 20:50:37 +08:00
2024-05-28 02:33:41 +08:00
async writeAttachment(
name: string,
data: Uint8Array,
selfUpdate?: boolean,
): Promise<AttachmentMeta> {
2024-05-28 02:33:41 +08:00
return fileMetaToAttachmentMeta(
await this.spacePrimitives.writeFile(name, data, selfUpdate),
);
}
2022-09-12 20:50:37 +08:00
deleteAttachment(name: string): Promise<void> {
2023-01-13 22:41:29 +08:00
return this.spacePrimitives.deleteFile(name);
}
// Even though changes coming from a sync cycle will immediately trigger a reload
// there are scenarios in which other tabs run the sync, so we have to poll for changes
watch() {
if (this.watchInterval) {
clearInterval(this.watchInterval);
}
this.watchInterval = setInterval(() => {
safeRun(async () => {
if (this.saving) {
return;
}
for (const pageName of this.watchedPages) {
await this.getPageMeta(pageName);
}
});
}, pageWatchInterval);
}
unwatch() {
if (this.watchInterval) {
clearInterval(this.watchInterval);
}
}
watchPage(pageName: string) {
this.watchedPages.add(pageName);
}
unwatchPage(pageName: string) {
this.watchedPages.delete(pageName);
}
2022-04-05 23:02:17 +08:00
}
2022-09-12 20:50:37 +08:00
2023-08-27 17:02:24 +08:00
export function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
2023-11-06 16:14:16 +08:00
const name = fileMeta.name.substring(0, fileMeta.name.length - 3);
try {
return {
...fileMeta,
ref: name,
tag: "page",
name,
created: new Date(fileMeta.created).toISOString(),
lastModified: new Date(fileMeta.lastModified).toISOString(),
} as PageMeta;
} catch (e) {
console.error("Failed to convert fileMeta to pageMeta", fileMeta, e);
throw e;
}
2022-09-12 20:50:37 +08:00
}
2024-05-28 02:33:41 +08:00
export function fileMetaToAttachmentMeta(
fileMeta: FileMeta,
): AttachmentMeta {
try {
return {
...fileMeta,
ref: fileMeta.name,
tag: "attachment",
created: new Date(fileMeta.created).toISOString(),
lastModified: new Date(fileMeta.lastModified).toISOString(),
} as AttachmentMeta;
} catch (e) {
console.error("Failed to convert fileMeta to attachmentMeta", fileMeta, e);
throw e;
}
}