Sha256: bdec161426f841c0a66ea67686ee703d8742e4af3c56b6d2085b05082984acf0

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

#
# implementation of the service broker which only dispatches
# messages to internally registered listeners
#
#
require 'set'
module Appcelerator
	class InmemoryServiceBroker
		include Singleton

		def initialize 
			@listeners={}
		end	  	
	
	  	def register_listener(msgtype,listener)      
        begin
          @listeners[msgtype] << listener
        rescue
          @listeners[msgtype] = Set.new << listener
        end
      end
	  	
	  	def unregister_listener(msgtype,listener)
	  		array = @listeners[msgtype]
	  		array.delete_if { |a| a == listener } if array
	  	end
	  	
      def clear_listeners
        @listeners.clear
      end
	  
	  	def send (req,type,obj)
			array = @listeners[type]
			if array
			  array.each do |listener|
				begin
				  listener.call(req,type,obj)
				rescue => error
				  puts $!
				  raise error
				end
			  end
			  return true
			end
			false
	  	end
    
    def diagnostics
      all = []
      @listeners.each do |k,v|
        v.each do |proc|
          all << proc.to_s
        end
      end
      all.join "\n"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appcelerator-2.0.1.1 lib/appcelerator/servicebroker/inmemory_service_broker.rb