本文介绍了在每次请求时自动重新加载 wisper 侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 app/listeners 目录中有 wisper 监听器.

I have wisper listeners in the app/listeners directory.

我也有/config/initializers/wisper.rb

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)

我可以以某种方式强制 Rails 在每次请求时重新加载监听器吗?

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

推荐答案

您可以尝试将订阅包装在 to_prepare 块中,例如:

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 在生产环境中和开发环境中的每个请求之前调用一次.

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

如果您订阅了多个初始化程序,您可以将 Wisper.clear 放在名为01_clear_subscribers"的初始化程序中,以确保订阅者只被清除一次.

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.

顺便说一句,您不需要覆盖 setup 来配置广播器,只需执行 Wisper.configure do |config|.

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

这篇关于在每次请求时自动重新加载 wisper 侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:38