question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Process node worker on input change

See original GitHub issue

Hi Guys,

First of all, ReteJS great project. But I need some help, I already did some research myself but can’t figure this one out.

I have a basic flow of 3 components:

  • OnSlide component: which triggers an action (event) when the user slides a GUI component, it also exposes a value between 0 and 100 which is the the sliders position.
  • ColorRamp component: it converts a value to an index of a color gradient. So for example i have a gradient running from green to red. An input value of 50 would result in a yellow color output (hex color).
  • SetColor component: Which takes an action, and color… and sets the background color of the UI component to the given color.
Schermafbeelding 2020-04-01 om 20 56 52

Everything is working except the color that the ColorRamp component gives is always the same. I noticed that the worker() function in the ColorRamp component is only called the first time.

What I want to accomplish is that the Color Ramp plugin always gives the correct color, so whenever the input “value” changes… it recalculates the correct color output.

The strange thing is that everything works flawless but only the first time onSlide triggers an event. In that scenario the worker() functions of all components are called. But after the first time… only the worker() function of the onSlide component gets called…

I’ve been debugging all night… can’t figure it out… it looks to me like the “thread” somewhere get’s lost in asyncToGenerator.

Thank you very much! Kind regards, Bastiaan

Components source code, in order of the signal flow:

OnSlide component (event generator):

import Rete from "rete";
import Sockets from "../sockets";

class OnSlideComponent extends Rete.Component {
    constructor(item){
      super('On Slide');
      this.task = {
        outputs: {act: 'option', currentValue: 'output'},
        init(task){
           item.shape.on('sliding', function(slideEventData) {
                task.run(slideEventData.value);
                task.reset();
            });
        }
      }
    }
    
    builder(node) {
      node.addOutput(new Rete.Output('act', 'Action', Sockets.actionSocket));
      node.addOutput(new Rete.Output('currentValue', 'Value', Sockets.numberSocket));
    }
    
    worker(node, inputs, sliderValue) {  
      return {
          currentValue: sliderValue
      }
    }
}

export default OnSlideComponent;

ColorRamp component (value to color):

import Rete from "rete";
import Sockets from "../sockets";
import NumberControl from "../controls/numbercontrol";
import Rainbow from "rainbowvis.js";

class ColorRampComponent extends Rete.Component {
  
    constructor() {
      super('Color ramp');
      this.rainbow = new Rainbow();
      this.task = {
        outputs: {
            actout: "option",
            color: "output"
        }
      }
    }
  
    builder(node) {
      var rangeMinInput = new Rete.Input('rangemin', 'Min.', Sockets.numberSocket);
      rangeMinInput.addControl(new NumberControl("rangemin", node.data.midiChannel));

      var rangeMaxInput = new Rete.Input('rangemax', 'Max.', Sockets.numberSocket);
      rangeMaxInput.addControl(new NumberControl("rangemax", node.data.midiChannel));

      var valueInput = new Rete.Input('value', 'Value', Sockets.numberSocket);
      valueInput.addControl(new NumberControl("value", node.data.midiValue));

      var colorOutput = new Rete.Output('color', 'Color', Sockets.colorSocket);

      node
        .addInput(new Rete.Input('act', 'Action', Sockets.actionSocket))
        .addInput(rangeMinInput)
        .addInput(rangeMaxInput)
        .addInput(valueInput)
        .addOutput(colorOutput)
        .addOutput(new Rete.Output('actout', 'Action', Sockets.actionSocket));
    }
  
     worker(node, inputs, outputs) {
        var value = inputs["value"] ? inputs["value"][0] : node.data.value;
        var calculatedColor = "#" + this.component.rainbow.colourAt(value);
        return {color: calculatedColor};
     }
  }

export default ColorRampComponent;

SetColorComponent (updates UI component):

import Rete from "rete";
import Sockets from "../sockets";
import Spectrum from "spectrum-colorpicker";
import "../../../node_modules/spectrum-colorpicker/spectrum.css";

import ColorPickerControl from "../controls/colorpickercontrol";

class SetColorComponent extends Rete.Component {
  
    constructor(item) {
      super('Set Color');
      this.task = {
        item: item, /* BW: There might be an easier way to pass objects to the worker function, but I don;t know it yet */
        outputs: {}
      }
    }
  
    builder(node) {
      var ctrl = new ColorPickerControl("color", node.data.color);
      
      var colorInput = new Rete.Input('color', 'Color', Sockets.colorSocket);
      colorInput.addControl(ctrl);

      node
        .addInput(colorInput)
        .addInput(new Rete.Input('act', 'Action', Sockets.actionSocket));
    }
  
    worker(node, inputs) {
        var color = inputs["color"] ? inputs["color"][0] : node.data.color;
        this.component.task.item.setColor(color);
   }
  }

  export default SetColorComponent;

Modules versions: “rete”: “^1.4.3-rc.1”, “rete-connection-plugin”: “^0.9.0”, “rete-context-menu-plugin”: “^0.5.2”, “rete-keyboard-plugin”: “^0.1.2”, “rete-task-plugin”: “^0.3.0”, “rete-vue-render-plugin”: “^0.5.0”,

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Ni55aNcommented, Apr 1, 2020

How do you call task.run()? Every node caches their output data. By default it will reset output data after the tasks chain will be completed

https://github.com/retejs/task-plugin/blob/47d6ca139ef42785869ebf2ea4b74800302ba9ce/src/task.js#L28

item: item, /* BW: There might be an easier way to pass objects to the worker function, but I don;t know it yet */

task.run(data);
///
worker(node, inputs, data) { // <--

}
0reactions
Ni55aNcommented, Apr 26, 2020

@defcon8 v0.2.0 caches data, thats why the Render node gets old data from Value to Color

At the moment, I do not see a workaround for you except using 0.1.7, since in some cases caching is important. Perhaps for more complex things you might be better off using, for example, Rx.js instead of task plugin

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Web Worker on onChange event - Stack Overflow
I want to ask if it is a good approach to use WebWorker in onChange event. Like new instance of webworker will be...
Read more >
Process | Node.js v19.3.0 Documentation
Node.js v19.3.0 documentation ... Alternatively, change the process. ... The 'worker' event is emitted after a new <Worker> thread has been created.
Read more >
Understanding Worker Threads in Node.js | by NodeSource
To understand Workers, first, it's necessary to understand how Node.js is structured. When a Node.js process is launched, it runs:.
Read more >
Node.js multithreading: Worker threads and why they matter
So, setImmediate() is sufficient for some simple use cases, but it's far from an ideal solution. Can we do parallel processing without threads?...
Read more >
Using Web Workers - Web APIs - MDN Web Docs
Data is sent between workers and the main thread via a system of messages — both sides send their messages using the postMessage()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found