Class: Farce::Subscription

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Internal::Noncopyable

#duplicable?

Instance Attribute Details

#publisher ⇒ Farce::Abstract::Publisher (readonly)

Returns the publisher associated with this subscription.

Returns:



57
58
59
# File 'lib/farce/subscription.rb', line 57

def publisher
  @publisher
end

#subscriber ⇒ BasicObject (readonly)

Returns the subscriber associated with this subscription.

Returns:

  • (BasicObject) —

    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| ... }

Note:

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.

Examples:

Farce::Subscription.define(MyInbox) { |payload| subscriber.push(payload) }

Parameters:

  • classes (Array<Class>) —

    the subscriber classes to support

Yield Parameters:

  • payload (BasicObject) —

    the published payload

Raises:

  • (LocalJumpError)

Yield Receiver:



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.

Raises:



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.

Returns:

  • (Boolean) —

    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.

Returns:

  • (Boolean) —

    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.

Returns:

  • (BasicObject, nil) —

    the subscriber, or nil if already unsubscribed



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