Class: Farce::Subscription
- Inherits:
-
Object
- Object
- Farce::Subscription
- Includes:
- Internal::Noncopyable, Internal::Freeze::Unfreezable
- Defined in:
- lib/farce/subscription.rb,
lib/farce/subscription/definitions.rb
Overview
A publisher subscription that can be paused or permanently canceled. Disabling skips new messages. Unsubscribing drains an existing callback queue before stopping it.
Constant Summary collapse
- UnsubscribedError =
Raised when an operation is attempted on an unsubscribed subscription.
Class.new(StandardError)
Instance Attribute Summary collapse
-
#publisher ⇒ Farce::Abstract::Publisher
readonly
The publisher associated with this subscription.
-
#subscriber ⇒ BasicObject
readonly
The subscriber associated with this subscription.
Class Method Summary collapse
-
.define(*classes) {|payload| ... }
Register delivery behavior for subscriber classes from the main Ractor.
Instance Method Summary collapse
-
#disable
Disables the subscription, preventing it from receiving published payloads.
-
#enable
Enables the subscription, allowing it to receive published payloads.
-
#enabled? ⇒ Boolean
Whether the subscription is currently enabled.
-
#subscribed? ⇒ Boolean
Whether the subscription has not been permanently canceled.
-
#unsubscribe ⇒ BasicObject?
(also: #cancel)
Removes the subscription permanently, allowing it and the subscriber to be garbage collected.
Methods included from Internal::Noncopyable
Instance Attribute Details
#publisher ⇒ Farce::Abstract::Publisher (readonly)
Returns the publisher associated with this subscription.
57 58 59 |
# File 'lib/farce/subscription.rb', line 57 def publisher @publisher end |
#subscriber ⇒ BasicObject (readonly)
Returns the subscriber associated with this subscription.
54 55 56 |
# File 'lib/farce/subscription.rb', line 54 def subscriber @subscriber end |
Class Method Details
.define(*classes) {|payload| ... }
The block must be convertible to a shareable proc.
This method returns an undefined value.
Register delivery behavior for subscriber classes from the main Ractor. Subclasses inherit the nearest registered delivery method.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/farce/subscription.rb', line 33 def self.define(*classes, &) raise LocalJumpError, "no block given" unless block_given? definition = Internal.prepare_method_definition(&) classes.each do |klass| REGISTER.define(klass) do define_method(:deliver, definition) private :deliver end end end |
Instance Method Details
#disable
This method returns an undefined value.
Disables the subscription, preventing it from receiving published payloads.
103 |
# File 'lib/farce/subscription.rb', line 103 def disable = @enabled.value = false |
#enable
This method returns an undefined value.
Enables the subscription, allowing it to receive published payloads.
96 97 98 99 |
# File 'lib/farce/subscription.rb', line 96 def enable raise UnsubscribedError, "Cannot enable a subscription that has been unsubscribed" unless @subscribed.value @enabled.value = true end |
#enabled? ⇒ Boolean
Returns whether the subscription is currently enabled.
88 |
# File 'lib/farce/subscription.rb', line 88 def enabled? = @subscribed.value && @enabled.value |
#subscribed? ⇒ Boolean
Returns whether the subscription has not been permanently canceled.
91 |
# File 'lib/farce/subscription.rb', line 91 def subscribed? = @subscribed.value |
#unsubscribe ⇒ BasicObject? Also known as: cancel
Removes the subscription permanently, allowing it and the subscriber to be garbage collected.
107 108 109 110 111 112 |
# File 'lib/farce/subscription.rb', line 107 def unsubscribe return unless @subscribed.compare_and_set(true, false) @map.delete_pair(key, self) subscriber.seal if @callback subscriber end |