This issue is related to this older discussion: Does dry-events expect publishers to be "static" classes?
We’re using dry-events 0.2.0 (because of an older version of Rails) and running into a problem when including a publisher in our development environment. It seems that when classes are reloaded in development (due to the config.cache_classes = false
environment setting) this causes our publisher class to reload, which results in a PublisherAlreadyRegisteredError
error.
Conditionally including the publisher (like below) does not work, as the #register_event
method is unknown if the include does not apply.
include Dry::Events::Publisher[:event_trolly] if Dry::Events::Publisher.registry.keys.exclude?(:event_trolly)
We did not notice this issue until we implemented another bit of code that seems to (I think) output to the file system, which in turn triggers a class reload in development, which in turn triggers this error.
Is this something that has been solved in later versions of dry-events? Is there any recommendation as to what I could potentially do about this in my current situation?
I appreciate any help I can get.
Here is a simplified version of the publisher:
# frozen_string_literal: true
require 'dry/events/publisher'
require 'singleton'
class EventTrolly
include Singleton
include Dry::Events::Publisher[:event_trolly]
register_event('story.published')
# This method is pulled from dry-events version 0.3.0.
# Link to repo: https://github.com/dry-rb/dry-events/blob/master/lib/dry/events/bus.rb
# This method comes from dry/events/bus class. We currently need to use dry-events version 0.2.0 due to the version
# of dry-core being used in coordination with the version of Graphiti we are using, itself due to our old version of
# Rails (4.2).
#
# Call example:
# publisher.subscribed?(subscriber.method(:on_event))
# where subscriber is the class that implements the method corresponding to the event that is registered to the
# publisher.
def subscribed?(listener)
self.__bus__.listeners.values.any? do |value|
value.any? do |block, _|
case listener
when Proc then block.equal?(listener)
when Method then listener.owner == block.owner && listener.name == block.name
end
end
end
end