2023-05-23 18:53:53 +00:00
|
|
|
import { SpacePrimitives } from "../common/spaces/space_primitives.ts";
|
|
|
|
import { plugPrefix } from "../common/spaces/constants.ts";
|
|
|
|
import { safeRun } from "../common/util.ts";
|
2023-08-29 19:17:29 +00:00
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
import { AttachmentMeta, FileMeta, PageMeta } from "$sb/types.ts";
|
2023-08-27 09:02:24 +00:00
|
|
|
import { EventHook } from "../plugos/hooks/event.ts";
|
2023-08-29 19:17:29 +00:00
|
|
|
import { throttle } from "$sb/lib/async.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { DataStore } from "../plugos/lib/datastore.ts";
|
|
|
|
import { LimitedMap } from "../common/limited_map.ts";
|
2022-04-05 15:02:17 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
const pageWatchInterval = 5000;
|
|
|
|
|
2023-08-27 09:02:24 +00:00
|
|
|
export class Space {
|
2023-10-03 12:16:33 +00:00
|
|
|
imageHeightCache = new LimitedMap<number>(100); // url -> height
|
|
|
|
widgetHeightCache = new LimitedMap<number>(100); // bodytext -> height
|
2023-08-27 09:02:24 +00:00
|
|
|
cachedPageList: PageMeta[] = [];
|
2023-05-29 08:26:56 +00:00
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
debouncedImageCacheFlush = throttle(() => {
|
|
|
|
this.ds.set(["cache", "imageHeight"], this.imageHeightCache).catch(
|
2023-05-29 08:26:56 +00:00
|
|
|
console.error,
|
|
|
|
);
|
|
|
|
console.log("Flushed image height cache to store");
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
setCachedImageHeight(url: string, height: number) {
|
2023-10-03 12:16:33 +00:00
|
|
|
this.imageHeightCache.set(url, height);
|
|
|
|
this.debouncedImageCacheFlush();
|
2023-05-29 08:26:56 +00:00
|
|
|
}
|
|
|
|
getCachedImageHeight(url: string): number {
|
2023-10-03 12:16:33 +00:00
|
|
|
return this.imageHeightCache.get(url) ?? -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
debouncedWidgetCacheFlush = throttle(() => {
|
|
|
|
this.ds.set(["cache", "widgetHeight"], this.widgetHeightCache.toJSON())
|
|
|
|
.catch(
|
|
|
|
console.error,
|
|
|
|
);
|
|
|
|
console.log("Flushed widget height cache to store");
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
setCachedWidgetHeight(bodyText: string, height: number) {
|
|
|
|
this.widgetHeightCache.set(bodyText, height);
|
|
|
|
this.debouncedWidgetCacheFlush();
|
|
|
|
}
|
|
|
|
getCachedWidgetHeight(bodyText: string): number {
|
|
|
|
return this.widgetHeightCache.get(bodyText) ?? -1;
|
2023-05-29 08:26:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
// 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>();
|
2023-05-23 18:53:53 +00:00
|
|
|
watchInterval?: number;
|
|
|
|
|
2023-08-27 09:02:24 +00:00
|
|
|
// private initialPageListLoad = true;
|
2022-04-07 13:21:30 +00:00
|
|
|
private saving = false;
|
|
|
|
|
2023-05-29 08:26:56 +00:00
|
|
|
constructor(
|
|
|
|
readonly spacePrimitives: SpacePrimitives,
|
2023-10-03 12:16:33 +00:00
|
|
|
private ds: DataStore,
|
2023-08-27 09:02:24 +00:00
|
|
|
private eventHook: EventHook,
|
2023-05-29 08:26:56 +00:00
|
|
|
) {
|
2023-08-27 09:02:24 +00:00
|
|
|
// super();
|
2023-10-03 12:16:33 +00:00
|
|
|
this.ds.batchGet([["cache", "imageHeight"], ["cache", "widgetHeight"]])
|
|
|
|
.then(([imageCache, widgetCache]) => {
|
|
|
|
if (imageCache) {
|
|
|
|
this.imageHeightCache = new LimitedMap(100, imageCache);
|
|
|
|
}
|
|
|
|
if (widgetCache) {
|
|
|
|
// console.log("Loaded widget cache from store", widgetCache);
|
|
|
|
this.widgetHeightCache = new LimitedMap(100, widgetCache);
|
|
|
|
}
|
|
|
|
});
|
2023-08-27 09:02:24 +00:00
|
|
|
eventHook.addLocalListener("file:listed", (files: FileMeta[]) => {
|
|
|
|
this.cachedPageList = files.filter(this.isListedPage).map(
|
|
|
|
fileMetaToPageMeta,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
eventHook.addLocalListener("page:deleted", (pageName: string) => {
|
|
|
|
if (this.watchedPages.has(pageName)) {
|
|
|
|
// Stop watching deleted pages already
|
|
|
|
this.watchedPages.delete(pageName);
|
2022-04-07 13:21:30 +00:00
|
|
|
}
|
|
|
|
});
|
2023-08-27 09:02:24 +00:00
|
|
|
}
|
2022-04-26 17:04:36 +00:00
|
|
|
|
2023-08-27 09:02:24 +00:00
|
|
|
public async updatePageList() {
|
|
|
|
// This will trigger appropriate events automatically
|
|
|
|
await this.fetchPageList();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
async getPageMeta(name: string): Promise<PageMeta> {
|
2023-08-27 09:02:24 +00:00
|
|
|
return fileMetaToPageMeta(
|
2023-01-13 14:41:29 +00:00
|
|
|
await this.spacePrimitives.getFileMeta(`${name}.md`),
|
2022-09-12 12:50:37 +00:00
|
|
|
);
|
2022-04-07 13:21:30 +00:00
|
|
|
}
|
2022-04-05 15:02:17 +00:00
|
|
|
|
2023-01-17 08:34:49 +00:00
|
|
|
listPages(): PageMeta[] {
|
2023-08-27 09:02:24 +00:00
|
|
|
return this.cachedPageList;
|
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
|
2023-05-23 18:53:53 +00:00
|
|
|
.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 }> {
|
2023-05-23 18:53:53 +00:00
|
|
|
const pageData = await this.spacePrimitives.readFile(`${name}.md`);
|
2022-09-12 12:50:37 +00:00
|
|
|
return {
|
2023-05-23 18:53:53 +00:00
|
|
|
text: new TextDecoder().decode(pageData.data),
|
2023-08-27 09:02:24 +00:00
|
|
|
meta: fileMetaToPageMeta(pageData.meta),
|
2022-09-12 12:50:37 +00:00
|
|
|
};
|
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;
|
2023-08-27 09:02:24 +00:00
|
|
|
return fileMetaToPageMeta(
|
2023-01-13 14:41:29 +00:00
|
|
|
await this.spacePrimitives.writeFile(
|
|
|
|
`${name}.md`,
|
2023-05-23 18:53:53 +00:00
|
|
|
new TextEncoder().encode(text),
|
2023-01-13 14:41:29 +00:00
|
|
|
selfUpdate,
|
|
|
|
),
|
2022-04-07 13:21:30 +00:00
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
this.saving = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 05:56:29 +00:00
|
|
|
// We're listing all pages that don't start with a _
|
2023-08-27 09:02:24 +00:00
|
|
|
isListedPage(fileMeta: FileMeta): boolean {
|
|
|
|
return fileMeta.name.endsWith(".md") && !fileMeta.name.startsWith("_");
|
2023-08-15 05:56:29 +00:00
|
|
|
}
|
|
|
|
|
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-27 09:02:24 +00:00
|
|
|
.filter(this.isListedPage)
|
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-27 09:02:24 +00:00
|
|
|
!this.isListedPage(fileMeta) &&
|
2023-05-23 18:53:53 +00:00
|
|
|
!fileMeta.name.endsWith(".plug.js"),
|
2022-09-12 12:50:37 +00:00
|
|
|
);
|
2022-09-05 09:47:30 +00:00
|
|
|
}
|
2022-09-12 12:50:37 +00:00
|
|
|
|
2023-01-08 14:29:34 +00:00
|
|
|
/**
|
|
|
|
* Reads an attachment
|
|
|
|
* @param name path of the attachment
|
|
|
|
* @returns
|
|
|
|
*/
|
2023-05-26 12:04:32 +00:00
|
|
|
readAttachment(
|
2022-09-05 14:15:01 +00:00
|
|
|
name: string,
|
2023-05-26 12:04:32 +00:00
|
|
|
): Promise<{ data: Uint8Array; meta: AttachmentMeta }> {
|
|
|
|
return this.spacePrimitives.readFile(name);
|
2022-09-05 09:47:30 +00:00
|
|
|
}
|
2022-09-12 12:50:37 +00:00
|
|
|
|
2022-09-05 09:47:30 +00:00
|
|
|
getAttachmentMeta(name: string): Promise<AttachmentMeta> {
|
2023-01-13 14:41:29 +00:00
|
|
|
return this.spacePrimitives.getFileMeta(name);
|
2022-09-05 09:47:30 +00:00
|
|
|
}
|
2022-09-12 12:50:37 +00:00
|
|
|
|
2022-09-05 09:47:30 +00:00
|
|
|
writeAttachment(
|
|
|
|
name: string,
|
2023-05-26 12:04:32 +00:00
|
|
|
data: Uint8Array,
|
2023-06-13 18:47:05 +00:00
|
|
|
selfUpdate?: boolean,
|
2022-09-05 09:47:30 +00:00
|
|
|
): Promise<AttachmentMeta> {
|
2023-06-13 18:47:05 +00:00
|
|
|
return this.spacePrimitives.writeFile(name, data, selfUpdate);
|
2022-09-05 09:47:30 +00:00
|
|
|
}
|
2022-09-12 12:50:37 +00:00
|
|
|
|
2022-09-05 09:47:30 +00:00
|
|
|
deleteAttachment(name: string): Promise<void> {
|
2023-01-13 14:41:29 +00:00
|
|
|
return this.spacePrimitives.deleteFile(name);
|
2022-09-05 09:47:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
// 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);
|
|
|
|
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-04-05 15:02:17 +00:00
|
|
|
}
|
2022-09-12 12:50:37 +00:00
|
|
|
|
2023-08-27 09:02:24 +00:00
|
|
|
export function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
|
2022-09-12 12:50:37 +00:00
|
|
|
return {
|
|
|
|
...fileMeta,
|
|
|
|
name: fileMeta.name.substring(0, fileMeta.name.length - 3),
|
|
|
|
} as PageMeta;
|
|
|
|
}
|