Note that I have updated my original code above, based on some feedback from @gregorius
The original sidebar was initialized every time the 'Deploy' button was clicked. Because the wrong 'runtime-state' event handler was removed, e.g. the handler of another sidebar.
The code is now refactored to something like this, to make sure our sidebar event handler function is only called once (i.e. when all the nodes have been loaded):
var initializeSidebar = function( ) {
// Remove the event handler, to avoid that the sidebar is initialized at every deploy
RED.events.off('runtime-state', initializeSidebar);
...
}
// Add your plugin as a new tabsheet in the right sidebar AFTER the flow editor is completely started
RED.events.on('runtime-state', initializeSidebar);