Sha256: 631b04d72cfbd00c7e297fffe47c4250c2e5129e9b69fa26753c0e9c72748dce

Contents?: true

Size: 834 Bytes

Versions: 3

Compression:

Stored size: 834 Bytes

Contents

class Knj::Thread2
	def initialize(args = {}, &block)
		@args = args
		@block = block if block_given?
		@callbacks = {}
	end
	
	def connect(signal, &block)
		@callbacks[signal] = [] if !@callbacks.has_key?(signal)
		@callbacks[signal] << block
	end
	
	def call(signal, *args)
		return false if !@callbacks.has_key?(signal)
		@callbacks[signal].each do |block|
			block.call(*args)
		end
		
		return {:count => count}
	end
	
	def run
		Thread.new do
			abort_on_exception = true
			call(:on_run)
			
			begin
				@block.call
			rescue SystemExit
				call(:on_exit)
				exit
			rescue Exception => e
				call(:on_error, e)
				
				if !@args.has_key?(:print_error) or @args[:print_error]
					print "Error: "
					puts e.inspect
					print "\n"
					puts e.backtrace
					print "\n\n"
				end
			end
			
			call(:on_done)
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
knjrbfw-0.0.7 lib/knj/thread2.rb
knjrbfw-0.0.4 lib/knj/thread2.rb
knjrbfw-0.0.3 lib/knj/thread2.rb