1
0

Plugins -> Plugs

This commit is contained in:
Zef Hemel 2022-03-04 12:17:44 +01:00
parent e94d4d3253
commit fd72374c7d
5 changed files with 46 additions and 46 deletions

View File

@ -11,9 +11,9 @@ export class FunctionWorker {
private initCallback: any;
private invokeResolve?: (result?: any) => void;
private invokeReject?: (reason?: any) => void;
private plugin: Plugin<any>;
private plug: Plug<any>;
constructor(plugin: Plugin<any>, pathPrefix: string, name: string) {
constructor(plug: Plug<any>, pathPrefix: string, name: string) {
let worker = window.Worker;
this.worker = new worker("/function_worker.js");
@ -29,7 +29,7 @@ export class FunctionWorker {
this.inited = new Promise((resolve) => {
this.initCallback = resolve;
});
this.plugin = plugin;
this.plug = plug;
}
async onmessage(evt: MessageEvent) {
@ -40,7 +40,7 @@ export class FunctionWorker {
this.initCallback();
break;
case "syscall":
let result = await this.plugin.system.syscall(data.name, data.args);
let result = await this.plug.system.syscall(data.name, data.args);
this.worker.postMessage({
type: "syscall-response",
@ -76,11 +76,11 @@ export class FunctionWorker {
}
}
export interface PluginLoader<HookT> {
export interface PlugLoader<HookT> {
load(name: string, manifest: Manifest<HookT>): Promise<void>;
}
export class Plugin<HookT> {
export class Plug<HookT> {
pathPrefix: string;
system: System<HookT>;
private runningFunctions: Map<string, FunctionWorker>;
@ -96,7 +96,7 @@ export class Plugin<HookT> {
async load(manifest: Manifest<HookT>) {
this.manifest = manifest;
await this.system.pluginLoader.load(this.name, manifest);
await this.system.plugLoader.load(this.name, manifest);
await this.dispatchEvent("load");
}
@ -136,15 +136,15 @@ export class Plugin<HookT> {
}
export class System<HookT> {
protected plugins: Map<string, Plugin<HookT>>;
protected plugs: Map<string, Plug<HookT>>;
protected pathPrefix: string;
registeredSyscalls: SysCallMapping;
pluginLoader: PluginLoader<HookT>;
plugLoader: PlugLoader<HookT>;
constructor(PluginLoader: PluginLoader<HookT>, pathPrefix: string) {
this.pluginLoader = PluginLoader;
constructor(plugLoader: PlugLoader<HookT>, pathPrefix: string) {
this.plugLoader = plugLoader;
this.pathPrefix = pathPrefix;
this.plugins = new Map<string, Plugin<HookT>>();
this.plugs = new Map<string, Plug<HookT>>();
this.registeredSyscalls = {};
}
@ -167,16 +167,16 @@ export class System<HookT> {
return Promise.resolve(callback(...args));
}
async load(name: string, manifest: Manifest<HookT>): Promise<Plugin<HookT>> {
const plugin = new Plugin(this, this.pathPrefix, name);
await plugin.load(manifest);
this.plugins.set(name, plugin);
return plugin;
async load(name: string, manifest: Manifest<HookT>): Promise<Plug<HookT>> {
const plug = new Plug(this, this.pathPrefix, name);
await plug.load(manifest);
this.plugs.set(name, plug);
return plug;
}
async stop(): Promise<void[]> {
return Promise.all(
Array.from(this.plugins.values()).map((plugin) => plugin.stop())
Array.from(this.plugs.values()).map((plug) => plug.stop())
);
}
}

View File

@ -38,7 +38,7 @@ import { markdown } from "./markdown";
import { IPageNavigator, PathPageNavigator } from "./navigator";
import customMarkDown from "./parser";
import { BrowserSystem } from "./plugbox_browser/browser_system";
import { Plugin } from "../../plugbox/src/runtime";
import { Plug } from "../../plugbox/src/runtime";
import { slashCommandRegexp } from "./types";
import reducer from "./reducer";
@ -80,7 +80,7 @@ export class Editor implements AppEventDispatcher {
openPages: Map<string, PageState>;
space: Space;
editorCommands: Map<string, AppCommand>;
plugins: Plugin<NuggetHook>[];
plugs: Plug<NuggetHook>[];
indexer: Indexer;
navigationResolve?: (val: undefined) => void;
pageNavigator: IPageNavigator;
@ -88,7 +88,7 @@ export class Editor implements AppEventDispatcher {
constructor(space: Space, parent: Element) {
this.editorCommands = new Map();
this.openPages = new Map();
this.plugins = [];
this.plugs = [];
this.space = space;
this.viewState = initialViewState;
this.viewDispatch = () => {};
@ -104,7 +104,7 @@ export class Editor implements AppEventDispatcher {
async init() {
await this.loadPageList();
await this.loadPlugins();
await this.loadPlugs();
this.focus();
this.pageNavigator.subscribe(async (pageName) => {
@ -123,8 +123,8 @@ export class Editor implements AppEventDispatcher {
}
}
async loadPlugins() {
const system = new BrowserSystem<NuggetHook>("/plugin");
async loadPlugs() {
const system = new BrowserSystem<NuggetHook>("/plug");
system.registerSyscalls(
dbSyscalls,
editorSyscalls(this),
@ -133,12 +133,12 @@ export class Editor implements AppEventDispatcher {
);
await system.bootServiceWorker();
console.log("Now loading core plugin");
let mainPlugin = await system.load("core", coreManifest);
this.plugins.push(mainPlugin);
console.log("Now loading core plug");
let mainPlug = await system.load("core", coreManifest);
this.plugs.push(mainPlug);
this.editorCommands = new Map<string, AppCommand>();
for (let plugin of this.plugins) {
this.buildCommands(plugin);
for (let plug of this.plugs) {
this.buildCommands(plug);
}
this.viewDispatch({
type: "update-commands",
@ -146,14 +146,14 @@ export class Editor implements AppEventDispatcher {
});
}
private buildCommands(plugin: Plugin<NuggetHook>) {
const cmds = plugin.manifest!.hooks.commands;
private buildCommands(plug: Plug<NuggetHook>) {
const cmds = plug.manifest!.hooks.commands;
for (let name in cmds) {
let cmd = cmds[name];
this.editorCommands.set(name, {
command: cmd,
run: async (arg): Promise<any> => {
return await plugin.invoke(cmd.invoke, [arg]);
return await plug.invoke(cmd.invoke, [arg]);
},
});
}
@ -162,10 +162,10 @@ export class Editor implements AppEventDispatcher {
// TODO: Parallelize?
async dispatchAppEvent(name: AppEvent, data?: any): Promise<any[]> {
let results: any[] = [];
for (let plugin of this.plugins) {
let pluginResults = await plugin.dispatchEvent(name, data);
if (pluginResults) {
for (let result of pluginResults) {
for (let plug of this.plugs) {
let plugResults = await plug.dispatchEvent(name, data);
if (plugResults) {
for (let result of plugResults) {
results.push(result);
}
}
@ -209,7 +209,7 @@ export class Editor implements AppEventDispatcher {
closeBrackets(),
autocompletion({
override: [
this.pluginCompleter.bind(this),
this.plugCompleter.bind(this),
this.commandCompleter.bind(this),
],
}),
@ -304,7 +304,7 @@ export class Editor implements AppEventDispatcher {
});
}
async pluginCompleter(
async plugCompleter(
ctx: CompletionContext
): Promise<CompletionResult | null> {
let allCompletionResults = await this.dispatchAppEvent("editor:complete");

View File

@ -1,8 +1,8 @@
import { PluginLoader, System } from "../../../plugbox/src/runtime";
import { PlugLoader, System } from "../../../plugbox/src/runtime";
import { Manifest } from "../../../plugbox/src/types";
import { sleep } from "../util";
export class BrowserLoader<HookT> implements PluginLoader<HookT> {
export class BrowserLoader<HookT> implements PlugLoader<HookT> {
readonly pathPrefix: string;
constructor(pathPrefix: string) {

View File

@ -2,7 +2,7 @@ import { Manifest } from "./types";
import { openDB } from "idb";
const rootUrl = location.origin + "/plugin";
const rootUrl = location.origin + "/plug";
// Storing manifests in IndexedDB, y'all
const db = openDB("manifests-store", undefined, {
@ -64,13 +64,13 @@ self.addEventListener("fetch", (event: any) => {
return await handlePut(req, path);
}
let [pluginName, resourceType, functionName] = path.split("/");
let [plugName, resourceType, functionName] = path.split("/");
let manifest = await getManifest(pluginName);
let manifest = await getManifest(plugName);
if (!manifest) {
// console.log("Ain't got", pluginName);
return new Response(`Plugin not loaded: ${pluginName}`, {
// console.log("Ain't got", plugName);
return new Response(`Plug not loaded: ${plugName}`, {
status: 404,
});
}

View File

@ -6,7 +6,7 @@ window.addEventListener("message", async (event) => {
let data = messageEvent.data;
if (data.type === "iframe_event") {
// @ts-ignore
window.mainPlugin.dispatchEvent(data.data.event, data.data.data);
window.mainPlug.dispatchEvent(data.data.event, data.data.data);
}
});