Sha256: c471c9d009f766ab3494594148e5828c1d4ff79ae064409b9d535cf5ee9e82b9

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

#require 'singleton'

module EventAggregator

	# Public: TODO: Could potentially turn this into a module.
	#
	# 	module OtherSingleton
	# 		@index = -1
	# 		@colors = %w{ red green blue }
	# 		def self.change
	# 			@colors[(@index += 1) % @colors.size]
	# 		end
	# 	end
	class Aggregator
		#include Singleton
		class <<self; private :new; end
		#TODO: Figure out how to do singleton pattern properly

		@@listeners = Hash.new{|h, k| h[k] = []}

		def self.register( listener, message_type )
			@@listeners[message_type] << listener unless ! (listener.class < EventAggregator::Listener) || @@listeners[message_type].include?(listener)
		end

		def self.unregister( listener, message_type )
			@@listeners[message_type].delete listener
		end
		def self.unregister_all( listener )
			@@listeners.each do |e|
				e[1].delete(listener)
			end
		end
		def self.message_publish ( message )
			raise "Invalid message" unless message.is_a? EventAggregator::Message

			@@listeners[message.message_type].each do |l|
				l.receive_message message
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
event_aggregator-0.0.2 lib/event_aggregator/aggregator.rb