Class: LaunchDarkly::Impl::Broadcaster Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/broadcaster.rb

Overview

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

A generic mechanism for registering event listeners and broadcasting events to them.

The SDK maintains an instance of this for each available type of listener (flag change, data store status, etc.). They are all intended to share a single executor service; notifications are submitted individually to this service for each listener.

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(executor, logger) ⇒ Broadcaster

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

Since:

  • 5.5.0



16
17
18
19
20
# File 'lib/ldclient-rb/impl/broadcaster.rb', line 16

def initialize(executor, logger)
  @listeners = Concurrent::Set.new
  @executor = executor
  @logger = logger
end

Instance Method Details

#add_listener(listener) ⇒ 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.

Register a listener to this broadcaster.

Parameters:

  • listener (#update)

Since:

  • 5.5.0



27
28
29
30
31
32
33
34
# File 'lib/ldclient-rb/impl/broadcaster.rb', line 27

def add_listener(listener)
  unless listener.respond_to? :update
    logger.warn("listener (#{listener.class}) does not respond to :update method. ignoring as registered listener")
    return
  end

  listeners.add(listener)
end

#broadcast(event) ⇒ 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.

Broadcast the provided event to all registered listeners.

Each listener will be notified using the broadcasters executor. This method is non-blocking.

Since:

  • 5.5.0



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ldclient-rb/impl/broadcaster.rb', line 53

def broadcast(event)
  listeners.each do |listener|
    executor.post do
      begin
        listener.update(event)
      rescue StandardError => e
        logger.error("listener (#{listener.class}) raised exception (#{e}) processing event (#{event.class})")
      end
    end
  end
end

#has_listeners?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)

Since:

  • 5.5.0



43
44
45
# File 'lib/ldclient-rb/impl/broadcaster.rb', line 43

def has_listeners?
  !listeners.empty?
end

#remove_listener(listener) ⇒ 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.

Removes a registered listener from this broadcaster.

Since:

  • 5.5.0



39
40
41
# File 'lib/ldclient-rb/impl/broadcaster.rb', line 39

def remove_listener(listener)
  listeners.delete(listener)
end