Sha256: 0a453799d984e258bd2c29f531289fe7b811c0c220647ed234d9c3d1ff973374

Contents?: true

Size: 930 Bytes

Versions: 68

Compression:

Stored size: 930 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.key?(signal)
    @callbacks[signal] << block
  end
  
  def call(signal, *args)
    return false if !@callbacks.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 => e
        call(:on_error, e)
        
        if !@args.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

68 entries across 68 versions & 1 rubygems

Version Path
knjrbfw-0.0.116 lib/knj/thread2.rb
knjrbfw-0.0.115 lib/knj/thread2.rb
knjrbfw-0.0.114 lib/knj/thread2.rb
knjrbfw-0.0.113 lib/knj/thread2.rb
knjrbfw-0.0.111 lib/knj/thread2.rb
knjrbfw-0.0.110 lib/knj/thread2.rb
knjrbfw-0.0.109 lib/knj/thread2.rb
knjrbfw-0.0.108 lib/knj/thread2.rb
knjrbfw-0.0.107 lib/knj/thread2.rb
knjrbfw-0.0.105 lib/knj/thread2.rb
knjrbfw-0.0.104 lib/knj/thread2.rb
knjrbfw-0.0.103 lib/knj/thread2.rb
knjrbfw-0.0.102 lib/knj/thread2.rb
knjrbfw-0.0.101 lib/knj/thread2.rb
knjrbfw-0.0.100 lib/knj/thread2.rb
knjrbfw-0.0.99 lib/knj/thread2.rb
knjrbfw-0.0.98 lib/knj/thread2.rb
knjrbfw-0.0.97 lib/knj/thread2.rb
knjrbfw-0.0.96 lib/knj/thread2.rb
knjrbfw-0.0.95 lib/knj/thread2.rb