lib/dry/events/bus.rb in dry-events-0.1.0 vs lib/dry/events/bus.rb in dry-events-0.1.1

- old
+ new

@@ -16,26 +16,25 @@ # @return [Hash] A hash with event listeners registered within a bus attr_reader :listeners # Initialize a new event bus # - # @param [Symbol] id The bus identifier # @param [Hash] events A hash with events # @param [Hash] listeners A hash with listeners # # @api private def initialize(events: EMPTY_HASH, listeners: LISTENERS_HASH.dup) @listeners = listeners @events = events end # @api private - def process(event_id, payload, &block) - listeners[event_id].each do |(listener, query)| + def process(event_id, payload) + listeners[event_id].each do |listener, filter| event = events[event_id].payload(payload) - if event.trigger?(query) + if filter.(payload) yield(event, listener) end end end @@ -45,16 +44,16 @@ listener.(event) end end # @api private - def attach(listener, query) + def attach(listener, filter) events.each do |id, event| meth = event.listener_method if listener.respond_to?(meth) - listeners[id] << [listener.method(meth), query] + listeners[id] << [listener.method(meth), filter] end end end # @api private @@ -66,17 +65,17 @@ end end end # @api private - def subscribe(event_id, query, &block) - listeners[event_id] << [block, query] + def subscribe(event_id, filter, &block) + listeners[event_id] << [block, filter] self end # @api private def subscribed?(listener) - listeners.values.any? { |value| value.any? { |(block, _)| block.equal?(listener) } } + listeners.values.any? { |value| value.any? { |block, _| block.equal?(listener) } } end end end end