class IO::Event::Timers

def after(offset, &block)

@parameter offset [#to_f] The time offset from the current time at which the block should be called.
Schedule a block to be called after a specific time offset, relative to the current time as returned by {#now}.
def after(offset, &block)
	schedule(self.now + offset.to_f, block)
end

def fire(now = self.now)

def fire(now = self.now)
	# Flush scheduled timers into the heap:
	flush!
	
	# Get the earliest timer:
	while handle = @heap.peek
		if handle.cancelled?
			@heap.pop
		elsif handle.time <= now
			# Remove the earliest timer from the heap:
			@heap.pop
			
			# Call the block:
			handle.call(now)
		else
			break
		end
	end
end

def flush!

def flush!
dle = @scheduled.pop
sh(handle) unless handle.cancelled?

def initialize

def initialize
	@heap = PriorityHeap.new
	@scheduled = []
end

def now

def now
	::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end

def schedule(time, block)

@parameter time [Float] The time at which the block should be called, relative to {#now}.
Schedule a block to be called at a specific time in the future.
def schedule(time, block)
	handle = Handle.new(time, block)
	
	@scheduled << handle
	
	return handle
end

def size

def size
	flush!
	
	return @heap.size
end

def wait_interval(now = self.now)

def wait_interval(now = self.now)
	flush!
	
	while handle = @heap.peek
		if handle.cancelled?
			@heap.pop
		else
			return handle.time - now
		end
	end
end