Class: Brainguy::SingleEventSubscription

Inherits:
Subscription show all
Defined in:
lib/brainguy/single_event_subscription.rb

Overview

A subscription to a single type (name) of event. The listener will only be notified if the event name matches subscribed_event_name.

See Emitter#on for where this class is used.

Instance Attribute Summary

Attributes inherited from Subscription

#listener, #owner

Instance Method Summary collapse

Methods inherited from Subscription

#<=>, #cancel, #hash

Constructor Details

#initialize(owner, listener, subscribed_event_name) ⇒ SingleEventSubscription

Returns a new instance of SingleEventSubscription.

Parameters:

  • owner (Emitter)

    the owning Emitter

  • listener (:call)

    some callable that should be called when the named event occurs

  • subscribed_event_name (Symbol)

    the event to subscribe to



13
14
15
16
# File 'lib/brainguy/single_event_subscription.rb', line 13

def initialize(owner, listener, subscribed_event_name)
  @subscribed_event_name = subscribed_event_name
  super(owner, listener)
end

Instance Method Details

#handle(event) ⇒ Object

Call listener if the event name is the one being subscribed to.

Parameters:

  • event (Event)

    the event to (maybe) dispatch



20
21
22
23
# File 'lib/brainguy/single_event_subscription.rb', line 20

def handle(event)
  return unless event.name == @subscribed_event_name
  @listener.call(*event.args)
end