For clarity:
I'm not asking how to implement multiple inputs to a function, I already know the workarounds. I want to know why Node-RED developers would inhibit such a functionality.
It seems so obvious and natural to allow multiple inputs to a function.
For example: say I want to create a function Divide(dividend, divisor)
that returns dividend
divided by divisor
. In JavaScript this would be:
function Divide(dividend, divisor) {
return dividend / divisor
}
With Node-RED, the same function looks like this:
var dividend = 0
var divisor = 0
if (msg.topic == 'dividend')
{
dividend = msg.payload
context.set('dividend', dividend)
divisor = context.get('divisor')
}
else if (msg.topic == 'divisor')
{
divisor = msg.payload
context.set('divisor', divisor)
dividend = context.get('dividend')
}
return {payload:(dividend/divisor)};
(In addition, I have to assign topics 'divisor' and 'dividend' to the inputs so that the function doesn't get confused between which is which)
I understand that this is because Node-RED is message-based rather than functional, but I can't really be the only person who thinks this is an absurd amount of effort for such a simple task, right?
I understand that I could avoid using context
if I used a join
node and wait to receive two messages, but I want my function to update upon any change of input, since otherwise the function's output would be out-of-date until the second message is received.
I would imagine if developers implemented a multiple input functionality, the function would run upon receiving a new message on any of its inputs, and since messages on any given wire are retained by default until the next message is sent, you wouldn't have to worry about implementing retention using context
.
Also, if you were able to name function inputs, you wouldn't have to worry about sorting the inputs by topic and explicitly assigning a topic to each of your inputs to support this.
Is this a deficiency of Node-RED or am I completely missing the point?