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

111 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 18:03:23 +02:00
import { createLogger } from "./helpers";
2024-04-04 19:17:27 +02:00
const nodeTypes: NodeType[] = [
{
2024-04-05 19:38:10 +02:00
id: "max/plantarium/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: {
2024-04-10 14:27:23 +02:00
"op_type": { label: "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 {
2024-04-10 21:57:03 +02:00
status: "loading" | "ready" | "error" = "loading";
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();
2024-04-05 19:38:10 +02:00
nodeIds.push("max/plantarium/random");
nodeIds.push("max/plantarium/float");
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) {
2024-04-10 21:57:03 +02:00
this.status = "error";
2024-04-05 18:03:23 +02:00
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) {
2024-04-10 21:57:03 +02:00
this.status = "error";
2024-04-05 18:03:23 +02:00
throw new Error(`Failed to load node ${id}`);
}
const node = await response.json();
2024-04-05 19:38:10 +02:00
node.execute = wasmWrapper.execute;
2024-04-05 18:03:23 +02:00
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");
2024-04-10 21:57:03 +02:00
this.status = "ready";
}
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];
}
}