9

I have wisper listeners in the app/listeners directory.

I also have the /config/initializers/wisper.rb

module Wisper
  def self.setup
    configure do |config|
      config.broadcaster(:default, Broadcasters::LoggerBroadcaster.new(Rails.logger, Broadcasters::SendBroadcaster.new))
    end
  end
end

Wisper.setup

Wisper.subscribe(ProjectListener.new)
Wisper.subscribe(FeedListener.new)

Can I somehow force Rails to reload the Listeners at every request?

Boti
  • 3,275
  • 1
  • 29
  • 54

1 Answers1

21

You could try wrapping the subscribes in a to_prepare block, something like:

Rails.application.config.to_prepare do
  Wisper.clear if Rails.env.development?
  Wisper.subscribe(ProjectListener.new)
  Wisper.subscribe(FeedListener.new)
end

to_prepare is called once in production and before every request in development environment.

If you subscribe in more than one initializer you could put the Wisper.clear in an initializer named '01_clear_subscribers` to ensure the subscribers are only cleared once.

Incidentally you don't need to override setup to configure the broadcaster, just do Wisper.configure do |config|.

Kris
  • 19,188
  • 9
  • 91
  • 111
  • 1
    This works in TDD/BDD mode too: Wisper.clear unless Rails.env.production? – Boti Feb 27 '15 at 08:47
  • 5
    It's worth noting that this fixed the problem I had where changes to any of my Rails code resulted in the "A copy of X has been removed from the module tree but is still active!" error. Hopefully, this comment will mean that people will find this solution quicker than I did! – Steve N Feb 04 '16 at 09:50
  • I ran into this issue in my Rails project and I decided to use `before_initialize` instead of `to_prepare` so that I ensure that the clear runs only once during the initialization process for all environments. – ecbrodie Dec 01 '16 at 20:56