1
0
silverbullet/web/space.ts

271 lines
7.9 KiB
TypeScript
Raw Normal View History

import { SpacePrimitives } from "../common/spaces/space_primitives.ts";
import { FileMeta } from "../common/types.ts";
import { EventEmitter } from "../plugos/event.ts";
import { plugPrefix } from "../common/spaces/constants.ts";
import { safeRun } from "../common/util.ts";
import { AttachmentMeta, PageMeta } from "./types.ts";
2023-05-29 08:26:56 +00:00
import { throttle } from "../common/async_util.ts";
2023-08-04 16:56:55 +00:00
import { KVStore } from "../plugos/lib/kv_store.ts";
2022-04-07 13:21:30 +00:00
2022-04-05 15:02:17 +00:00
export type SpaceEvents = {
pageCreated: (meta: PageMeta) => void;
pageChanged: (meta: PageMeta) => void;
pageDeleted: (name: string) => void;
pageListUpdated: (pages: PageMeta[]) => void;
2022-04-05 15:02:17 +00:00
};
const pageWatchInterval = 5000;
export class Space extends EventEmitter<SpaceEvents> {
2023-05-29 08:26:56 +00:00
imageHeightCache: Record<string, number> = {};
2023-08-15 05:56:29 +00:00
pageMetaCache = new Map<string, PageMeta>();
2023-05-29 08:26:56 +00:00
debouncedCacheFlush = throttle(() => {
this.kvStore.set("imageHeightCache", this.imageHeightCache).catch(
console.error,
);
console.log("Flushed image height cache to store");
}, 5000);
setCachedImageHeight(url: string, height: number) {
this.imageHeightCache[url] = height;
this.debouncedCacheFlush();
}
getCachedImageHeight(url: string): number {
return this.imageHeightCache[url] ?? -1;
}
// We do watch files in the background to detect changes
// This set of pages should only ever contain 1 page
2022-04-07 13:21:30 +00:00
watchedPages = new Set<string>();
watchInterval?: number;
2022-04-07 13:21:30 +00:00
private initialPageListLoad = true;
private saving = false;
2023-05-29 08:26:56 +00:00
constructor(
readonly spacePrimitives: SpacePrimitives,
2023-08-04 16:56:55 +00:00
private kvStore: KVStore,
2023-05-29 08:26:56 +00:00
) {
2022-04-07 13:21:30 +00:00
super();
2023-05-29 08:26:56 +00:00
this.kvStore.get("imageHeightCache").then((cache) => {
if (cache) {
// console.log("Loaded image height cache from KV store", cache);
2023-05-29 08:26:56 +00:00
this.imageHeightCache = cache;
}
});
2022-04-07 13:21:30 +00:00
}
2022-04-26 17:04:36 +00:00
public async updatePageList() {
2022-10-15 17:02:56 +00:00
const newPageList = await this.fetchPageList();
const deletedPages = new Set<string>(this.pageMetaCache.keys());
2022-09-12 12:50:37 +00:00
newPageList.forEach((meta) => {
2022-04-26 17:04:36 +00:00
const pageName = meta.name;
const oldPageMeta = this.pageMetaCache.get(pageName);
const newPageMeta: PageMeta = { ...meta };
2022-04-26 17:04:36 +00:00
if (
!oldPageMeta &&
(pageName.startsWith(plugPrefix) || !this.initialPageListLoad)
) {
this.emit("pageCreated", newPageMeta);
} else if (
oldPageMeta &&
2022-09-12 12:50:37 +00:00
oldPageMeta.lastModified !== newPageMeta.lastModified
2022-04-26 17:04:36 +00:00
) {
this.emit("pageChanged", newPageMeta);
2022-04-07 13:21:30 +00:00
}
2022-04-26 17:04:36 +00:00
// Page found, not deleted
deletedPages.delete(pageName);
2022-04-07 13:21:30 +00:00
2022-04-26 17:04:36 +00:00
// Update in cache
this.pageMetaCache.set(pageName, newPageMeta);
2022-04-07 13:21:30 +00:00
});
2022-04-26 17:04:36 +00:00
for (const deletedPage of deletedPages) {
this.pageMetaCache.delete(deletedPage);
this.emit("pageDeleted", deletedPage);
}
this.emit("pageListUpdated", this.listPages());
this.initialPageListLoad = false;
2022-04-07 13:21:30 +00:00
}
2022-10-15 17:02:56 +00:00
async deletePage(name: string): Promise<void> {
2022-04-07 13:21:30 +00:00
await this.getPageMeta(name); // Check if page exists, if not throws Error
2023-01-13 14:41:29 +00:00
await this.spacePrimitives.deleteFile(`${name}.md`);
2022-04-07 13:21:30 +00:00
this.pageMetaCache.delete(name);
this.emit("pageDeleted", name);
this.emit("pageListUpdated", [...this.pageMetaCache.values()]);
2022-04-07 13:21:30 +00:00
}
async getPageMeta(name: string): Promise<PageMeta> {
2022-10-15 17:02:56 +00:00
const oldMeta = this.pageMetaCache.get(name);
const newMeta = fileMetaToPageMeta(
2023-01-13 14:41:29 +00:00
await this.spacePrimitives.getFileMeta(`${name}.md`),
2022-09-12 12:50:37 +00:00
);
if (oldMeta) {
if (oldMeta.lastModified !== newMeta.lastModified) {
// Changed on disk, trigger event
this.emit("pageChanged", newMeta);
}
}
return this.metaCacher(name, newMeta);
2022-04-07 13:21:30 +00:00
}
2022-04-05 15:02:17 +00:00
listPages(): PageMeta[] {
return [...new Set(this.pageMetaCache.values())];
2022-04-07 13:21:30 +00:00
}
2022-09-12 12:50:37 +00:00
async listPlugs(): Promise<string[]> {
2023-01-13 14:41:29 +00:00
const files = await this.spacePrimitives.fetchFileList();
return files
.filter((fileMeta) =>
fileMeta.name.startsWith(plugPrefix) &&
fileMeta.name.endsWith(".plug.js")
)
2022-09-12 12:50:37 +00:00
.map((fileMeta) => fileMeta.name);
2022-04-07 13:21:30 +00:00
}
async readPage(name: string): Promise<{ text: string; meta: PageMeta }> {
const pageData = await this.spacePrimitives.readFile(`${name}.md`);
2022-10-15 17:02:56 +00:00
const previousMeta = this.pageMetaCache.get(name);
const newMeta = fileMetaToPageMeta(pageData.meta);
if (previousMeta) {
2022-09-12 12:50:37 +00:00
if (previousMeta.lastModified !== newMeta.lastModified) {
// Page changed since last cached metadata, trigger event
2022-09-12 12:50:37 +00:00
this.emit("pageChanged", newMeta);
}
}
2022-10-15 17:02:56 +00:00
const meta = this.metaCacher(name, newMeta);
2022-09-12 12:50:37 +00:00
return {
text: new TextDecoder().decode(pageData.data),
2022-09-12 12:50:37 +00:00
meta: meta,
};
2022-04-07 13:21:30 +00:00
}
async writePage(
name: string,
text: string,
2022-10-10 16:19:08 +00:00
selfUpdate?: boolean,
2022-04-07 13:21:30 +00:00
): Promise<PageMeta> {
try {
this.saving = true;
2022-10-10 16:19:08 +00:00
const pageMeta = fileMetaToPageMeta(
2023-01-13 14:41:29 +00:00
await this.spacePrimitives.writeFile(
`${name}.md`,
new TextEncoder().encode(text),
2023-01-13 14:41:29 +00:00
selfUpdate,
),
2022-04-07 13:21:30 +00:00
);
if (!selfUpdate) {
this.emit("pageChanged", pageMeta);
}
return this.metaCacher(name, pageMeta);
} finally {
this.saving = false;
}
}
2023-08-15 05:56:29 +00:00
// We're listing all pages that don't start with a _
isListedPageName(name: string): boolean {
return name.endsWith(".md") && !name.startsWith("_");
}
2022-09-12 12:50:37 +00:00
async fetchPageList(): Promise<PageMeta[]> {
2023-01-13 14:41:29 +00:00
return (await this.spacePrimitives.fetchFileList())
2023-08-15 05:56:29 +00:00
.filter((fileMeta) => this.isListedPageName(fileMeta.name))
2022-09-12 12:50:37 +00:00
.map(fileMetaToPageMeta);
2022-04-07 13:21:30 +00:00
}
2022-09-12 12:50:37 +00:00
async fetchAttachmentList(): Promise<AttachmentMeta[]> {
2023-01-13 14:41:29 +00:00
return (await this.spacePrimitives.fetchFileList()).filter(
2022-09-12 12:50:37 +00:00
(fileMeta) =>
2023-08-15 05:56:29 +00:00
!this.isListedPageName(fileMeta.name) &&
!fileMeta.name.endsWith(".plug.js"),
2022-09-12 12:50:37 +00:00
);
}
2022-09-12 12:50:37 +00:00
/**
* Reads an attachment
* @param name path of the attachment
* @returns
*/
readAttachment(
2022-09-05 14:15:01 +00:00
name: string,
): Promise<{ data: Uint8Array; meta: AttachmentMeta }> {
return this.spacePrimitives.readFile(name);
}
2022-09-12 12:50:37 +00:00
getAttachmentMeta(name: string): Promise<AttachmentMeta> {
2023-01-13 14:41:29 +00:00
return this.spacePrimitives.getFileMeta(name);
}
2022-09-12 12:50:37 +00:00
writeAttachment(
name: string,
data: Uint8Array,
selfUpdate?: boolean,
): Promise<AttachmentMeta> {
return this.spacePrimitives.writeFile(name, data, selfUpdate);
}
2022-09-12 12:50:37 +00:00
deleteAttachment(name: string): Promise<void> {
2023-01-13 14:41:29 +00: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) {
const oldMeta = this.pageMetaCache.get(pageName);
if (!oldMeta) {
// No longer in cache, meaning probably deleted let's unwatch
this.watchedPages.delete(pageName);
continue;
}
// This seems weird, but simply fetching it will compare to local cache and trigger an event if necessary
await this.getPageMeta(pageName);
}
});
}, pageWatchInterval);
this.updatePageList().catch(console.error);
}
unwatch() {
if (this.watchInterval) {
clearInterval(this.watchInterval);
}
}
watchPage(pageName: string) {
this.watchedPages.add(pageName);
}
unwatchPage(pageName: string) {
this.watchedPages.delete(pageName);
}
2022-09-12 12:50:37 +00:00
private metaCacher(name: string, meta: PageMeta): PageMeta {
if (meta.lastModified !== 0) {
// Don't cache metadata for pages with a 0 lastModified timestamp (usualy dynamically generated pages)
this.pageMetaCache.set(name, meta);
}
2022-09-12 12:50:37 +00:00
return meta;
2022-04-07 13:21:30 +00:00
}
2022-04-05 15:02:17 +00:00
}
2022-09-12 12:50:37 +00:00
function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
return {
...fileMeta,
name: fileMeta.name.substring(0, fileMeta.name.length - 3),
} as PageMeta;
}