Huge event system refactoring
This commit is contained in:
parent
9ee9008bf2
commit
593597454a
@ -3,17 +3,96 @@ import { EventHook } from "../../plugos/hooks/event.ts";
|
|||||||
|
|
||||||
import type { SpacePrimitives } from "./space_primitives.ts";
|
import type { SpacePrimitives } from "./space_primitives.ts";
|
||||||
|
|
||||||
export class EventedSpacePrimitives implements SpacePrimitives {
|
/**
|
||||||
constructor(private wrapped: SpacePrimitives, private eventHook: EventHook) {}
|
* Events exposed:
|
||||||
|
* - file:changed (FileMeta)
|
||||||
|
* - file:deleted (string)
|
||||||
|
* - file:listed (FileMeta[])
|
||||||
|
* - page:saved (string, FileMeta)
|
||||||
|
* - page:deleted (string)
|
||||||
|
*/
|
||||||
|
|
||||||
fetchFileList(): Promise<FileMeta[]> {
|
export class EventedSpacePrimitives implements SpacePrimitives {
|
||||||
return this.wrapped.fetchFileList();
|
private fileMetaCache = new Map<string, FileMeta>();
|
||||||
|
initialFileListLoad = true;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private wrapped: SpacePrimitives,
|
||||||
|
private eventHook: EventHook,
|
||||||
|
private eventsToDispatch = [
|
||||||
|
"file:changed",
|
||||||
|
"file:deleted",
|
||||||
|
"file:listed",
|
||||||
|
"page:saved",
|
||||||
|
"page:deleted",
|
||||||
|
],
|
||||||
|
) {}
|
||||||
|
|
||||||
|
dispatchEvent(name: string, ...args: any[]): Promise<any[]> {
|
||||||
|
if (this.eventsToDispatch.includes(name)) {
|
||||||
|
return this.eventHook.dispatchEvent(name, ...args);
|
||||||
|
} else {
|
||||||
|
return Promise.resolve([]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readFile(
|
async fetchFileList(): Promise<FileMeta[]> {
|
||||||
|
const newFileList = await this.wrapped.fetchFileList();
|
||||||
|
const deletedFiles = new Set<string>(this.fileMetaCache.keys());
|
||||||
|
for (const meta of newFileList) {
|
||||||
|
const oldFileMeta = this.fileMetaCache.get(meta.name);
|
||||||
|
const newFileMeta: FileMeta = { ...meta };
|
||||||
|
if (
|
||||||
|
(
|
||||||
|
// New file scenario
|
||||||
|
!oldFileMeta && !this.initialFileListLoad
|
||||||
|
) || (
|
||||||
|
// Changed file scenario
|
||||||
|
oldFileMeta &&
|
||||||
|
oldFileMeta.lastModified !== newFileMeta.lastModified
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
this.dispatchEvent("file:changed", newFileMeta);
|
||||||
|
}
|
||||||
|
// Page found, not deleted
|
||||||
|
deletedFiles.delete(meta.name);
|
||||||
|
|
||||||
|
// Update in cache
|
||||||
|
this.fileMetaCache.set(meta.name, newFileMeta);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const deletedFile of deletedFiles) {
|
||||||
|
this.fileMetaCache.delete(deletedFile);
|
||||||
|
this.dispatchEvent("file:deleted", deletedFile);
|
||||||
|
|
||||||
|
if (deletedFile.endsWith(".md")) {
|
||||||
|
const pageName = deletedFile.substring(0, deletedFile.length - 3);
|
||||||
|
await this.dispatchEvent("page:deleted", pageName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileList = [...new Set(this.fileMetaCache.values())];
|
||||||
|
this.dispatchEvent("file:listed", fileList);
|
||||||
|
this.initialFileListLoad = false;
|
||||||
|
return fileList;
|
||||||
|
}
|
||||||
|
|
||||||
|
async readFile(
|
||||||
name: string,
|
name: string,
|
||||||
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
||||||
return this.wrapped.readFile(name);
|
const data = await this.wrapped.readFile(name);
|
||||||
|
const previousMeta = this.fileMetaCache.get(name);
|
||||||
|
const newMeta = data.meta;
|
||||||
|
if (previousMeta) {
|
||||||
|
if (previousMeta.lastModified !== newMeta.lastModified) {
|
||||||
|
// Page changed since last cached metadata, trigger event
|
||||||
|
this.dispatchEvent("file:changed", newMeta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
data: data.data,
|
||||||
|
meta: this.metaCacher(name, newMeta),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async writeFile(
|
async writeFile(
|
||||||
@ -28,6 +107,11 @@ export class EventedSpacePrimitives implements SpacePrimitives {
|
|||||||
selfUpdate,
|
selfUpdate,
|
||||||
meta,
|
meta,
|
||||||
);
|
);
|
||||||
|
if (!selfUpdate) {
|
||||||
|
this.dispatchEvent("file:changed", newMeta);
|
||||||
|
}
|
||||||
|
this.metaCacher(name, newMeta);
|
||||||
|
|
||||||
// This can happen async
|
// This can happen async
|
||||||
if (name.endsWith(".md")) {
|
if (name.endsWith(".md")) {
|
||||||
const pageName = name.substring(0, name.length - 3);
|
const pageName = name.substring(0, name.length - 3);
|
||||||
@ -35,10 +119,9 @@ export class EventedSpacePrimitives implements SpacePrimitives {
|
|||||||
const decoder = new TextDecoder("utf-8");
|
const decoder = new TextDecoder("utf-8");
|
||||||
text = decoder.decode(data);
|
text = decoder.decode(data);
|
||||||
|
|
||||||
this.eventHook
|
this.dispatchEvent("page:saved", pageName, newMeta)
|
||||||
.dispatchEvent("page:saved", pageName, newMeta)
|
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return this.eventHook.dispatchEvent("page:index_text", {
|
return this.dispatchEvent("page:index_text", {
|
||||||
name: pageName,
|
name: pageName,
|
||||||
text,
|
text,
|
||||||
});
|
});
|
||||||
@ -48,20 +131,51 @@ export class EventedSpacePrimitives implements SpacePrimitives {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (name.startsWith("_plug/") && name.endsWith(".plug.js")) {
|
if (name.startsWith("_plug/") && name.endsWith(".plug.js")) {
|
||||||
await this.eventHook.dispatchEvent("plug:changed", name);
|
await this.dispatchEvent("plug:changed", name);
|
||||||
}
|
}
|
||||||
return newMeta;
|
return newMeta;
|
||||||
}
|
}
|
||||||
|
|
||||||
getFileMeta(name: string): Promise<FileMeta> {
|
async getFileMeta(name: string): Promise<FileMeta> {
|
||||||
return this.wrapped.getFileMeta(name);
|
try {
|
||||||
|
const oldMeta = this.fileMetaCache.get(name);
|
||||||
|
const newMeta = await this.wrapped.getFileMeta(name);
|
||||||
|
if (oldMeta) {
|
||||||
|
if (oldMeta.lastModified !== newMeta.lastModified) {
|
||||||
|
// Changed on disk, trigger event
|
||||||
|
this.dispatchEvent("file:changed", newMeta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.metaCacher(name, newMeta);
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteFile(name: string): Promise<void> {
|
async deleteFile(name: string): Promise<void> {
|
||||||
if (name.endsWith(".md")) {
|
if (name.endsWith(".md")) {
|
||||||
const pageName = name.substring(0, name.length - 3);
|
const pageName = name.substring(0, name.length - 3);
|
||||||
await this.eventHook.dispatchEvent("page:deleted", pageName);
|
await this.dispatchEvent("page:deleted", pageName);
|
||||||
}
|
}
|
||||||
return this.wrapped.deleteFile(name);
|
// await this.getPageMeta(name); // Check if page exists, if not throws Error
|
||||||
|
await this.wrapped.deleteFile(name);
|
||||||
|
this.fileMetaCache.delete(name);
|
||||||
|
this.dispatchEvent("file:deleted", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private metaCacher(name: string, meta: FileMeta): FileMeta {
|
||||||
|
if (meta.lastModified !== 0) {
|
||||||
|
// Don't cache metadata for pages with a 0 lastModified timestamp (usualy dynamically generated pages)
|
||||||
|
this.fileMetaCache.set(name, meta);
|
||||||
|
}
|
||||||
|
return meta;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ export class ServerSystem {
|
|||||||
),
|
),
|
||||||
pageIndexCalls,
|
pageIndexCalls,
|
||||||
);
|
);
|
||||||
const space = new Space(this.spacePrimitives, this.kvStore);
|
const space = new Space(this.spacePrimitives, this.kvStore, eventHook);
|
||||||
|
|
||||||
// Add syscalls
|
// Add syscalls
|
||||||
this.system.registerSyscalls(
|
this.system.registerSyscalls(
|
||||||
|
@ -6,7 +6,7 @@ import {
|
|||||||
gitIgnoreCompiler,
|
gitIgnoreCompiler,
|
||||||
syntaxTree,
|
syntaxTree,
|
||||||
} from "../common/deps.ts";
|
} from "../common/deps.ts";
|
||||||
import { Space } from "./space.ts";
|
import { fileMetaToPageMeta, Space } from "./space.ts";
|
||||||
import { FilterOption, PageMeta } from "./types.ts";
|
import { FilterOption, PageMeta } from "./types.ts";
|
||||||
import { parseYamlSettings } from "../common/util.ts";
|
import { parseYamlSettings } from "../common/util.ts";
|
||||||
import { EventHook } from "../plugos/hooks/event.ts";
|
import { EventHook } from "../plugos/hooks/event.ts";
|
||||||
@ -37,6 +37,7 @@ import { DexieMQ } from "../plugos/lib/mq.dexie.ts";
|
|||||||
import { cleanPageRef } from "$sb/lib/resolve.ts";
|
import { cleanPageRef } from "$sb/lib/resolve.ts";
|
||||||
import { expandPropertyNames } from "$sb/lib/json.ts";
|
import { expandPropertyNames } from "$sb/lib/json.ts";
|
||||||
import { SpacePrimitives } from "../common/spaces/space_primitives.ts";
|
import { SpacePrimitives } from "../common/spaces/space_primitives.ts";
|
||||||
|
import { FileMeta } from "$sb/types.ts";
|
||||||
const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/;
|
const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/;
|
||||||
|
|
||||||
const autoSaveInterval = 1000;
|
const autoSaveInterval = 1000;
|
||||||
@ -320,6 +321,12 @@ export class Client {
|
|||||||
}
|
}
|
||||||
})().catch(console.error);
|
})().catch(console.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this.eventHook.addLocalListener("page:deleted", (pageName) => {
|
||||||
|
// if (pageName === this.currentPage) {
|
||||||
|
// this.flashNotification("Page does exist, creating as a new page");
|
||||||
|
// }
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
initSpace(): SpacePrimitives {
|
initSpace(): SpacePrimitives {
|
||||||
@ -365,26 +372,37 @@ export class Client {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
localSpacePrimitives = this.plugSpaceRemotePrimitives;
|
localSpacePrimitives = new EventedSpacePrimitives(
|
||||||
|
this.plugSpaceRemotePrimitives,
|
||||||
|
this.eventHook,
|
||||||
|
[
|
||||||
|
"file:changed",
|
||||||
|
"file:listed",
|
||||||
|
"page:deleted",
|
||||||
|
],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.space = new Space(localSpacePrimitives, this.kvStore);
|
this.space = new Space(localSpacePrimitives, this.kvStore, this.eventHook);
|
||||||
|
|
||||||
this.space.on({
|
this.eventHook.addLocalListener("file:changed", (fileMeta: FileMeta) => {
|
||||||
pageChanged: (meta) => {
|
// Only reload when watching the current page (to avoid reloading when switching pages)
|
||||||
// Only reload when watching the current page (to avoid reloading when switching pages)
|
if (
|
||||||
if (this.space.watchInterval && this.currentPage === meta.name) {
|
this.space.watchInterval && `${this.currentPage}.md` === fileMeta.name
|
||||||
console.log("Page changed elsewhere, reloading");
|
) {
|
||||||
this.flashNotification("Page changed elsewhere, reloading");
|
console.log("Page changed elsewhere, reloading");
|
||||||
this.reloadPage();
|
this.flashNotification("Page changed elsewhere, reloading");
|
||||||
}
|
this.reloadPage();
|
||||||
},
|
}
|
||||||
pageListUpdated: (pages) => {
|
});
|
||||||
this.ui.viewDispatch({
|
|
||||||
type: "pages-listed",
|
this.eventHook.addLocalListener("file:listed", (fileList: FileMeta[]) => {
|
||||||
pages: pages,
|
this.ui.viewDispatch({
|
||||||
});
|
type: "pages-listed",
|
||||||
},
|
pages: fileList.filter((f) => f.name.endsWith(".md")).map(
|
||||||
|
fileMetaToPageMeta,
|
||||||
|
),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
this.space.watch();
|
this.space.watch();
|
||||||
|
@ -111,6 +111,24 @@ export class ClientSystem {
|
|||||||
}
|
}
|
||||||
this.plugsUpdated = true;
|
this.plugsUpdated = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Debugging
|
||||||
|
// this.eventHook.addLocalListener("file:listed", (files) => {
|
||||||
|
// console.log("New file list", files);
|
||||||
|
// });
|
||||||
|
|
||||||
|
this.eventHook.addLocalListener("file:changed", (file) => {
|
||||||
|
console.log("File changed", file);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.eventHook.addLocalListener("file:created", (file) => {
|
||||||
|
console.log("File created", file);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.eventHook.addLocalListener("file:deleted", (file) => {
|
||||||
|
console.log("File deleted", file);
|
||||||
|
});
|
||||||
|
|
||||||
this.registerSyscalls();
|
this.registerSyscalls();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
145
web/space.ts
145
web/space.ts
@ -1,24 +1,18 @@
|
|||||||
import { SpacePrimitives } from "../common/spaces/space_primitives.ts";
|
import { SpacePrimitives } from "../common/spaces/space_primitives.ts";
|
||||||
import { EventEmitter } from "../plugos/event.ts";
|
|
||||||
import { plugPrefix } from "../common/spaces/constants.ts";
|
import { plugPrefix } from "../common/spaces/constants.ts";
|
||||||
import { safeRun } from "../common/util.ts";
|
import { safeRun } from "../common/util.ts";
|
||||||
import { AttachmentMeta, PageMeta } from "./types.ts";
|
import { AttachmentMeta, PageMeta } from "./types.ts";
|
||||||
import { throttle } from "../common/async_util.ts";
|
import { throttle } from "../common/async_util.ts";
|
||||||
import { KVStore } from "../plugos/lib/kv_store.ts";
|
import { KVStore } from "../plugos/lib/kv_store.ts";
|
||||||
import { FileMeta } from "$sb/types.ts";
|
import { FileMeta } from "$sb/types.ts";
|
||||||
|
import { EventHook } from "../plugos/hooks/event.ts";
|
||||||
export type SpaceEvents = {
|
|
||||||
pageCreated: (meta: PageMeta) => void;
|
|
||||||
pageChanged: (meta: PageMeta) => void;
|
|
||||||
pageDeleted: (name: string) => void;
|
|
||||||
pageListUpdated: (pages: PageMeta[]) => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
const pageWatchInterval = 5000;
|
const pageWatchInterval = 5000;
|
||||||
|
|
||||||
export class Space extends EventEmitter<SpaceEvents> {
|
export class Space {
|
||||||
imageHeightCache: Record<string, number> = {};
|
imageHeightCache: Record<string, number> = {};
|
||||||
pageMetaCache = new Map<string, PageMeta>();
|
// pageMetaCache = new Map<string, PageMeta>();
|
||||||
|
cachedPageList: PageMeta[] = [];
|
||||||
|
|
||||||
debouncedCacheFlush = throttle(() => {
|
debouncedCacheFlush = throttle(() => {
|
||||||
this.kvStore.set("imageHeightCache", this.imageHeightCache).catch(
|
this.kvStore.set("imageHeightCache", this.imageHeightCache).catch(
|
||||||
@ -40,81 +34,82 @@ export class Space extends EventEmitter<SpaceEvents> {
|
|||||||
watchedPages = new Set<string>();
|
watchedPages = new Set<string>();
|
||||||
watchInterval?: number;
|
watchInterval?: number;
|
||||||
|
|
||||||
private initialPageListLoad = true;
|
// private initialPageListLoad = true;
|
||||||
private saving = false;
|
private saving = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
readonly spacePrimitives: SpacePrimitives,
|
readonly spacePrimitives: SpacePrimitives,
|
||||||
private kvStore: KVStore,
|
private kvStore: KVStore,
|
||||||
|
private eventHook: EventHook,
|
||||||
) {
|
) {
|
||||||
super();
|
// super();
|
||||||
this.kvStore.get("imageHeightCache").then((cache) => {
|
this.kvStore.get("imageHeightCache").then((cache) => {
|
||||||
if (cache) {
|
if (cache) {
|
||||||
// console.log("Loaded image height cache from KV store", cache);
|
// console.log("Loaded image height cache from KV store", cache);
|
||||||
this.imageHeightCache = cache;
|
this.imageHeightCache = cache;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updatePageList() {
|
public async updatePageList() {
|
||||||
const newPageList = await this.fetchPageList();
|
// This will trigger appropriate events automatically
|
||||||
const deletedPages = new Set<string>(this.pageMetaCache.keys());
|
await this.fetchPageList();
|
||||||
newPageList.forEach((meta) => {
|
// const deletedPages = new Set<string>(this.pageMetaCache.keys());
|
||||||
const pageName = meta.name;
|
// newPageList.forEach((meta) => {
|
||||||
const oldPageMeta = this.pageMetaCache.get(pageName);
|
// const pageName = meta.name;
|
||||||
const newPageMeta: PageMeta = { ...meta };
|
// const oldPageMeta = this.pageMetaCache.get(pageName);
|
||||||
if (
|
// const newPageMeta: PageMeta = { ...meta };
|
||||||
!oldPageMeta &&
|
// if (
|
||||||
(pageName.startsWith(plugPrefix) || !this.initialPageListLoad)
|
// !oldPageMeta &&
|
||||||
) {
|
// (pageName.startsWith(plugPrefix) || !this.initialPageListLoad)
|
||||||
this.emit("pageCreated", newPageMeta);
|
// ) {
|
||||||
} else if (
|
// this.emit("pageCreated", newPageMeta);
|
||||||
oldPageMeta &&
|
// } else if (
|
||||||
oldPageMeta.lastModified !== newPageMeta.lastModified
|
// oldPageMeta &&
|
||||||
) {
|
// oldPageMeta.lastModified !== newPageMeta.lastModified
|
||||||
this.emit("pageChanged", newPageMeta);
|
// ) {
|
||||||
}
|
// this.emit("pageChanged", newPageMeta);
|
||||||
// Page found, not deleted
|
// }
|
||||||
deletedPages.delete(pageName);
|
// // Page found, not deleted
|
||||||
|
// deletedPages.delete(pageName);
|
||||||
|
|
||||||
// Update in cache
|
// // Update in cache
|
||||||
this.pageMetaCache.set(pageName, newPageMeta);
|
// this.pageMetaCache.set(pageName, newPageMeta);
|
||||||
});
|
// });
|
||||||
|
|
||||||
for (const deletedPage of deletedPages) {
|
// for (const deletedPage of deletedPages) {
|
||||||
this.pageMetaCache.delete(deletedPage);
|
// this.pageMetaCache.delete(deletedPage);
|
||||||
this.emit("pageDeleted", deletedPage);
|
// this.emit("pageDeleted", deletedPage);
|
||||||
}
|
// }
|
||||||
|
|
||||||
this.emit("pageListUpdated", this.listPages());
|
// this.emit("pageListUpdated", this.listPages());
|
||||||
this.initialPageListLoad = false;
|
// this.initialPageListLoad = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async deletePage(name: string): Promise<void> {
|
async deletePage(name: string): Promise<void> {
|
||||||
await this.getPageMeta(name); // Check if page exists, if not throws Error
|
await this.getPageMeta(name); // Check if page exists, if not throws Error
|
||||||
await this.spacePrimitives.deleteFile(`${name}.md`);
|
await this.spacePrimitives.deleteFile(`${name}.md`);
|
||||||
|
|
||||||
this.pageMetaCache.delete(name);
|
|
||||||
this.emit("pageDeleted", name);
|
|
||||||
this.emit("pageListUpdated", [...this.pageMetaCache.values()]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPageMeta(name: string): Promise<PageMeta> {
|
async getPageMeta(name: string): Promise<PageMeta> {
|
||||||
const oldMeta = this.pageMetaCache.get(name);
|
return fileMetaToPageMeta(
|
||||||
const newMeta = fileMetaToPageMeta(
|
|
||||||
await this.spacePrimitives.getFileMeta(`${name}.md`),
|
await this.spacePrimitives.getFileMeta(`${name}.md`),
|
||||||
);
|
);
|
||||||
if (oldMeta) {
|
|
||||||
if (oldMeta.lastModified !== newMeta.lastModified) {
|
|
||||||
// Changed on disk, trigger event
|
|
||||||
this.emit("pageChanged", newMeta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.metaCacher(name, newMeta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listPages(): PageMeta[] {
|
listPages(): PageMeta[] {
|
||||||
return [...new Set(this.pageMetaCache.values())];
|
return this.cachedPageList;
|
||||||
}
|
}
|
||||||
|
|
||||||
async listPlugs(): Promise<string[]> {
|
async listPlugs(): Promise<string[]> {
|
||||||
@ -129,18 +124,9 @@ export class Space extends EventEmitter<SpaceEvents> {
|
|||||||
|
|
||||||
async readPage(name: string): Promise<{ text: string; meta: PageMeta }> {
|
async readPage(name: string): Promise<{ text: string; meta: PageMeta }> {
|
||||||
const pageData = await this.spacePrimitives.readFile(`${name}.md`);
|
const pageData = await this.spacePrimitives.readFile(`${name}.md`);
|
||||||
const previousMeta = this.pageMetaCache.get(name);
|
|
||||||
const newMeta = fileMetaToPageMeta(pageData.meta);
|
|
||||||
if (previousMeta) {
|
|
||||||
if (previousMeta.lastModified !== newMeta.lastModified) {
|
|
||||||
// Page changed since last cached metadata, trigger event
|
|
||||||
this.emit("pageChanged", newMeta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const meta = this.metaCacher(name, newMeta);
|
|
||||||
return {
|
return {
|
||||||
text: new TextDecoder().decode(pageData.data),
|
text: new TextDecoder().decode(pageData.data),
|
||||||
meta: meta,
|
meta: fileMetaToPageMeta(pageData.meta),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,37 +137,33 @@ export class Space extends EventEmitter<SpaceEvents> {
|
|||||||
): Promise<PageMeta> {
|
): Promise<PageMeta> {
|
||||||
try {
|
try {
|
||||||
this.saving = true;
|
this.saving = true;
|
||||||
const pageMeta = fileMetaToPageMeta(
|
return fileMetaToPageMeta(
|
||||||
await this.spacePrimitives.writeFile(
|
await this.spacePrimitives.writeFile(
|
||||||
`${name}.md`,
|
`${name}.md`,
|
||||||
new TextEncoder().encode(text),
|
new TextEncoder().encode(text),
|
||||||
selfUpdate,
|
selfUpdate,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
if (!selfUpdate) {
|
|
||||||
this.emit("pageChanged", pageMeta);
|
|
||||||
}
|
|
||||||
return this.metaCacher(name, pageMeta);
|
|
||||||
} finally {
|
} finally {
|
||||||
this.saving = false;
|
this.saving = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We're listing all pages that don't start with a _
|
// We're listing all pages that don't start with a _
|
||||||
isListedPageName(name: string): boolean {
|
isListedPage(fileMeta: FileMeta): boolean {
|
||||||
return name.endsWith(".md") && !name.startsWith("_");
|
return fileMeta.name.endsWith(".md") && !fileMeta.name.startsWith("_");
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchPageList(): Promise<PageMeta[]> {
|
async fetchPageList(): Promise<PageMeta[]> {
|
||||||
return (await this.spacePrimitives.fetchFileList())
|
return (await this.spacePrimitives.fetchFileList())
|
||||||
.filter((fileMeta) => this.isListedPageName(fileMeta.name))
|
.filter(this.isListedPage)
|
||||||
.map(fileMetaToPageMeta);
|
.map(fileMetaToPageMeta);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchAttachmentList(): Promise<AttachmentMeta[]> {
|
async fetchAttachmentList(): Promise<AttachmentMeta[]> {
|
||||||
return (await this.spacePrimitives.fetchFileList()).filter(
|
return (await this.spacePrimitives.fetchFileList()).filter(
|
||||||
(fileMeta) =>
|
(fileMeta) =>
|
||||||
!this.isListedPageName(fileMeta.name) &&
|
!this.isListedPage(fileMeta) &&
|
||||||
!fileMeta.name.endsWith(".plug.js"),
|
!fileMeta.name.endsWith(".plug.js"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -225,13 +207,6 @@ export class Space extends EventEmitter<SpaceEvents> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (const pageName of this.watchedPages) {
|
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);
|
await this.getPageMeta(pageName);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -252,17 +227,9 @@ export class Space extends EventEmitter<SpaceEvents> {
|
|||||||
unwatchPage(pageName: string) {
|
unwatchPage(pageName: string) {
|
||||||
this.watchedPages.delete(pageName);
|
this.watchedPages.delete(pageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
return meta;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
|
export function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
|
||||||
return {
|
return {
|
||||||
...fileMeta,
|
...fileMeta,
|
||||||
name: fileMeta.name.substring(0, fileMeta.name.length - 3),
|
name: fileMeta.name.substring(0, fileMeta.name.length - 3),
|
||||||
|
Loading…
Reference in New Issue
Block a user