lib/emittance/dispatchers/synchronous.rb in emittance-0.0.6 vs lib/emittance/dispatchers/synchronous.rb in emittance-0.1.0

- old
+ new

@@ -3,77 +3,31 @@ module Emittance class Synchronous ## # The synchronous dispatcher. Runs callbacks one-by-one, in series. # - class Dispatcher - @registrations = {} - @enabled = true - + class Dispatcher < Emittance::Dispatcher class << self - def process_event(event) + private + + def _process_event(event) registrations_for(event).each do |registration| registration.call event end end - def registrations_for(identifier) - event_klass = find_event_klass identifier - registrations[event_klass] ||= empty_registration - registrations[event_klass] - end - - def register(identifier, &callback) - event_klass = find_event_klass identifier - registrations[event_klass] ||= empty_registration - registrations_for(event_klass) << Registration.new(event_klass, &callback) - + def _register(identifier, &callback) + registrations = registrations_for identifier + registrations << callback callback end - def register_method_call(identifier, object, method_name) + def _register_method_call(identifier, object, method_name) register identifier, &lambda_for_method_call(object, method_name) end - def clear_registrations! - registrations.each_key do |event_klass| - clear_registrations_for! event_klass - end - end - - def clear_registrations_for!(identifier) - event_klass = find_event_klass identifier - registrations[event_klass].clear - end - - private - - attr_accessor :registrations - - def empty_registration - Set.new - end - - def find_event_klass(event) - Emittance::EventLookup.find_event_klass(event) - end - def lambda_for_method_call(object, method_name) ->(event) { object.send method_name, event } - end - end - - # @private - class Registration - attr_reader :event_klass - - def initialize(event_klass, &callback) - @event_klass = event_klass - @callback = callback - end - - def call(event) - @callback.call event end end end end end