Context updated automatically?

Hi All,

I have a function node, in which I read a flow context variable, and based on some conditions, I update them. Not in all circumstances. But there is a case where the code runs through a condition which does not have a flow.set statement, but it looks like the context is still getting updated.
I could be overviewing something obvious but I could not figure out what.
My function node in high level is like this

let data = flow.get("data");

if (<condition A>) {
  data.state = "A";
  flow.set("data",data);
  node.status({...});
  return msg;
}
if (<condition B>) {
  data.state = "B";
  node.status({...});
}

My issue is that I am looking at the case where condition B is true (condition A is not), and in this if statement, there is no flow.set(), but still I see the change in the flow variable in the right hand side of the screen.

This should not happen right? data is a copy of what is stored in the context and it is not a reference to the context instance?

Thanks,
Csongor

No. flow.get will return the object itself. Assuming you are using an in-memory context store, then any modification will be reflected in context.

1 Like

oh, I had no idea. Thanks for the clarification. After all it is not a problem, I needed to be clear about it.
I use mix of in-memory context store and also file storage. Is the behaviour based on the type of context store used?

Yes (with a but)

If you get an object from either memory or file store & modify its properties, you will be modifying the object (subsequent gets will reflect that). However, without calling set you will not instruct the file store to "flush" changed stuff to file.

In simple terms, do a flow.set("myObject", myObject, "filestore") after modifying an object directly to instruct the file store there are changes to write to disk.

1 Like

Thanks for the confirmation. From now on I start omitting flow.set for in-memory context variables.

To be honest, I would recommend doing the opposite - ensure you always call flow.set() after modifying context. If in the future you want to use a persistent context store, you don't have to go back through all of your function nodes to add it in - risking missing one.

3 Likes

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