<commit_before>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
		class <<self; private :new; end

		@@listeners = Hash.new{|h, k| h[k] = []}
		
		# Public: Register an EventAggregator::Listener to recieve
		# 		  a specified message type
		#
		# listener - An EventAggregator::Listener which should recieve 
		# 			 the messages.
		# message_type - The message type to recieve. Can be anything except nil.
		# 				 Often it is preferable to use a string eg. "Message Type".
		#
		# Returns True if listener is added. #TODO: Verify this
		def self.register( listener, message_type, callback )
			@@listeners[message_type] << [listener, callback] unless ! (listener.class < EventAggregator::Listener) || @@listeners[message_type].include?(listener)
		end
		
		# Public: Unegister an EventAggregator::Listener to a 
		# 		  specified message type. The listener will no
		# 		  longer get messages of this type.
		# 
		# listener - The EventAggregator::Listener which should no longer recieve 
		# 			 the messages.
		# message_type - The message type to unregister for.
		#
		# Returns True if listener is no longer recieving this message type. #TODO: Verify this
		def self.unregister( listener, message_type )
			@@listeners[message_type].delete_if{|value| value[0] == listener} 
		end
		
		# Public: As Unregister, but will unregister listener from all message types.
		#
		# listener - The listener who should no longer get any messages at all, 
		# 			 regardless of type.
		def self.unregister_all( listener )
			@@listeners.each do |e|
				e[1].delete_if{|value| value[0] == listener}
			end
		end
		
		# Public: Will publish the specified message to all listeners
		# 		  who has registered for this message type.
		#
		# message - The message to be distributed to the listeners.
		def self.message_publish ( message )
			raise "Invalid message" unless message.is_a? EventAggregator::Message

			@@listeners[message.message_type].each do |l|
				EventAggregator::MessageJob.new.async.perform(message.data, l[1]) if l[1].respond_to? :call
				
				#l[1].call(message.data) if l[1].respond_to? :call
			end
		end
	end
end<commit_msg>Fixing up some un-ruby-like behaviour. Adding async and consistent_data to the message_publish. This helps combad possible deadlocks.<commit_after>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
		class <<self; private :new; end

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

		# Public: Register an EventAggregator::Listener to recieve
		# 		  a specified message type
		#
		# listener - An EventAggregator::Listener which should recieve
		# 			 the messages.
		# message_type - The message type to recieve. Can be anything except nil.
		# 				 Often it is preferable to use a string eg. "Message Type".
		#
		# Returns True if listener is added. #TODO: Verify this
		def self.register( listener, message_type, callback )
			@@listeners[message_type] << [listener, callback] unless ! (listener.class < EventAggregator::Listener) || @@listeners[message_type].include?(listener)
		end

		# Public: Unegister an EventAggregator::Listener to a
		# 		  specified message type. The listener will no
		# 		  longer get messages of this type.
		#
		# listener - The EventAggregator::Listener which should no longer recieve
		# 			 the messages.
		# message_type - The message type to unregister for.
		#
		# Returns True if listener is no longer recieving this message type. #TODO: Verify this
		def self.unregister( listener, message_type )
			@@listeners[message_type].delete_if{|value| value[0] == listener}
		end

		# Public: As Unregister, but will unregister listener from all message types.
		#
		# listener - The listener who should no longer get any messages at all,
		# 			 regardless of type.
		def self.unregister_all( listener )
			@@listeners.each do |e|
				e[1].delete_if{|value| value[0] == listener}
			end
		end

		# Public: Will publish the specified message to all listeners
		# 		  who has registered for this message type.
		#
		# message - The message to be distributed to the listeners.
		def self.message_publish ( message, async = true, consisten_data = false )
			raise "Invalid message" unless message.respond_to?(:message_type) && message.respond_to?(:data)
			
			@@listeners[message.message_type].each do |l|
				if l[1].respond_to? :call
					case [async, consisten_data]
					when [true, true]   then EventAggregator::MessageJob.new.async.perform(message.data, l[1])
					when [true, false]  then EventAggregator::MessageJob.new.async.perform(message.data.clone, l[1])
					when [false, true]  then EventAggregator::MessageJob.new.perform(message.data, l[1])
					when [false, false] then EventAggregator::MessageJob.new.perform(message.data.clone, l[1])
					end
				end
			end
		end
	end
end
{'fraction_non_alphanumeric': 0.1, 'fraction_numerical': 0.0036608863198458576, 'mean_word_length': 3.4904844290657437, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3932994', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1613, 'n_words': 532}