More event refactoring work

This commit is contained in:
Zef Hemel
2023-08-27 14:13:18 +02:00
parent 593597454a
commit c3d384330d
13 changed files with 140 additions and 149 deletions
+2 -13
View File
@@ -321,12 +321,6 @@ export class Client {
}
})().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 {
@@ -375,20 +369,15 @@ export class Client {
localSpacePrimitives = new EventedSpacePrimitives(
this.plugSpaceRemotePrimitives,
this.eventHook,
[
"file:changed",
"file:listed",
"page:deleted",
],
);
}
this.space = new Space(localSpacePrimitives, this.kvStore, this.eventHook);
this.eventHook.addLocalListener("file:changed", (fileMeta: FileMeta) => {
this.eventHook.addLocalListener("file:changed", (path: string) => {
// Only reload when watching the current page (to avoid reloading when switching pages)
if (
this.space.watchInterval && `${this.currentPage}.md` === fileMeta.name
this.space.watchInterval && `${this.currentPage}.md` === path
) {
console.log("Page changed elsewhere, reloading");
this.flashNotification("Page changed elsewhere, reloading");
+5 -2
View File
@@ -37,7 +37,6 @@ import { indexProxySyscalls } from "./syscalls/index.proxy.ts";
import { storeProxySyscalls } from "./syscalls/store.proxy.ts";
export class ClientSystem {
system: System<SilverBulletHooks> = new System("client");
commandHook: CommandHook;
slashCommandHook: SlashCommandHook;
namespaceHook: PlugNamespaceHook;
@@ -45,15 +44,19 @@ export class ClientSystem {
codeWidgetHook: CodeWidgetHook;
plugsUpdated = false;
mdExtensions: MDExt[] = [];
system: System<SilverBulletHooks>;
constructor(
private client: Client,
private kvStore: DexieKVStore,
private mq: DexieMQ,
private dbPrefix: string,
dbPrefix: string,
private eventHook: EventHook,
private thinClientMode: boolean,
) {
// Only set environment to "client" when running in thin client mode, otherwise we run everything locally (hybrid)
this.system = new System(thinClientMode ? "client" : undefined);
this.system.addHook(this.eventHook);
// Plug page namespace hook
-30
View File
@@ -65,36 +65,6 @@ export class Space {
public async updatePageList() {
// This will trigger appropriate events automatically
await this.fetchPageList();
// const deletedPages = new Set<string>(this.pageMetaCache.keys());
// newPageList.forEach((meta) => {
// const pageName = meta.name;
// const oldPageMeta = this.pageMetaCache.get(pageName);
// const newPageMeta: PageMeta = { ...meta };
// if (
// !oldPageMeta &&
// (pageName.startsWith(plugPrefix) || !this.initialPageListLoad)
// ) {
// this.emit("pageCreated", newPageMeta);
// } else if (
// oldPageMeta &&
// oldPageMeta.lastModified !== newPageMeta.lastModified
// ) {
// this.emit("pageChanged", newPageMeta);
// }
// // Page found, not deleted
// deletedPages.delete(pageName);
// // Update in cache
// this.pageMetaCache.set(pageName, newPageMeta);
// });
// for (const deletedPage of deletedPages) {
// this.pageMetaCache.delete(deletedPage);
// this.emit("pageDeleted", deletedPage);
// }
// this.emit("pageListUpdated", this.listPages());
// this.initialPageListLoad = false;
}
async deletePage(name: string): Promise<void> {
+20 -1
View File
@@ -2,17 +2,27 @@ import type { Plug } from "../../plugos/plug.ts";
import { SysCallMapping, System } from "../../plugos/system.ts";
import type { Client } from "../client.ts";
import { CommandDef } from "../hooks/command.ts";
import { proxySyscall } from "./util.ts";
export function systemSyscalls(
editor: Client,
system: System<any>,
): SysCallMapping {
return {
const api: SysCallMapping = {
"system.invokeFunction": (
ctx,
_env: string,
name: string,
...args: any[]
) => {
// For backwards compatibility
// TODO: Remove at some point
return api["system.invoke"](ctx, name, ...args);
},
"system.invoke": (
ctx,
name: string,
...args: any[]
) => {
if (!ctx.plug) {
throw Error("No plug associated with context");
@@ -28,6 +38,14 @@ export function systemSyscalls(
}
name = functionName;
}
const functionDef = plug.manifest!.functions[name];
if (!functionDef) {
throw Error(`Function ${name} not found`);
}
if (functionDef.env && system.env && functionDef.env !== system.env) {
// Proxy to another environment
return proxySyscall(editor.remoteSpacePrimitives, name, args);
}
return plug.invoke(name, args);
},
"system.invokeCommand": (_ctx, name: string) => {
@@ -47,4 +65,5 @@ export function systemSyscalls(
return system.env;
},
};
return api;
}
+28 -22
View File
@@ -1,3 +1,4 @@
import { HttpSpacePrimitives } from "../../common/spaces/http_space_primitives.ts";
import { SysCallMapping } from "../../plugos/system.ts";
import { SyscallResponse } from "../../server/rpc.ts";
import { Client } from "../client.ts";
@@ -5,29 +6,34 @@ import { Client } from "../client.ts";
export function proxySyscalls(client: Client, names: string[]): SysCallMapping {
const syscalls: SysCallMapping = {};
for (const name of names) {
syscalls[name] = async (_ctx, ...args: any[]) => {
if (!client.remoteSpacePrimitives) {
throw new Error("Not supported");
}
const resp = await client.remoteSpacePrimitives.authenticatedFetch(
`${client.remoteSpacePrimitives.url}/.rpc`,
{
method: "POST",
body: JSON.stringify({
operation: "syscall",
name,
args,
}),
},
);
const result: SyscallResponse = await resp.json();
if (result.error) {
console.error("Remote syscall error", result.error);
throw new Error(result.error);
} else {
return result.result;
}
syscalls[name] = (_ctx, ...args: any[]) => {
return proxySyscall(client.remoteSpacePrimitives, name, args);
};
}
return syscalls;
}
export async function proxySyscall(
httpSpacePrimitives: HttpSpacePrimitives,
name: string,
args: any[],
): Promise<any> {
const resp = await httpSpacePrimitives.authenticatedFetch(
`${httpSpacePrimitives.url}/.rpc`,
{
method: "POST",
body: JSON.stringify({
operation: "syscall",
name,
args,
}),
},
);
const result: SyscallResponse = await resp.json();
if (result.error) {
console.error("Remote syscall error", result.error);
throw new Error(result.error);
} else {
return result.result;
}
}