Sha256: 1dfad255a337fca4742f253913d41ca89ef5ebde395e76a19abd3df5f40476dd

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

# -*- encoding: utf-8 -*-

# Manages receipt handling for a {Stomper::Connection}
class Stomper::ReceiptManager
  # Creates a new receipt handler for the supplied {Stomper::Connection connection}
  # @param [Stomper::Connection] connection
  def initialize(connection)
    @mon = ::Monitor.new
    @callbacks = {}
    connection.before_disconnect do |d, con|
      @close_on = d[:receipt] if d[:receipt]
    end
    connection.on_receipt do |r, con|
      dispatch(r)
      connection.close if r[:'receipt-id'] == @close_on
    end
  end
  
  # Adds a callback handler for a RECEIPT frame that matches the supplied
  # receipt ID.
  # @param [String] r_id ID of the receipt to match
  # @param [Proc] callback Proc to invoke when a matching RECEIPT frame is
  #   received from the broker.
  # @return [self]
  def add(r_id, callback)
    @mon.synchronize { @callbacks[r_id] = callback }
    self
  end
  
  # Remove all receipt handlers.
  def clear
    @mon.synchronize { @callbacks.clear }
  end
  
  private
  def dispatch(receipt)
    cb = @mon.synchronize { @callbacks.delete(receipt[:'receipt-id']) }
    cb && cb.call(receipt)
    self
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stomper-2.0.6 lib/stomper/receipt_manager.rb
stomper-2.0.5 lib/stomper/receipt_manager.rb
stomper-2.0.4 lib/stomper/receipt_manager.rb
stomper-2.0.3 lib/stomper/receipt_manager.rb
stomper-2.0.2 lib/stomper/receipt_manager.rb