@selenite/graph-editor
Version:
A graph editor for visual programming, based on rete and svelte.
92 lines (88 loc) • 2.92 kB
JavaScript
import { Node, registerNode } from '../Node.svelte';
export class EveryNode extends Node {
current = 0;
// state: { current: number } = { current: 0 };
constructor({ count = 100, factory }) {
super({ label: 'Every', factory, height: 270, width: 200, params: { count } });
this.addInExec();
this.addInExec('reset', 'Reset');
this.addOutExec();
this.oldAddInData({
name: 'count',
displayName: 'Count',
socketLabel: 'Count',
type: 'number',
control: {
type: 'number',
options: {
initial: count,
label: 'Count',
change: (value) => {
console.log('yo');
console.log(value);
this.getDataflowEngine().reset(this.id);
this.factory.pythonDataflowEngine.reset(this.id);
}
}
}
});
this.oldAddOutData({
name: 'current',
displayName: 'Current',
socketLabel: 'Current',
type: 'number'
});
// TODO: change init into getter
this.pythonComponent.addInitCode(`$(every${this.getData('count')}) = Every(${this.getData('count')})`);
this.pythonComponent.addDynamicOutput('current');
this.pythonComponent.setDataCodeGetter('current', () => `$(every${this.getData('count')}).current`);
// TODO : dynamic variable
this.pythonComponent.addVariable(`every${this.getData('count')}`);
this.pythonComponent.setCodeTemplateGetter(() => {
return `
if $(every${this.getData('count')})():
{{exec}}?
`;
});
this.pythonComponent.setCodeTemplateGetter(() => {
return `$(every${this.getData('count')}).reset()`;
}, 'reset');
this.pythonComponent.addClass(`
class Every:
def __init__(self, count):
self.count = count
self.current = 0
def __call__(self):
self.current += 1
return (self.current - 1) % self.count == 0
def reset(self):
self.current = 0
`);
// this.addInExec('reset', 'Reset');
}
isFlowing() {
const count = this.getData('count');
if (count === undefined)
return true;
return this.current % count === 0;
}
data(inputs) {
return { current: this.current };
}
async execute(input, forward, forwardExec) {
if (input === 'exec') {
if (this.isFlowing()) {
forward('exec');
}
this.current++;
}
if (input === 'reset') {
this.current = 0;
this.getDataflowEngine().reset(this.id);
}
super.execute(input, forward, false);
}
getNaturalFlow() {
return this.isFlowing() ? 'exec' : undefined;
}
}