Plugbox cleanup
This commit is contained in:
@@ -6,7 +6,7 @@ import yargs from "yargs";
|
||||
import { hideBin } from "yargs/helpers";
|
||||
import { Manifest } from "../../webapp/src/plugins/types";
|
||||
|
||||
async function compile(filePath: string, sourceMap: string) {
|
||||
async function compile(filePath: string, sourceMap: boolean) {
|
||||
let tempFile = "out.js";
|
||||
let js = await esbuild.build({
|
||||
entryPoints: [filePath],
|
||||
@@ -25,7 +25,7 @@ async function compile(filePath: string, sourceMap: string) {
|
||||
return jsCode;
|
||||
}
|
||||
|
||||
async function bundle(manifestPath, sourceMaps) {
|
||||
async function bundle(manifestPath: string, sourceMaps: boolean) {
|
||||
const rootPath = path.dirname(manifestPath);
|
||||
const manifest = JSON.parse(
|
||||
(await readFile(manifestPath)).toString()
|
||||
@@ -53,7 +53,7 @@ async function run() {
|
||||
})
|
||||
.parse();
|
||||
|
||||
let generatedManifest = await bundle(args._[0], !!args.debug);
|
||||
let generatedManifest = await bundle(args._[0] as string, !!args.debug);
|
||||
writeFile(args._[1] as string, JSON.stringify(generatedManifest, null, 2));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
import { Manifest } from "./types";
|
||||
|
||||
interface SysCallMapping {
|
||||
// TODO: Better typing
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export class FunctionWorker {
|
||||
private worker: Worker;
|
||||
private inited: Promise<any>;
|
||||
private initCallback: any;
|
||||
private invokeResolve?: (result?: any) => void;
|
||||
private invokeReject?: (reason?: any) => void;
|
||||
private plugin: Plugin;
|
||||
|
||||
constructor(plugin: Plugin, pathPrefix: string, name: string) {
|
||||
let worker = window.Worker;
|
||||
this.worker = new worker("/function_worker.js");
|
||||
|
||||
// console.log("Starting worker", this.worker);
|
||||
this.worker.onmessage = this.onmessage.bind(this);
|
||||
this.worker.postMessage({
|
||||
type: "boot",
|
||||
prefix: pathPrefix,
|
||||
name: name,
|
||||
// @ts-ignore
|
||||
userAgent: navigator.userAgent,
|
||||
});
|
||||
this.inited = new Promise((resolve) => {
|
||||
this.initCallback = resolve;
|
||||
});
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
async onmessage(evt: MessageEvent) {
|
||||
let data = evt.data;
|
||||
if (!data) return;
|
||||
switch (data.type) {
|
||||
case "inited":
|
||||
this.initCallback();
|
||||
break;
|
||||
case "syscall":
|
||||
let result = await this.plugin.system.syscall(data.name, data.args);
|
||||
|
||||
this.worker.postMessage({
|
||||
type: "syscall-response",
|
||||
id: data.id,
|
||||
data: result,
|
||||
});
|
||||
break;
|
||||
case "result":
|
||||
this.invokeResolve!(data.result);
|
||||
break;
|
||||
case "error":
|
||||
this.invokeReject!(data.reason);
|
||||
break;
|
||||
default:
|
||||
console.error("Unknown message type", data);
|
||||
}
|
||||
}
|
||||
|
||||
async invoke(args: Array<any>): Promise<any> {
|
||||
await this.inited;
|
||||
this.worker.postMessage({
|
||||
type: "invoke",
|
||||
args: args,
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
this.invokeResolve = resolve;
|
||||
this.invokeReject = reject;
|
||||
});
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.worker.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
export interface PluginLoader {
|
||||
load(name: string, manifest: Manifest): Promise<void>;
|
||||
}
|
||||
|
||||
export class Plugin {
|
||||
pathPrefix: string;
|
||||
system: System;
|
||||
private runningFunctions: Map<string, FunctionWorker>;
|
||||
public manifest?: Manifest;
|
||||
private name: string;
|
||||
|
||||
constructor(system: System, pathPrefix: string, name: string) {
|
||||
this.name = name;
|
||||
this.pathPrefix = `${pathPrefix}/${name}`;
|
||||
this.system = system;
|
||||
this.runningFunctions = new Map<string, FunctionWorker>();
|
||||
}
|
||||
|
||||
async load(manifest: Manifest) {
|
||||
this.manifest = manifest;
|
||||
await this.system.pluginLoader.load(this.name, manifest);
|
||||
await this.dispatchEvent("load");
|
||||
}
|
||||
|
||||
async invoke(name: string, args: Array<any>): Promise<any> {
|
||||
if (!this.runningFunctions.has(name)) {
|
||||
this.runningFunctions.set(
|
||||
name,
|
||||
new FunctionWorker(this, this.pathPrefix, name)
|
||||
);
|
||||
}
|
||||
return await this.runningFunctions.get(name)!.invoke(args);
|
||||
}
|
||||
|
||||
async dispatchEvent(name: string, data?: any): Promise<any[]> {
|
||||
let functionsToSpawn = this.manifest!.events[name];
|
||||
if (functionsToSpawn) {
|
||||
return await Promise.all(
|
||||
functionsToSpawn.map(
|
||||
async (functionToSpawn: string) =>
|
||||
await this.invoke(functionToSpawn, [data])
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async stop() {
|
||||
for (const [functionname, worker] of Object.entries(
|
||||
this.runningFunctions
|
||||
)) {
|
||||
console.log(`Stopping ${functionname}`);
|
||||
worker.stop();
|
||||
}
|
||||
this.runningFunctions = new Map<string, FunctionWorker>();
|
||||
}
|
||||
}
|
||||
|
||||
export class System {
|
||||
protected plugins: Map<string, Plugin>;
|
||||
protected pathPrefix: string;
|
||||
registeredSyscalls: SysCallMapping;
|
||||
pluginLoader: PluginLoader;
|
||||
|
||||
constructor(PluginLoader: PluginLoader, pathPrefix: string) {
|
||||
this.pluginLoader = PluginLoader;
|
||||
this.pathPrefix = pathPrefix;
|
||||
this.plugins = new Map<string, Plugin>();
|
||||
this.registeredSyscalls = {};
|
||||
}
|
||||
|
||||
registerSyscalls(...registrationObjects: Array<SysCallMapping>) {
|
||||
for (const registrationObject of registrationObjects) {
|
||||
for (let p in registrationObject) {
|
||||
this.registeredSyscalls[p] = registrationObject[p];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async syscall(name: string, args: Array<any>): Promise<any> {
|
||||
const callback = this.registeredSyscalls[name];
|
||||
if (!name) {
|
||||
throw Error(`Unregistered syscall ${name}`);
|
||||
}
|
||||
if (!callback) {
|
||||
throw Error(`Registered but not implemented syscall ${name}`);
|
||||
}
|
||||
return Promise.resolve(callback(...args));
|
||||
}
|
||||
|
||||
async load(name: string, manifest: Manifest): Promise<Plugin> {
|
||||
const plugin = new Plugin(this, this.pathPrefix, name);
|
||||
await plugin.load(manifest);
|
||||
this.plugins.set(name, plugin);
|
||||
return plugin;
|
||||
}
|
||||
|
||||
async stop(): Promise<void[]> {
|
||||
return Promise.all(
|
||||
Array.from(this.plugins.values()).map((plugin) => plugin.stop())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Starting");
|
||||
@@ -0,0 +1,30 @@
|
||||
export interface Manifest {
|
||||
events: { [key: string]: string[] };
|
||||
commands: {
|
||||
[key: string]: CommandDef;
|
||||
};
|
||||
functions: {
|
||||
[key: string]: FunctionDef;
|
||||
};
|
||||
}
|
||||
|
||||
export const slashCommandRegexp = /\/[\w\-]*/;
|
||||
|
||||
export interface CommandDef {
|
||||
// Function name to invoke
|
||||
invoke: string;
|
||||
|
||||
// Bind to keyboard shortcut
|
||||
key?: string;
|
||||
mac?: string;
|
||||
|
||||
// If to show in slash invoked menu and if so, with what label
|
||||
// should match slashCommandRegexp
|
||||
slashCommand?: string;
|
||||
}
|
||||
|
||||
export interface FunctionDef {
|
||||
path: string;
|
||||
functionName?: string;
|
||||
code?: string;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
export function countWords(str: string): number {
|
||||
var matches = str.match(/[\w\d\'\'-]+/gi);
|
||||
return matches ? matches.length : 0;
|
||||
}
|
||||
|
||||
export function readingTime(wordCount: number): number {
|
||||
// 225 is average word reading speed for adults
|
||||
return Math.ceil(wordCount / 225);
|
||||
}
|
||||
|
||||
export function safeRun(fn: () => Promise<void>) {
|
||||
fn().catch((e) => {
|
||||
console.error(e);
|
||||
});
|
||||
}
|
||||
|
||||
export function sleep(ms: number): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, ms);
|
||||
});
|
||||
}
|
||||
|
||||
export function isMacLike() {
|
||||
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
|
||||
}
|
||||
Reference in New Issue
Block a user