nodes/app/src/lib/node-registry.ts

108 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-04-04 19:17:27 +02:00
import type { NodeRegistry, NodeType } from "@nodes/types";
2024-04-05 16:46:51 +02:00
import * as d from "plantarium-nodes-math";
2024-04-05 18:03:23 +02:00
import { createLogger } from "./helpers";
2024-04-04 19:17:27 +02:00
const nodeTypes: NodeType[] = [
{
id: "max/plantarium/input-float",
2024-04-04 19:17:27 +02:00
inputs: {
"value": { type: "float", value: 0.1, internal: true },
},
outputs: ["float"],
2024-04-05 18:03:23 +02:00
execute: (value) => { return value; }
2024-04-04 19:17:27 +02:00
},
{
id: "max/plantarium/math",
inputs: {
"op_type": { title: "type", type: "select", labels: ["add", "subtract", "multiply", "divide"], value: 0 },
"a": { type: "float" },
"b": { type: "float" },
},
outputs: ["float"],
2024-04-05 18:03:23 +02:00
execute: (op_type: number, a: number, b: number) => {
2024-04-05 16:46:51 +02:00
switch (op_type) {
case 0: return a + b;
case 1: return a - b;
case 2: return a * b;
case 3: return a / b;
}
2024-04-04 19:17:27 +02:00
}
},
{
2024-04-05 18:03:23 +02:00
id: "max/plantarium/output",
2024-04-04 19:17:27 +02:00
inputs: {
"input": { type: "float" },
},
outputs: [],
}
]
2024-04-05 18:03:23 +02:00
const log = createLogger("node-registry");
export class RemoteNodeRegistry implements NodeRegistry {
private nodes: Map<string, NodeType> = new Map();
constructor(private url: string) { }
async load(nodeIds: string[]) {
2024-04-05 18:03:23 +02:00
const a = performance.now();
for (const id of nodeIds) {
2024-04-05 18:03:23 +02:00
const nodeUrl = `${this.url}/n/${id}`;
const response = await fetch(nodeUrl);
const wasmResponse = await fetch(`${nodeUrl}/wasm`);
const wrapperReponse = await fetch(`${nodeUrl}/wrapper`);
if (!wrapperReponse.ok) {
throw new Error(`Failed to load node ${id}`);
}
2024-04-05 18:03:23 +02:00
let wrapperCode = await wrapperReponse.text();
wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return;
wasm = val;`);
const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}`);
const module = new WebAssembly.Module(await wasmResponse.arrayBuffer());
const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wasmWrapper });
wasmWrapper.__wbg_set_wasm(instance.exports);
if (!response.ok) {
throw new Error(`Failed to load node ${id}`);
}
const node = await response.json();
node.execute = (...args) => {
console.log("Executing", id, args);
return wasmWrapper.execute(...args)
};
this.nodes.set(id, node);
}
2024-04-05 18:03:23 +02:00
const duration = performance.now() - a;
log.log("loaded nodes in", duration, "ms");
}
getNode(id: string) {
return this.nodes.get(id);
}
getAllNodes() {
return [...this.nodes.values()];
}
}
2024-04-04 19:17:27 +02:00
export class MemoryNodeRegistry implements NodeRegistry {
async load(nodeIds: string[]) {
// Do nothing
}
getNode(id: string) {
2024-04-04 19:17:27 +02:00
return nodeTypes.find((nodeType) => nodeType.id === id);
}
getAllNodes() {
2024-04-04 19:17:27 +02:00
return [...nodeTypes];
}
}