Class: Farce::Abstract::Publisher Abstract
- Inherits:
-
Object
- Object
- Farce::Abstract::Publisher
- Includes:
- Internal::Noncopyable, Internal::Freeze::Unfreezable
- Defined in:
- lib/farce/abstract/publisher.rb
Overview
Shared superclass for all publisher implementations.
Implements a publisher-subscriber (pub-sub) pattern.
Direct Known Subclasses
Local::Publisher, Publisher, Strict::Publisher, Unshared::Publisher
Instance Method Summary collapse
-
#closed? ⇒ Boolean
Whether the publisher is closed.
-
#initialize(*subscribers) ⇒ Publisher
constructor
A new instance of Publisher.
-
#publish(payload = nil) ⇒ self
(also: #send, #<<, #broadcast)
Publishes the given payload to all current subscribers.
-
#subscribe ⇒ Subscription
(also: #monitor)
The created subscription.
-
#subscribed?(subscriber) ⇒ Boolean
Whether the given subscriber is currently subscribed.
-
#subscription_for(subscriber) ⇒ Subscription?
The subscription for the given subscriber, or nil if none exists.
-
#unsubscribe(subscriber)
(also: #cancel, #unmonitor)
Unsubscribes the given subscriber from the publisher.
Methods included from Internal::Noncopyable
Constructor Details
Instance Method Details
#closed? ⇒ Boolean
Returns whether the publisher is closed.
24 |
# File 'lib/farce/abstract/publisher.rb', line 24 def closed? = @closed.value |
#publish(payload = nil) ⇒ self Also known as: send, <<, broadcast
Publishes the given payload to all current subscribers.
Full bounded queues drop new messages. Failed publishing attempts to closed or frozen receivers cancel their subscriptions.
33 34 35 36 37 |
# File 'lib/farce/abstract/publisher.rb', line 33 def publish(payload = nil) raise "Cannot publish to a closed publisher" if closed? map.each_value { it.publish(payload) } self end |
#subscribe(subscriber) ⇒ Subscription #subscribe(*args, mode: nil, scheduler: nil, capacity: nil) {|*args, payload| ... } ⇒ Subscription Also known as: monitor
Returns the created subscription.
108 |
# File 'lib/farce/abstract/publisher.rb', line 108 def subscribe(...) = block_given? ? create_subscription(...) : add_subscription(...) |
#subscribed?(subscriber) ⇒ Boolean
Returns whether the given subscriber is currently subscribed.
124 |
# File 'lib/farce/abstract/publisher.rb', line 124 def subscribed?(subscriber) = !!subscription_for(subscriber)&.subscribed? |
#subscription_for(subscriber) ⇒ Subscription?
Returns the subscription for the given subscriber, or nil if none exists.
113 114 115 116 117 118 119 120 121 |
# File 'lib/farce/abstract/publisher.rb', line 113 def subscription_for(subscriber) if subscriber.is_a?(Subscription) return unless subscriber.publisher.equal?(self) subscription = map[subscriber.key] subscription if subscription.equal?(subscriber) else map[subscriber.__id__] end end |
#unsubscribe(subscriber) Also known as: cancel, unmonitor
This method returns an undefined value.
Unsubscribes the given subscriber from the publisher.
129 |
# File 'lib/farce/abstract/publisher.rb', line 129 def unsubscribe(subscriber) = subscription_for(subscriber)&.unsubscribe |