I thought that deprecated meant that at some point in the future it will be removed and therefore it should not be used.
A further question, am I right in thinking that if one asks for a local file store and a memory store, that when using the memory store the operation using context.set() and specifying the memory store is identical to using context.myname?
@Colin
I'm surprised that you don't use the contextStorage property.
This is one of the features I was waiting since I'm using Node-RED. I guess 80% or more running v0.19 or greater use it with the default localfilesystem.
no - that is why it is "deprecated". As this thread has shown using .set works by serialising the data. This is necessary if you want to store the data "offboard" eg in a file / database / over the network. Whereas context.myname can handle non-serialisable data like objects containing functions or circular references - and is ONLY held in memory.
So in most cases it will appear the same to the user... but is NOT identical.
Has that always been the case? If so then the circular error seen is not inherent in storing timer id using context.set but must be something about this specific use case.
Back on the deprecation of context.myname. If it has a useful purpose that is not covered by context.set then I would have thought it cannot be deprecated. Unless my understanding of deprecated is wrong (that it should not be used as it will be removed from node-RED at some point).
I use MQTT with retained topics for this. It has a number of advantages:
Any subscribers to the topic get notified when the value changes and can take action. This does not happen with persistent context variables.
The persistent values are available across systems. For example I have similar dashboards in a number of different node-red systems. They can all subscribe to the same topics on the same server (where appropriate) so that, for example, if I have a switch on the dashboard and I switch it on one system then that is immediately echoed on the dashboards on the other systems.
Automatic initialisation of dependent objects on re-deploy. If I redeploy node-RED then the switch mentioned above automatically gets initialised. To achieve that with context variables requires something like using inject nodes configured to inject the value at startup.
I am not saying there are no uses for persistent context, just that I have not found the need so far.
Yes. There is nothing in setTimeout that is circular - but the way he is calling his function from inside his function makes it so.
context is a javascript object - we can't remove it or stop people using it in normal javascript ways (such as adding a property) - but we can advise against using it if they want to be able to persist values when restarting Node-RED.
Does that fix it for the users case? If so that appears to conflict with @dceejay's statement that context.set with memory store will still do the serialisation.
Deprecated means you shouldn't use it as there's no guarantee it will be there in the future. But nor is it a guarantee that it'll be removed.
In this instance, the only result of preventing a user from setting properties on the context object directly would be to break a lot of people's flows needlessly. There would be no benefit in doing that.
It was deprecated very early on in favour of context.set() because we did not want users to assume their flows would magically start persisting those values. That was long before we got into the detailed design and implementation work around persistent context.
During that work, it became clear there was still a role to play for setting context.foo directly - for volatile values that cannot be persisted, but have no need to be persisted. We just haven't spelt that out in the documentation.
I'll try this later today when I get a chance to VPN home. Did you test it with my function, or with a different (test) function?
I don't fully understand everything in this thread, but it's fascinating nevertheless, and I can pick up various bits and pieces of things people say here and experiment myself...
I modified you original function (and didn't change your "coding style"):
// Check for correct incoming values
if (!msg.reset &&
(!msg.payload ||
!msg.dimtime ||
!msg.initialdelay)) {
node.warn("Fader not correctly configured");
return;
}
var thisnodeid = "fader_" + node.id;
// Get incoming values
var startval = Number(msg.payload); // just in case incoming was a string
var dimtime = msg.dimtime * 1000;
var initialdelay = msg.initialdelay * 1000;
var steptime = dimtime / startval;
// If we got a msg.reset, cancel timer, clear status,
// clear function from memory, and don't restart
if (msg.reset) {
var id = context.get(thisnodeid, 'memoryOnly');
clearTimeout(id);
node.status({});
context.set(thisnodeid,"", 'memoryOnly');
return;
}
// if context.get('id') has an object, then it's running
// and if it's running then clear the timer, clear the memory,
// then continue (i.e. start over with new fader)
if (context.get('id') !== '') {
var id = context.get(thisnodeid, 'memoryOnly');
clearTimeout(id);
node.status({});
context.set(thisnodeid,"",'memoryOnly');
}
// Set initial light level
msg.payload = startval; // (use startval in case input was string)
node.send(msg);
var id = setTimeout(function(){
var i = startval;
function f() {
if (i>0) {
i--;
msg.payload = i;
node.send(msg);
var fadetxt = "Fading " + (i*steptime/1000).toString() + "s, " + i.toString() + "/" + startval.toString();
node.status({fill:"green", shape:"ring", text:fadetxt});
} else {
node.status({});
context.set(thisnodeid,"", 'memoryOnly');
return;
}
id = setTimeout( f, steptime );
context.set(thisnodeid,id, 'memoryOnly');
}
f();
},initialdelay)
context.set(thisnodeid,id, 'memoryOnly');
node.status({fill:"blue", shape:"ring", text:"Delay " + msg.initialdelay.toString() + "s"});