Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use after_initialize instead of to_prepare to prevent subscribing multiple times to events in development environment #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/downstream/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Downstream
class Engine < ::Rails::Engine
config.downstream = Downstream.config

config.to_prepare do
config.after_initialize do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only setup subscribers during the app initialization, we lose code updates on reload—the store would still references previous versions of subscribers.

I think, what we should do here is to reset the store and then trigger the hook to let the app to register fresh versions of the subscribers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that would be a better solution. I did some digging, but its quite hard to unsubscribe to all downstream-events since we don't keep a list of subscribers. I was able to make it work, but I'm accessing private methods, so it's very fragile.

config.to_prepare do
  ActiveSupport::Notifications.notifier.instance_variable_get(:@string_subscribers).each_key do |key|
    next unless key.is_a?(String) && key.starts_with?("downstream-events")

    ActiveSupport::Notifications.unsubscribe(key)
  end
  ActiveSupport.run_load_hooks("downstream-events", Downstream)
end

@palkan Do you have any suggestions to improve this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, dealing with AS internals doesn't look stable. I guess, a better solution would be to add some reset functionality to Downstream (extend the Pubsub interface: https://github.com/palkan/downstream/blob/master/lib/downstream/pubsub_adapters/stateless/pubsub.rb).

ActiveSupport.run_load_hooks("downstream-events", Downstream)
end
end
Expand Down
Loading