1

Is there a way to delete all observers listening to Sketchup.active_model.shadow_info ?

something like Sketchup.active_model.shadow_info.remove_all_observers()

johowie
  • 2,475
  • 6
  • 26
  • 42

1 Answers1

1

I'm afraid not. If that was possible you would remove observers for other extensions and cause all kinds of problems for these extensions.

I'm guessing this is for convenience when developing? Where you some times end up adding multiple observers?

The pattern I use is to always keep a reference to the observer instance I attach, and then have a wrapper function that tries to remove it before attaching again.

module Example

  def.attach_shadow_observer
    model = Sketchup.active_model
    model.shadow_info.remove_observer(@shadow_observer) if @shadow_observer
    @shadow_observer = MyShadowObserver.new
    model.shadow_info.add_observer(@shadow_observer)
  end

end

That's a very naive and simple example. You might find it convenient to create a manager that saves you from creating specific methods for every observer you attach.

thomthom
  • 2,854
  • 1
  • 23
  • 53