I am using AsyncLocalStorage in a project, and I think we have a memory leak related to it.
So, my understanding from the documentation is I can start a new context using run or enterWith (I don't know the difference between them).
It is not clear to me how I can update the values. Currently, I am doing this:
const values = als.getStore();
const newValues = { // merge metadata
...values,
...additionalValues
};
als.enterWith(newValues);
return newMetadata;
Should I disable the AsyncLocalStorage before updating the value? I am using Node 18.
If you are using objects with values, the getStore method returns an object. Objects in JavaScript are not mere copies, they are references, which means that if you change them, you change the object.
const context = Context.getInstance().getStore();
console.log(Context.getInstance().getStore()) // { id: 1762430988552 }
context.raineri = "Marcelo";
console.log(Context.getInstance().getStore()) // { id: 1762430988552, raineri: 'Marcelo' }