class MMPlayer::Player::Messenger

Handle sending MPlayer messages

Constants

FREQUENCY_LIMIT

Public Class Methods

new() click to toggle source
# File lib/mmplayer/player/messenger.rb, line 10
def initialize
  @messages = []
end

Public Instance Methods

send_message(&block) click to toggle source

Send mplayer a message asynch @return [Hash, nil]

# File lib/mmplayer/player/messenger.rb, line 16
def send_message(&block)
  timestamp = Time.now.to_f
  # Throttled messages are disregarded
  if @messages.empty? || !throttle?(timestamp, @messages.last[:timestamp])
    thread = ::MMPlayer::Thread.new(&block)
    record_message(thread, timestamp)
  end
end

Private Instance Methods

record_message(thread, timestamp) click to toggle source

Record that a message has been sent @param [Thread] thread @param [Float] timestamp @return [Hash]

# File lib/mmplayer/player/messenger.rb, line 39
def record_message(thread, timestamp)
  message = {
    :thread => thread,
    :timestamp => timestamp
  }
  @messages << message
  message
end
throttle?(timestamp, last_timestamp) click to toggle source

Should adding a message be throttled for the given timestamp? @param [Float] timestamp @param [Float] last_timestamp @return [Boolean]

# File lib/mmplayer/player/messenger.rb, line 31
def throttle?(timestamp, last_timestamp)
  timestamp - last_timestamp <= FREQUENCY_LIMIT
end