# 1.3.4 not using modified class function node (in subflow?)

**URL:** <https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582>\
**Category:** General\
**Created:** [13 May 2021 09:29 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582 "2021-05-13T09:29:14Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 09:29 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/1 "2021-05-13T09:29:14Z")

</div>

Using node-red 1.3.4, nodejs 14.17.0 I have a function node inside a subflow with a class definition embedded in the function node.

I find that if I edit the code inside the class and deploy (even a full deploy) the modifications are not seen, the class keeps using the old definition. I have to restart node-red to get it to use the new code. The code can be as simple as adding a node.warn() statement, even a syntax error is not seen. I don't yet know whether the fact that it is inside a subflow is significant. If I edit the code in the same node, outside of the class definition, then the modified code is seen (but mods inside the class already made are still not picked up).

I intend to generate a simple flow to exhibit the problem but thought I would post this first in case it is a known issue, to save me investigating further.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 10:37 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/2 "2021-05-13T10:37:02Z")

</div>

I can't replicate it with a simple class so this will take a bit more investigation.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 11:15 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/3 "2021-05-13T11:15:13Z")

</div>

Got it! It is nothing to do with being in a subflow, it is the fact that I am saving a class object in the context, and I can understand that keeping a class instance in the context may be problematic. This is the function:

```auto
class Test {
    warn() {
        node.warn("testing 2")
    }
}

if (!context.get("test")) {
    context.set("test", new Test())
}

test = context.get("test")
test.warn()
return msg;

```

Run it and then change the text output in the `warn()` call. A Deploy does not pick up the the modified text. Restarting node-red or clearing the context fixes it.

```auto
[{"id":"3faa08ce.24ca18","type":"function","z":"84405ff5.25fa6","name":"","func":"class Test {\n warn() {\n node.warn(\"testing 2\")\n }\n}\n\nif (!context.get(\"test\")) {\n context.set(\"test\", new Test())\n}\n\ntest = context.get(\"test\")\ntest.warn()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":1680,"wires":[[]]},{"id":"df7fa705.bd14d","type":"inject","z":"84405ff5.25fa6","name":"Test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":1680,"wires":[["3faa08ce.24ca18"]]}]

```

**[Edit]** I realise I had posted the wrong function and flow. Now corrected.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 May 2021 11:21 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/4 "2021-05-13T11:21:37Z")

</div>

Maybe the solution is to use the on close function and do `context.set("test", undefined)`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 11:24 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/5 "2021-05-13T11:24:28Z")

</div>

> [@Steve-Mcl](#):
>
> use the on close function

Genius! Yes that does work around the problem.

So the question is, I suppose, does anything need to be done about it in node-red (if indeed there is a practical solution)?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 May 2021 11:28 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/6 "2021-05-13T11:28:01Z")

</div>

I guess what you're asking is should a function nodes context be reset when the function node is (re)deployed?

It seems, from your example, it is not - I don't know if it should be. Perhaps Nick or Dave know if this is by design or incidental?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 11:35 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/7 "2021-05-13T11:35:13Z")

</div>

It wouldn't be quite such a genius workaround if I wanted to keep it in persistent context of course.

> [@Steve-Mcl](#):
>
> I guess what you're asking is should a function nodes context be reset when the function node is (re)deployed?

I think that is a different issue.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 May 2021 11:40 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/8 "2021-05-13T11:40:31Z")

</div>

> [@Colin](#):
>
> I think that is a different issue

Not really. As you create the instance only when context is empty, changes to your class are not live until the context is deleted & a new instance is generated. I.e. until the context is cleared, the original code (instance) is held in memory. That's how js works.

So, to rephrase, if function context is supposed to be retained on a function redeploy, then your code is working exactly as expected/as written.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 12:50 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/9 "2021-05-13T12:50:54Z")

</div>

