Right now if I wish to handle the configured output count "dynamically" inside function node, i'd also have to set it in javascript as the configured one is not exposed to the sandbox - so setting it twice.
My suggestion is to expose the output count as node.outputs
inside the sandbox - a really easy addition to 10-function.js.
This should also be added to the user guide about writing functions
@knolleary If you're happy with this addition I can do PR for both.
My specific use case is function which debounces and counts inputs (counting consecutive button clicks) and i'd like to send output immediately and reset after output limit is reached.
For anyone intrested, here is my code:
// How many milliseconds to wait for another click
const debounceMs = 1000;
// It would be nide to have node.outputs here :)
const outputs = 4;
// Read last state from node context
let counter = context.get('counter') || 1;
let timer = context.get('timer');
// We have existing timer which means this is consecutive click
if (timer) {
clearTimeout(timer);
counter++;
}
const sendMessages = () => {
// Reset state
context.set('counter');
context.set('timer');
// Send last message to corresponding output
const messages = new Array(counter);
messages[counter - 1] = msg;
node.send(messages);
};
if (counter < outputs) {
// We can still wait for another click
timer = setTimeout(sendMessages, debounceMs)
context.set('counter', counter);
context.set('timer', timer);
} else {
// Output limit reached, reset & send output immediately
sendMessages();
}
Another use case could be some sort of load balancing between outputs.
This addition would allow write these functions generically only needing to configure the output count of the node itself.