[Done 2.6 Beta] More Elegant Way To Make Sure An Observable Exists?

Fiddled quite a bit to get this working for all cases, but i’m not sure if this is really a proper way to do this. My currrent script does something like below to make sure all snapshots are deleted when: A new songs is created or loaded, the code changed, anything changed in the tracks list or the reload all tools menu command was invoked. Using app idle is maybe not an elegant solution?

The main problem is the renoise.song().tracks_observable:add_notifier(). If i put it above the function without using app idle, it works fine as long as Renoise isn’t restarted. Getting an error then, that renoise.song() doesn’t exists yet. I’ve tried a test for nil, but this seems to trigger the error too so no chance. Then tried using an initialization function for _AUTO_RELOAD_DEBUG which creats all notifiers, but then it doesn’t catch the reload all tools menu command and i don’t get notfied of track changes anymore after it was used. Is this really the only way to make sure, that the tracks notifier stays alive without plastering extra checks all over the code?

  
--[[script initialization]]-----------------------------------------------]]--  
  
_AUTO_RELOAD_DEBUG = true   
  
renoise.tool().app_new_document_observable:add_notifier (  
 function() delete_snapshots() end  
)  
  
-- using the app idle notifier here is an ugly workaround to make sure, that  
-- the tracks observable notifier doesn't get killed by the "reload all tools"  
-- menu command  
  
renoise.tool().app_idle_observable:add_notifier (  
 function() tracks_notifier() end  
)  
  
function tracks_notifier()  
  
 if not renoise.song().tracks_observable:has_notifier (  
 function() delete_snapshots() end  
 ) then  
  
 renoise.song().tracks_observable:add_notifier (  
 function() delete_snapshots() end  
 )  
 end  
  
end   
  

_AUTO_RELOAD_DEBUG = function() -> attach to the current songs track list end
app_new_document_observable -> attach to the new songs track list

should do the trick.

Avoid “reload all tools”. You don’t need it when using _AUTO_RELOAD_DEBUG.

I’ve tried this and it doesn’t work like expected. The problem is that if a user invokes “Reload all tools” menu command the track notifier is dead and i can’t rely on it anymore. For all other cases it works fine.

Edit: Basically the init function doesn’t get called again. Maybe that menu command should trigger the function too specified in _AUTO_RELOAD_DEBUG? Then it would work fine.

We also have a related problem with duplex:
Duplex has preferences which set up devices that should be automatically started with Renoise. This is right now applied in the “NewDocument” notifications, cause we can’t do this before when there is no song yet.

Right now, when reloading tools, “NewDocument” is not called, cause there is no “new” one, just the existing one. So autostarting doesn’t work in this case, but definitely should.

So I think it makes sense to connect scripts with a song with the “NewDocument” in all scenarios. Aka, it should also be fired in order to connect the tool to the song which is already there, not just “new” ones from Renoises point of view. Will do this for the next beta.


In your case, this will then (in the next beta) do the job:

renoise.tool().app_new_document_observable -> connect to the song, list of tracks
renoise.song.tracks_observable -> delete_snapshots

when starting Renoise, app_new_document_observable will connect you to the first song that gets created
when reloading, app_new_document_observable will connect you to the already running song (but which is new for your newly loaded script)

That sounds like a good solution and i can throw away that idle thing. :)