palkan / active_event_store

Rails Event Store in a more Rails way
MIT License
182 stars 13 forks source link

Following the documentation for subscribing to events causing duplicate subscriptions on code reload #19

Closed Samsinite closed 6 months ago

Samsinite commented 6 months ago

What did you do?

created an initializer like so as recommended in the documentation:

ActiveSupport.on_load(:active_event_store) do |store|
  # Subscribed event handlers to events here
end

What did you expect to happen?

That it would work as expected and the subscriber would only be called once.

What actually happened?

The subscriber would only be called once, until a file changed and code was reloaded, resulting in the on_load being called again, and if the event was fired, the subscriber would be called twice. For further clarification, the initializer file is only called once on app boot, but the block passed to ActiveSupport.on_load(:active_event_store) is called every time code reload happens.

Additional context

Changing the initializer to:

ActiveSupport.on_load(:active_event_store, { run_once: true }) do |store|
  # Subscribed event handlers to events here
end

worked like a charm. Either, my app has some funky config, or this is a bug in this libraries documentation. I suggest that the documentation is updated to recommend creating the initializer with :run_once, unless the application I am working on is doing something dumb. Happy to submit a PR with the README change, unless this shouldn't be happening in standard rails applications.

Samsinite commented 6 months ago

Nevermind, this is not an issue, pretty sure we had something dumb going on.