lib/emittance/broker.rb in emittance-0.0.1 vs lib/emittance/broker.rb in emittance-0.0.2

- old
+ new

@@ -1,73 +1,26 @@ -# @private -class Emittance::Broker - @registrations = {} - @enabled = true +# frozen_string_literal: true - class << self - attr_reader :enabled - - def process_event(event) - return unless enabled? - - registrations_for(event).each do |registration| - registration.call event +module Emittance + ## + # Base class for event brokers. + # + class Broker + class << self + # @param _event [Emittance::Event] the event to be passed off to watchers + def process_event(_event) + raise NotImplementedError end - end - def register(identifier, &callback) - identifier = normalize_identifier identifier - @registrations[identifier] ||= [] - registrations_for(identifier) << Emittance::Registration.new(identifier, &callback) - end - - def register_method_call(identifier, object, method_name) - register identifier, &lambda_for_method_call(object, method_name) - end - - def clear_registrations! - @registrations.keys.each do |identifier| - self.clear_registrations_for! identifier + def inherited(subklass) + register_broker subklass + super end - end - def clear_registrations_for!(identifier) - identifier = normalize_identifier identifier - @registrations[identifier].clear - end - - def registrations_for(identifier) - identifier = normalize_identifier identifier - @registrations[identifier] || [] - end - - private - - def normalize_identifier(identifier) - if is_event_klass?(identifier) || is_event_object?(identifier) - identifier.identifier - else - coerce_identifier_type identifier + def register_broker(broker) + Emittance::Brokerage.register_broker broker end end - - def lambda_for_method_call(object, method_name) - ->(event) { object.send method_name, event } - end - - def is_event_klass?(identifier) - identifier.is_a?(Class) && identifier < Emittance::Event - end - - def is_event_object?(identifier) - identifier.is_a? Emittance::Event - end - - def coerce_identifier_type(identifier) - identifier.to_sym - end - - def enabled? - enabled - end end end + +require 'emittance/brokers/synchronous'