PlugOS refactor and other tweaks (#631)

* Prep for in-process plug loading (e.g. for CF workers, Deno Deploy)
* Prototype of fixed in-process loading plugs
* Fix: buttons not to scroll with content
* Better positioning of modal especially on mobile
* Move query caching outside query
* Fix annoying mouse behavior when filter box appears
* Page navigator search tweaks
This commit is contained in:
Zef Hemel
2024-01-15 16:43:12 +01:00
committed by GitHub
parent a9eb252658
commit a2dbf7b3db
65 changed files with 591 additions and 617 deletions
+6 -6
View File
@@ -7,14 +7,14 @@ export function clientStoreSyscalls(
prefix: KvKey = ["client"],
): SysCallMapping {
return {
"clientStore.get": (ctx, key: string): Promise<any> => {
return ds.get([...prefix, ctx.plug!.name!, key]);
"clientStore.get": (_ctx, key: string): Promise<any> => {
return ds.get([...prefix, key]);
},
"clientStore.set": (ctx, key: string, val: any): Promise<void> => {
return ds.set([...prefix, ctx.plug!.name!, key], val);
"clientStore.set": (_ctx, key: string, val: any): Promise<void> => {
return ds.set([...prefix, key], val);
},
"clientStore.delete": (ctx, key: string): Promise<void> => {
return ds.delete([...prefix, ctx.plug!.name!, key]);
"clientStore.delete": (_ctx, key: string): Promise<void> => {
return ds.delete([...prefix, key]);
},
};
}
+3 -34
View File
@@ -1,48 +1,17 @@
import { KvQuery } from "$sb/types.ts";
import { LimitedMap } from "../../common/limited_map.ts";
import { LimitedMap } from "../../plug-api/lib/limited_map.ts";
import type { SysCallMapping } from "../../plugos/system.ts";
import type { Client } from "../client.ts";
import { proxySyscall, proxySyscalls } from "./util.ts";
export function dataStoreProxySyscalls(client: Client): SysCallMapping {
const syscalls = proxySyscalls(client, [
return proxySyscalls(client, [
"datastore.delete",
"datastore.set",
"datastore.batchSet",
"datastore.batchDelete",
"datastore.batchGet",
"datastore.query",
"datastore.get",
]);
// Add a cache for datastore.query
const queryCache = new LimitedMap<any>(5);
syscalls["datastore.query"] = async (ctx, query: KvQuery) => {
let cacheKey: string | undefined;
const cacheSecs = query.cacheSecs;
// Should we do caching?
if (cacheSecs) {
// Remove the cacheSecs from the query
query = { ...query, cacheSecs: undefined };
cacheKey = JSON.stringify(query);
const cachedResult = queryCache.get(cacheKey);
if (cachedResult) {
// Let's use the cached result
return cachedResult;
}
}
const result = await proxySyscall(
ctx,
client.httpSpacePrimitives,
"datastore.query",
[
query,
],
);
if (cacheKey) {
// Store in the cache
queryCache.set(cacheKey, result, cacheSecs! * 1000);
}
return result;
};
return syscalls;
}
+1 -4
View File
@@ -220,10 +220,7 @@ export function editorSyscalls(editor: Client): SysCallMapping {
): Promise<string | undefined> => {
return editor.prompt(message, defaultValue);
},
"editor.confirm": (
_ctx,
message: string,
): Promise<boolean> => {
"editor.confirm": (_ctx, message: string): Promise<boolean> => {
return editor.confirm(message);
},
"editor.getUiOption": (_ctx, key: string): any => {
+2 -8
View File
@@ -7,10 +7,7 @@ export function spaceSyscalls(editor: Client): SysCallMapping {
"space.listPages": (): Promise<PageMeta[]> => {
return editor.space.fetchPageList();
},
"space.readPage": async (
_ctx,
name: string,
): Promise<string> => {
"space.readPage": async (_ctx, name: string): Promise<string> => {
return (await editor.space.readPage(name)).text;
},
"space.getPageMeta": (_ctx, name: string): Promise<PageMeta> => {
@@ -39,10 +36,7 @@ export function spaceSyscalls(editor: Client): SysCallMapping {
"space.listAttachments": async (): Promise<AttachmentMeta[]> => {
return await editor.space.fetchAttachmentList();
},
"space.readAttachment": async (
_ctx,
name: string,
): Promise<Uint8Array> => {
"space.readAttachment": async (_ctx, name: string): Promise<Uint8Array> => {
return (await editor.space.readAttachment(name)).data;
},
"space.getAttachmentMeta": async (
+10 -21
View File
@@ -1,4 +1,3 @@
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";
@@ -11,30 +10,20 @@ export function systemSyscalls(
const api: SysCallMapping = {
"system.invokeFunction": (
ctx,
name: string,
fullName: string, // plug.function
...args: any[]
) => {
if (name === "server" || name === "client") {
// Backwards compatibility mode (previously there was an 'env' argument)
name = args[0];
args = args.slice(1);
const [plugName, functionName] = fullName.split(".");
if (!plugName || !functionName) {
throw Error(`Invalid function name ${fullName}`);
}
let plug: Plug<any> | undefined = ctx.plug;
const fullName = name;
// console.log("Invoking function", fullName, "on plug", plug);
if (name.includes(".")) {
// plug name in the name
const [plugName, functionName] = name.split(".");
plug = system.loadedPlugs.get(plugName);
if (!plug) {
throw Error(`Plug ${plugName} not found`);
}
name = functionName;
const plug = system.loadedPlugs.get(plugName);
if (!plug) {
throw Error(`Plug ${plugName} not found`);
}
const functionDef = plug?.manifest!.functions[name];
const functionDef = plug.manifest!.functions[functionName];
if (!functionDef) {
throw Error(`Function ${name} not found`);
throw Error(`Function ${functionName} not found`);
}
if (
client && functionDef.env && system.env &&
@@ -48,7 +37,7 @@ export function systemSyscalls(
[fullName, ...args],
);
}
return plug.invoke(name, args);
return plug.invoke(functionName, args);
},
"system.invokeCommand": (_ctx, name: string, args?: string[]) => {
if (!client) {
+4 -1
View File
@@ -18,8 +18,11 @@ export async function proxySyscall(
name: string,
args: any[],
): Promise<any> {
if (!ctx.plug) {
throw new Error(`Cannot proxy ${name} syscall without plug context`);
}
const resp = await httpSpacePrimitives.authenticatedFetch(
`${httpSpacePrimitives.url}/.rpc/${ctx.plug.name}/${name}`,
`${httpSpacePrimitives.url}/.rpc/${ctx.plug}/${name}`,
{
method: "POST",
body: JSON.stringify(args),