a corresponding resource is created in some external system/service
as you edit the flow, restart node-red, disable flows, re-enable flows, etc. the 1-1 relationship between that node and the external resource is maintained
when you delete the node from the flow the external resource is deleted
A concrete example could be a node that represents a web page on some external server. You add a node to your flow and a corresponding web page appears on the web site. You delete the node, the web page goes away.
(My use-case is the FlexDash dashboard and the external resources are widgets in FlexDash, but the question and difficulties are generic.)
Difficulties I'm encountering implementing something like that:
you cannot store a persistent reference to the external resource in your node, you can only send your node ID to the external service/system and have it store a back-pointer to the node. (You could use a persistent context store as a crutch, but that requires the user creating it in the settings and then entering its name into a config field of the node, which is really cumbersome.)
you cannot find out when a node got deleted, so you have to implement some form of garbage collection
to implement garbage collection you have to enumerate all nodes of the appropriate types and collect their IDs, I'm not sure there's a clean way to do this, also not sure where one would put code for this and how one would trigger it.
The MyNode function is called when Node-RED is started anyway.
You delete a (e.g. UI) node in the flow editor.
Every time you Deploy your changes, the MyNode function will also be called.
Anyway - based on the node types - you could build a list of ui node ids and send that to your FlexDash. In Flexdash you could determine whether UI nodes are added or removed, and then you can create your widgets. Because I assume that is the reason of your question?
But I doubt whether this is a good way to do it!
One main disadvantage I see, is that you want to do this only once in 1 single node. Because:
if you have N ui nodes in your flow, you want to do it only once.
if you remove the last UI node from the flow (which means you end up with 0 ui nodes), you still want to execute this loop.
That could be solved if we need to add a single Flexdash Manager node in the flow, that handles all this stuff. But some users will forget to add it, or remove it accidentally. Or they add multiple manager nodes...
Perhaps another approach would be better...
Suppose you have a FlexDash sidebar (similar to the current Dashboard). In that sidebar code you have access to all the flow editor events (see events), like the following events:
Perhaps you don't even need the latter two events, because in the deploy event you can enumerate all nodes and send them to Flexdash (but of course then you need the delta calculation in Flexdash...).
Imho this is a better solution since such a sidebar would allow you to integrate your Flexdash even more into Node-RED, for those who like the concept of ui nodes.
BTW developing a sidebar is completely different to developing a node. I am not an expert in it, and have done it only once (for my node-red-contrib-xterm node). If you want I can try to help you with building a very basic one, if you want...
Don't want to go too much off-topic, but I mean that you could simply collect all info about ui nodes in your sidebar panel. And that you add a link to an external third-party layout manager on your sidebar panel. That way you don't need to develop and maintain a complex sidebar like we have today. And perhaps you then even don't need to specify in every ui node's config screen, the size and tabsheet/group of the widget.
Anyway completely off topic, but just mentioning it as a possible way of working with a small manageble sidebar.