Sha256: eec8ecfeeaf862885185bdb8051c3dbb47175fb4a9277f52f485b6e7830b0dbd

Contents?: true

Size: 994 Bytes

Versions: 1

Compression:

Stored size: 994 Bytes

Contents

module God
  class EventHandler
    @@actions = {}
    @@handler = nil
    
    def self.handler=(value)
      @@handler = value
    end
    
    def self.register(pid, event, &block)
      @@actions[pid] ||= {}
      @@actions[pid][event] = block
      @@handler.register_process(pid, @@actions[pid].keys)
    end
    
    def self.deregister(pid, event=nil)
      if watching_pid? pid
        if event.nil?
          @@actions.delete(pid)
          @@handler.register_process(pid, []) if system("kill -0 #{pid} &> /dev/null")
        else
          @@actions[pid].delete(event)
          @@handler.register_process(pid, @@actions[pid].keys) if system("kill -0 #{pid} &> /dev/null")
        end
      end
    end
    
    def self.call(pid, event)
      @@actions[pid][event].call
    end
    
    def self.watching_pid?(pid)
      @@actions[pid]
    end
    
    def self.start
      Thread.new do
        loop do
          @@handler.handle_events
        end
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
god-0.2.0 lib/god/event_handler.rb