Not so. If you correct your event to links:add
instead of link:add
, you will see that as the Editor loads, you get a ton of messages. No need to do that in onpaletteadd
.
Here is the start of the Editor script code for one of my test nodes:
// Isolate this code
(function () {
'use strict'
/** Module name must match this nodes html file @constant {string} moduleName */
const moduleName = 'feedme'
/** Node's label @constant {string} paletteCategory */
const nodeLabel = moduleName
/** Node's palette category @constant {string} paletteCategory */
const paletteCategory = 'Totally Information' // 'advanced-input'
/** Node's background color @constant {string} paletteColor */
const paletteColor = '#C0DEED' // '#F6E0F8' //'#E6E0F8'
function runtimeCommsHandler(event, dataAsString) {
console.log('>>> MSG FROM RUNTIME', dataAsString, event)
}
// Can only push from the runtime to the editor, not the other way around
RED.comms.subscribe('jk-test-runtime', runtimeCommsHandler)
console.log('hi')
RED.events.on('links:add', function(link) {
console.log("A link has been added to the workspace!", link)
})
RED.events.on('links:remove', function(link) {
console.log("A link has been removed from the workspace!", link)
})
The events also fire immediately, no deploy is needed.
UPDATE: OK, so this is actually HARD! Ideally, you just want to know if a link is added AFTER the Editor is loaded. Node-RED will signal links:add
when the Editor is loaded for all existing wired nodes.
So somehow you need to exclude all those events.
Then the links:add
event is triggered when ANY link is added to ANY node. You can limit processing to the TYPE of node you are interested in but you cannot tie the processing to the "current" node because the Editor does not give you access to that information. Only the runtime has that & it only has it for the node instance code so you can't even pass it back in the registerType
settings option. Nor could you create an API in the runtime.
Then you have the issue that you need to be able to handle nodes that haven't yet been deployed.
So at the moment, my conclusion is that there appears to be no way to achieve what you are asking for.
Hopefully someone with a bigger brain can work something out.
Possibly you could create a runtime API that is called from the links:add
event handler in the Editor only if the source type is the correct type. On the runtime side, the API would need to retrieve the correct node(s) via the RED API and process the logic. The return would be a 200 code for success or some other code along with possibly some reason data if not a success and the event handler would then display an appropriate user message.
Complex.