> [@Steve-Mcl](#):
>
> the original code (instance) is held in memory.

I don't think the code for a class is stored in the context, though I may be wrong. I imagine that what is happening is that the instance data includes some sort of reference to the class definition. When the function is re-deployed the new class definition is added to the code set but the original code is still present and the context data will still reference it. So perhaps there is a simple fix, that the original code needs to be linked to (or replaced by) the new one. That may not be simple, or even possible though, or I may have the mechanism wrong.

To clear the node context data on deploy is not a valid solution in the case that persistent context is in use, It would break the persistence.

Whether keeping a class object in persistent context even works I don't know .....

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 13:01 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/10 "2021-05-13T13:01:34Z")

</div>

I have just realised that I posted the wrong function and flow earlier. Now corrected.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 13:18 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/11 "2021-05-13T13:18:21Z")

</div>

> [@Colin](#):
>
> Whether keeping a class object in persistent context even works I don't know

And the answer is, no it doesn't work. Amending the function to use file based context, running it and then restarting node-red results in "TypeError: test.warn is not a function". The only recovery path is to delete the context object. I guess the reason is that the class reference stored in context no longer refers to a valid class definition.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 May 2021 13:20 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/12 "2021-05-13T13:20:17Z")

</div>

File context doesn't store functions Colin.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 May 2021 13:26 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/13 "2021-05-13T13:26:26Z")

</div>

So the function node is kind of like a virtual machine, created when you deploy. the objects created in it from the class are kinda like their own living entity & kept alive so long as there is a reference to it. Even though you change the definition of the class, the instantiated object (held in context/memory) is still an instance of the original definition.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 15:02 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/14 "2021-05-13T15:02:06Z")

</div>

> [@Steve-Mcl](#):
>
> File context doesn't store functions Colin.

A class instance isn't a function, but I realise that you are right that class objects cannot be put in file context, it is only objects that can be stringified that can be put in file context. Even, for example, a Date object cannot be put in file based context with the expectation that it somehow automatically becomes a Date again when recovered.

> [@Steve-Mcl](#):
>
> Even though you change the definition of the class, the instantiated object (held in context/memory) is still an instance of the original definition.

I think that is down to the implementation. Each instance of the class object does not contain the code, but must contain some reference to the code. Whether it is possible (and practical) for the re-deploy code to account for this somehow I don't know. Quite possibly not.

I still maintain though that whether a function node should clear the node context on re-deploy is a separate, and much larger, issue. To clear the node context data on deploy would break the persistence for functions using file based node context.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 May 2021 15:25 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/15 "2021-05-13T15:25:58Z")

</div>

> [@Colin](#):
>
> A class instance isn't a function

That meant to refer to the fact classes are pretty much syntactic sugar to a function (under the hood) - not really the point here TBH.

> [@Colin](#):
>
> > [@Steve-Mcl](#):
> >
> > Even though you change the definition of the class, the instantiated object (held in context/memory) is still an instance of the original definition.
> 
> I think that is down to the implementation. Each instance of the class object does not contain the code, but must contain some reference to the code. Whether it is possible (and practical) for the re-deploy code to account for this somehow I don't know. Quite possibly not.

ok, so I was being simplistic in my description - i did not mean to imply "Each instance of the class actually contains the code - the point i was trying to state was that the code that executes is the code that was instantiated regardless of changing the class definition because the object/function/instance is held/loaded into memory.

In the function code (server side) `node.script.runInContext` is called when fire a msg into it. At that point, the script is compiled. lets call this created instance V1. Then this V1 instance is stored in context/memory. When you modify and deploy a V2 of the class, `node.script.runInContext` is called again and the new version of the class is compiled however the V1 instance is still alive in memory because of context.

I think now that perhaps you are asking should the vm/sandbox be destroyed when you re-deploy? I dont think nodejs can do that (for technical reasons?).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 May 2021 15:31 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/16 "2021-05-13T15:31:52Z")

</div>

I think you are likely right, in which case it may be that all that can be done is to include something in the documentation pointing out the potential issues with saving class objects in context.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 July 2021 15:32 UTC](https://discourse.nodered.org/t/1-3-4-not-using-modified-class-function-node-in-subflow/45582/17 "2021-07-12T15:32:28Z")

</div>

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