retejs / rete

JavaScript framework for visual programming
https://retejs.org
MIT License
10k stars 651 forks source link

Angular: send data from worker to html #295

Closed mGhassen closed 5 years ago

mGhassen commented 5 years ago

Hello,

Thank you for this super project, i tried to start playing with the code with the angular one.

I'm using the basic example, and i wanted to send data of "SUM" from the worker to the angular component then to the new input in the html.

How can i do it. thank you very much

Ni55aN commented 5 years ago

It depends on the relationship of the data to your components.

  1. Only one/few components require this ability:
class SumComponent extends Rete.Component {
    constructor(onChange){
        super("Number");
        this.onChange = onChange;
    }

    worker(node, inputs, outputs) {
        const sum = ...
        this.onChange(sum);
    }
}

const components = [new SumComponent(() => angularComponent.changeValue(...
  1. Use a general purpose event:
engine.bind('workerdone');
engine.on('workerdone', ({ component, sum }) => ...);
//...
    worker(node, inputs, outputs) {
        const sum = ...
        this.engine.trigger('workerdone', { component: this, sum });
    }
  1. Handler at the process time:
engine.process(data, null, { onSum() { ... } })

// ...
    worker(node, inputs, outputs, { onSum }) {
        const sum = ...
        onSum(sum);
    }