Publisher Already Registered Error in dry-events when reloading classes.

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

Well, apparently there is a time limit on editing a post, and mine took so long to get approved I’m out of that window. As an update, I’ve figured out a bit more about what is going on.

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 with a combination of ::Rails.application.eager_load! and the config.cache_classes = false environment setting, this causes our publisher class to reload (twice?), which results in a PublisherAlreadyRegisteredError error.

There is more info here as to why I’m eager loading the Rails app: Is it possible to get a list of all Resource class names? · Issue #417 · graphiti-api/graphiti · GitHub

In a flash of inspiration, I thought changing from include to require would solve the problem as require is only supposed to load the class if it hasn’t been loaded already. However that did not work, and was likely identical in function to my conditional loading attempt. The publisher class is not loaded, but some remnant of the publisher still remains in memory which seems to cause the issue.