Class: SubscriptionRegistry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/subscription_registry.rb

Overview

!! Note this code is not currently working in a multi-threaded environment e.g. Puma We are migrating to using Renalware.config.broadcast_subscription_map and the Broadcasting concern.

Responsible for subscribing listeners to specific classes. This registry avoids the issues of global subscriptions which would need to be disabled if you wish to use an instance of the class without listeners and avoids the issues with Rails auto-loading feature.

This registry is primary used to allow a listener in one module to subscribe to broadcasters in a different module allowing the developer to manage dependencies in the direction they prefer. Subscriptions within the same module should be made directly against the broadcaster, not via this registry.

Assumes broadcasters respond to the ‘subscribe` method.

Usage:

Register the listener class with the broadcaster class.

SubscriptionRegistry.instance.register(OtherModule::Broadcaster, Listener)

Then in the other module, subscribe listeners registered above to the broadcaster.

broadcaster = SubscriptionRegistry.instance.subscribe_listeners_to(Broadcaster.new)

Instance Method Summary collapse

Instance Method Details

#register(broadcaster_class, listener_class) ⇒ Object



36
37
38
# File 'lib/subscription_registry.rb', line 36

def register(broadcaster_class, listener_class)
  fetch_key(broadcaster_class.name).add(listener_class.name)
end

#subscribe_listeners_to(broadcaster_instance) ⇒ Object



40
41
42
# File 'lib/subscription_registry.rb', line 40

def subscribe_listeners_to(broadcaster_instance)
  broadcaster_instance.tap { subscribe_listeners(broadcaster_instance) }
end