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

Later registered events with class method will not be registered on the instance too #6

Open
drqCode opened this issue Feb 13, 2019 · 0 comments

Comments

@drqCode
Copy link

drqCode commented Feb 13, 2019

Later registered events with class method will not be registered on the instance too, because the duplication of events and listeners is being done only when any instance method ( any that use __bus__) is called.

# Subscribe to events.
#
# If the query parameter is provided, filters events by payload.
#
# @param [Symbol,String,Object] object_or_event_id The event identifier or a listener object
# @param [Hash] query An optional event filter
#
# @return [Object] self
#
# @api public
def subscribe(object_or_event_id, query = EMPTY_HASH, &block)
if block
__bus__.subscribe(object_or_event_id, query, &block)
else
__bus__.attach(object_or_event_id, query)
end
self
end
# Unsubscribe a listener
#
# @param [Object] listener The listener object
#
# @return [self]
#
# @api public
def unsubscribe(listener)
__bus__.detach(listener)
end
# Return true if a given listener has been subscribed to any event
#
# @api public
def subscribed?(listener)
__bus__.subscribed?(listener)
end
# Utility method which yields event with each of its listeners
#
# Listeners are already filtered out when query was provided during
# subscription
#
# @param [Symbol,String] event_id The event identifier
# param [Hash] payload An optional payload
#
# @api public
def process(event_id, payload = EMPTY_HASH, &block)
__bus__.process(event_id, payload, &block)
end
# Internal event bus
#
# @return [Bus]
#
# @api private
def __bus__
@__bus__ ||= self.class.new_bus
end

For example, given that dry-monitor registers :sql event with the .register_event(:name):

https://github.com/dry-rb/dry-monitor/blob/7a0625c199c8cbd8d053956b2735d4074a22b0dd/lib/dry/monitor/sql/logger.rb#L5-L10

loading the sql dry-monitor extension after an instance of Publisher was created and consumed will not registers new events if the entire app uses the same instance of 'Publisher' (Dry::Web::Container creates an instance of Notifications publisher that is supposed to be used globally, at least for logging)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant