Sha256: 6b08aae325085ca3b9066cbc25988587ef25937cfd1ddb613ecdbf4e832816b6
Contents?: true
Size: 1.96 KB
Versions: 2
Compression:
Stored size: 1.96 KB
Contents
# frozen_string_literal: true module Emittance class Synchronous ## # The synchronous dispatcher. Runs callbacks one-by-one, in series. # class Dispatcher @registrations = {} @enabled = true class << self 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) callback end 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
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
emittance-0.0.6 | lib/emittance/dispatchers/synchronous.rb |
emittance-0.0.5 | lib/emittance/dispatchers/synchronous.rb |