Expose configured output count to function node (I can PR)

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.

errr - outputs have to be wired to something to be useful - and then the changes need to be deployed to make those changes active... so I can't see how this could be useful ? If I started with one output (default) then the function somehow changed it to (say) 4 - the other 3 outputs wouldn't go anywhere... so ???

Whoops, I didn't mean the node to change number of outputs, just get the configured value so I can write more generic functions which can handle any number of configured outputs without repeating the number in the code.

Ah - much clearer... ! personally I can't see a problem with adding that.

1 Like

Awesome :slight_smile: I'll get to it!

Better be quick... just about to record the 1.3 release video... :wink:

2 Likes

Would node.outputCount or similar be a bit more accurate?

node.outputs kinda suggests to me this would be a collection/array/object of the outputs (and whatever properties they might have)?

I do like the clarity of node.outputCount, but node.outputs would be consistent with how you write nodes.

@knolleary Any preference?

outputCount is probably the clearest.

outputCount wins then!

I have updated the PR as well: Function node: add `node.outputCount` property to sandbox by kristianheljas Ā· Pull Request #2918 Ā· node-red/node-red Ā· GitHub

Any idea if this change will make it into 1.3?
Would like to know for documenting :slight_smile:

1 Like

looks like it's in ...

2 Likes

Awesome,

Following up with docs as well:

Let me know if I missed something! :slight_smile:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.