Class: Brainguy::Emitter
- Inherits:
-
Set
- Object
- Set
- Brainguy::Emitter
- Defined in:
- lib/brainguy/emitter.rb,
lib/brainguy/subscription_scope.rb
Overview
This object keeps track of all the listeners (observers) subscribed to a particular event source object.
Direct Known Subclasses
FluentEmitter, IdempotentEmitter, ManifestEmitter, SubscriptionScope
Constant Summary collapse
- DEFAULT_NOTIFIER =
BasicNotifier.new
Class Method Summary collapse
-
.new_from_existing(event_source, subscription_set) ⇒ Emitter
Create a new Emitter that shares its inner dataset with an existing one.
Instance Method Summary collapse
-
#attach(new_listener) ⇒ Subscription
Attach a new object to listen for events.
-
#detach(listener) ⇒ void
Detach a listener.
-
#emit(event_name, *extra_args) ⇒ Object
Emit an event to be distributed to all interested listeners.
-
#initialize(event_source = self, options = {}) ⇒ Emitter
constructor
A new instance of Emitter.
-
#on(name_or_handlers, &block) ⇒ Subscription
Attach blocks of code to handle specific named events.
-
#subscriptions ⇒ Set<Subscription>
The underlying set of subscription objects.
- #with_subscription_scope {|SubscriptionScope| ... } ⇒ Object
Constructor Details
#initialize(event_source = self, options = {}) ⇒ Emitter
Returns a new instance of Emitter.
32 33 34 35 36 37 38 |
# File 'lib/brainguy/emitter.rb', line 32 def initialize(event_source = self, = {}) super([:subscriptions] || Set.new) @event_source = event_source @notifier_maker = .fetch(:notifier_maker) { ->() { DEFAULT_NOTIFIER } } end |
Class Method Details
.new_from_existing(event_source, subscription_set) ⇒ Emitter
Create a new Brainguy::Emitter that shares its inner dataset with an existing one. This exists so that it's possible to generate temporary copies of a Brainguy::Emitter with different, specialized semantics; for instance, an IdempotentEmitter that shares the same set of subscriptions as an existing Brainguy::Emitter.
24 25 26 |
# File 'lib/brainguy/emitter.rb', line 24 def self.new_from_existing(event_source, subscription_set) new(event_source, subscriptions: subscription_set.subscriptions) end |
Instance Method Details
#attach(new_listener) ⇒ Subscription
Attach a new object to listen for events. A listener is expected to be
call-able, and it will receive the #call
message with an Brainguy::Event each
time one is emitted.
51 52 53 54 55 |
# File 'lib/brainguy/emitter.rb', line 51 def attach(new_listener) FullSubscription.new(self, new_listener).tap do |subscription| self << subscription end end |
#detach(listener) ⇒ void
This method returns an undefined value.
Detach a listener. This locates the subscription corresponding to the given listener (if any), and removes it.
61 62 63 |
# File 'lib/brainguy/emitter.rb', line 61 def detach(listener) delete(FullSubscription.new(self, listener)) end |
#emit(event_name, *extra_args) ⇒ Object
Emit an event to be distributed to all interested listeners.
92 93 94 95 96 97 98 99 |
# File 'lib/brainguy/emitter.rb', line 92 def emit(event_name, *extra_args) notifier = @notifier_maker.call each do |subscription| event = Event.new(event_name, @event_source, extra_args) notifier.notify(subscription, event) end notifier.result end |
#on(name, &block) ⇒ Subscription #on(handlers) ⇒ Subscription
Attach blocks of code to handle specific named events.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/brainguy/emitter.rb', line 76 def on(name_or_handlers, &block) case name_or_handlers when Symbol attach_to_single_event(name_or_handlers, block) when Hash attach(OpenObserver.new(name_or_handlers)) else fail ArgumentError, "Event name or Hash required" end end |
#subscriptions ⇒ Set<Subscription>
Returns the underlying set of subscription objects.
41 42 43 |
# File 'lib/brainguy/emitter.rb', line 41 def subscriptions __getobj__ end |
#with_subscription_scope {|SubscriptionScope| ... } ⇒ Object
58 59 60 |
# File 'lib/brainguy/subscription_scope.rb', line 58 def with_subscription_scope(&block) SubscriptionScope.open(self, &block) end |