Class: Omnes::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/omnes/subscription.rb

Overview

Subscription to an event

An instance of it is returned on Bus subscription methods.

Usually, it isn't used directly beyond as a reference to unsubscribe.

bus = Omnes::Bus.new
bus.register(:foo)
subscription = bus.subscribe(:foo) { |_event| do_something }
bus.unsubscribe(subscription)

Constant Summary collapse

SINGLE_EVENT_MATCHER =
lambda do |subscribed, candidate|
  subscribed == candidate.omnes_event_name
end
ALL_EVENTS_MATCHER =
->(_candidate) { true }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher:, callback:, id:) ⇒ Subscription

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Subscription.



41
42
43
44
45
46
47
# File 'lib/omnes/subscription.rb', line 41

def initialize(matcher:, callback:, id:)
  raise Omnes::InvalidSubscriptionNameError.new(id: id) unless id.is_a?(Symbol)

  @matcher = matcher
  @callback = callback
  @id = id
end

Instance Attribute Details

#callbackObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/omnes/subscription.rb', line 38

def callback
  @callback
end

#idObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/omnes/subscription.rb', line 38

def id
  @id
end

#matcherObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/omnes/subscription.rb', line 38

def matcher
  @matcher
end

Class Method Details

.random_idObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/omnes/subscription.rb', line 28

def self.random_id
  SecureRandom.uuid.to_sym
end

.takes_publication_context?(callable) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


33
34
35
# File 'lib/omnes/subscription.rb', line 33

def self.takes_publication_context?(callable)
  callable.parameters.count == 2
end

Instance Method Details

#call(event, publication_context) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/omnes/subscription.rb', line 50

def call(event, publication_context)
  result = nil
  benchmark = Benchmark.measure do
    # work around Ruby not being able to tell remaining arity for a curried
    # function (or uncurrying), because we want to be able to create subscriber
    # adapters partially applying the subscriber instance
    result = begin
      @callback.(event, publication_context)
    rescue ArgumentError
      @callback.(event)
    end
  end

  Execution.new(subscription: self, result: result, benchmark: benchmark)
end

#matches?(candidate) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


67
68
69
# File 'lib/omnes/subscription.rb', line 67

def matches?(candidate)
  matcher.(candidate)
end

#subscriptionsObject

Returns self within a single-item array

This method can be helpful to act polymorphic to an array of subscriptions from an Omnes::Subscriber, usually for testing purposes.



75
76
77
# File 'lib/omnes/subscription.rb', line 75

def subscriptions
  [self]
end