I have been writing a lot of components with event listeners lately and I have a few general questions about implementation so that I can be aware of best practices.
- I have a lot of things that I typically need to update (trigger new component solutions) when a new Rhino document has loaded. Typically, I use the output of my custom RhinoDoc listener component to manually expire these downstream components. Take this example, for instance:
Here I am updating the Wombat “Get Document Units” component using my listener (offscreen) + MetaHopper. First question is this: Is it safe to write a document units component that incorporates its own document listener and use that instead? Is it safe to write a whole bunch of components that have their own internal document listeners and use them all over a large definition?
Is this conservative approach of using a single document listener in the document and manually expiring downstream components via Metahopper actually providing any safety/benefit? Because if not, I would probably be adding listeners of all kinds to a lot of my components. This holds true for layer events, viewport events, etc. I’m concerned about execution flow (and triggering multiple downstream operations/race conditions/invalid flags, etc), which is why I’ve been doing it this way. I realize that the gaps introduced by using Metahopper’s ExpireSolution everywhere has its own concerns, probably.
- After hours and hours of debugging various components, I became alarmed that the standard practice that I’ve been using for years essentially detaches and attaches a listener every time the component is solved, which isn’t a big deal for listeners that are “set it and forget it”, but for listeners that have selective expiration of outputs based on the event results and call ExpireSolution, I feel like there is way too much listener hijinx going on (see the preposterous SelectedLayersListener example I posted last week). Would you recommend storing the “Active” state in a static variable so that I can determine whether or not to remove/add listeners only when absolutely necessary? Or does it not matter?
Thanks!
Marc