I've got a function node that has two inputs one output and it looks like this.
var allowThroughput = context.get("allowThroughput");
node.log("allowThroughput: " + allowThroughput)
if(msg.payload == "changeThroughput")
{
allowThroughput = !allowThroughput;
node.log("Changed allowThroughput: " + allowThroughput)
}
context.set("allowThroughput", allowThroughput)
if(allowThroughput && msg.payload != "changeThroughput" )
return msg;
else
return null;
The purpose of this function is to stop the throughput of messages if I press the inject node connected to it. Because for some bizarre reason I can't stop or pause my flow, which would've been a very handy thing to have while prototyping. So I've made this as a workaround to stop looping behaviour.
The second input is a function that sends 1 or 0 to light up an LED that I've got connected to my raspberry pi. The problem is that when I change it to initialize allowThroughput if it doesn't exist like so:
var allowThroughput = context.get("allowThroughput")||true;
then it stops working.
Below I've copied the output from my function.
Without initializing:
16 Aug 15:58:13 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:13 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:14 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:14 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:14 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:58:14 - [info] [function:Loop Controller] allowThroughput: false
16 Aug 15:58:15 - [info] [function:Loop Controller] allowThroughput: false
16 Aug 15:58:15 - [info] [function:Loop Controller] Changed allowThroughput: true
16 Aug 15:58:16 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:16 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:58:16 - [info] [function:Loop Controller] allowThroughput: false
16 Aug 15:58:16 - [info] [function:Loop Controller] Changed allowThroughput: true
As you can see, it correctly stops when I press the inject button, and I am able to control the allowThroughput variable.
With initializing:
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:36 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:36 - [info] [function:Loop Controller] allowThroughput: true
As you can see I am no longer able to control the allowThroughput variable. It is set to true no matter what. Can someone explain to me why this is?