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

79 lines
1.7 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-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 16:46:51 +02:00
execute: ({ value }) => { return [0, 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"],
execute: ({ op_type, a, b }: { 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
}
},
{
id: "output",
inputs: {
"input": { type: "float" },
},
outputs: [],
}
]
export class RemoteNodeRegistry implements NodeRegistry {
private nodes: Map<string, NodeType> = new Map();
constructor(private url: string) { }
async load(nodeIds: string[]) {
for (const id of nodeIds) {
const response = await fetch(`${this.url}/nodes/${id}`);
const node = this.getNode(id);
if (node) {
this.nodes.set(id, node);
}
}
}
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];
}
}