0

I want to register a global listener in my ApplicationController, that contains the current_user. I ended up trying this:

class ApplicationController < ActionController::Base
  before_action do
    @listener = MyListener.new(current_user)
    Wisper.clear if Rails.env.development?
    Wisper.subscribe(@listener, scope: :MyPublisher)
  end
end

However, when I deployed this code to heroku these global listeners never get unsubscribed and the app keeps on accumulating listeners through requests. I can't rely on after_action since the application could terminate due to an error. What is the right way of doing this, is it to forcibly clear before I subscribe, like this?

class ApplicationController < ActionController::Base
  before_action do
    @listener = MyListener.new(current_user)
    Wisper.clear
    Wisper.subscribe(@listener, scope: :MyPublisher)
  end
end

In another question, Kris suggested that we should use an initializer which subscribes once. The reason I am not doing this is because I want to have access to the current_user, and I prefer not to pass it via global variables/Thread.current. What is the best way to make GlobalListeners work with current_user?

My use case is to process all instances of ActiveRecord models loaded by current_user across all controller actions. Wisper does exactly what I needed it to do except for the issue mentioned.

class MyPublisher < ActiveRecord::Base
  include Wisper::Publisher
  after_find { broadcast(:process_find, self) }
end

and for the Listener:

class MyListener
  def initialize(current_user)
    @current_user = current_user
  end

  def process_find
    ...
  end
end
4

1 回答 1

0

您可以在一个块的持续时间内全局订阅您的侦听器:

def show
  Wisper.subscribe(MyListener.new(current_user)) do
    @model = MyPublisher.find(id)
  end
end

块完成后,侦听器将被取消订阅。

如果您希望它发生在多个操作中,您可以使用around_action过滤器:

around_action :subscribe_listener

def show
  @model = MyPublisher.find(id)
end

def create
  # ...
end

# etc.

private

def subscribe_listener
  Wisper.subscribe(MyListener.new(current_user)) do
    yield
  end
end
于 2016-05-12T08:33:05.473 回答