feat: first working version with parameters
This commit is contained in:
@@ -19,7 +19,7 @@ const nodeTypes: NodeType[] = [
|
||||
"b": { type: "float" },
|
||||
},
|
||||
outputs: ["float"],
|
||||
execute: (op_type: number, a: number, b: number) => {
|
||||
execute: ([op_type, a, b]: number[]) => {
|
||||
switch (op_type) {
|
||||
case 0: return a + b;
|
||||
case 1: return a - b;
|
||||
@@ -47,39 +47,49 @@ export class RemoteNodeRegistry implements NodeRegistry {
|
||||
|
||||
constructor(private url: string) { }
|
||||
|
||||
private async loadNode(id: string) {
|
||||
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) {
|
||||
this.status = "error";
|
||||
throw new Error(`Failed to load node ${id}`);
|
||||
}
|
||||
|
||||
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)}#${id}`);
|
||||
|
||||
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) {
|
||||
this.status = "error";
|
||||
throw new Error(`Failed to load node ${id}`);
|
||||
} else {
|
||||
log.log("loaded node", id);
|
||||
}
|
||||
const node = await response.json();
|
||||
node.execute = wasmWrapper.execute;
|
||||
return node;
|
||||
}
|
||||
|
||||
async load(nodeIds: string[]) {
|
||||
const a = performance.now();
|
||||
|
||||
nodeIds.push("max/plantarium/random");
|
||||
nodeIds.push("max/plantarium/float");
|
||||
nodeIds.push("max/plantarium/output");
|
||||
nodeIds.push("max/plantarium/array");
|
||||
nodeIds.push("max/plantarium/sum");
|
||||
|
||||
for (const id of nodeIds) {
|
||||
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) {
|
||||
this.status = "error";
|
||||
throw new Error(`Failed to load node ${id}`);
|
||||
}
|
||||
const nodes = await Promise.all(nodeIds.map(id => this.loadNode(id)));
|
||||
|
||||
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) {
|
||||
this.status = "error";
|
||||
throw new Error(`Failed to load node ${id}`);
|
||||
}
|
||||
const node = await response.json();
|
||||
node.execute = wasmWrapper.execute;
|
||||
this.nodes.set(id, node);
|
||||
for (const node of nodes) {
|
||||
this.nodes.set(node.id, node);
|
||||
}
|
||||
|
||||
const duration = performance.now() - a;
|
||||
|
||||
Reference in New Issue
Block a user