@selenite/graph-editor
Version:
A graph editor for visual programming, based on rete and svelte.
96 lines (95 loc) • 3.83 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Node, path, registerNode, tags } from '../Node.svelte';
import { getLeavesFromOutput } from '../utils';
import { DynamicTypeComponent } from '../components/DynamicTypeComponent.svelte';
// Class defining a For Each Node
let ForEachNode = class ForEachNode extends Node {
currentItemIndex = -1;
// state = { ...this.state, type: 'any' };
numConnections = 0;
constructor(params = {}) {
super({ ...params, label: 'For Each' });
const initialType = 'number';
this.pythonComponent.addVariables('item', 'index');
this.pythonComponent.setCodeTemplateGetter(() => {
return `
for $(index), $(item) in enumerate($(array)):
{{loop}}?
{{exec}}
`;
});
this.addInExec();
this.addOutExec('loop', 'Loop');
this.addOutExec('exec', 'Done');
let dynamicTypeCmpnt;
this.addInData('array', {
control: {
canChangeType: true
},
datastructure: 'array',
type: initialType,
changeType: (type) => {
if (type === 'exec') {
throw new Error('Cannot use exec as type');
}
if (!dynamicTypeCmpnt)
return;
dynamicTypeCmpnt.changeType(type);
if (this.inputs.array?.socket)
this.inputs.array.socket.type = type;
if (this.outputs.item?.socket)
this.outputs.item.socket.type = type;
}
});
this.addOutData('item', {
type: initialType
});
this.addOutData('index', {
type: 'number'
});
dynamicTypeCmpnt = this.addComponentByClass(DynamicTypeComponent, {
inputs: ['array'],
outputs: ['item'],
initial: initialType
});
}
// Executes the node
async execute(input, forward, forwardExec) {
const array = await this.getDataWithInputs('array');
for (let i = 0; i < array.length; i++) {
this.needsProcessing = true;
this.currentItemIndex = i;
this.getDataflowEngine().reset(this.id);
this.processDataflow();
const leavesFromLoopExec = getLeavesFromOutput(this, 'loop');
const promises = this.getWaitPromises(leavesFromLoopExec);
forward('loop');
await Promise.all(promises);
this.needsProcessing = false;
}
super.execute(input, forward);
}
data(inputs) {
const array = this.getData('array', inputs);
if (this.currentItemIndex === undefined || this.currentItemIndex === -1) {
return { item: array?.at(0), index: 0 };
}
const item = array[this.currentItemIndex];
return { item, index: this.currentItemIndex };
}
};
ForEachNode = __decorate([
registerNode('control.ForEach'),
path('Array'),
tags('loop', 'iteration'),
__metadata("design:paramtypes", [Object])
], ForEachNode);
export { ForEachNode };