Class: EventLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/event_loop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventLoop

Returns a new instance of EventLoop.



8
9
10
11
# File 'lib/event_loop.rb', line 8

def initialize
  @queue = []
  @item = {}
end

Instance Attribute Details

#itemObject

Returns the value of attribute item.



6
7
8
# File 'lib/event_loop.rb', line 6

def item
  @item
end

Instance Method Details

#call_later(fn, timeout, callback = nil) ⇒ Object



17
18
19
# File 'lib/event_loop.rb', line 17

def call_later(fn, timeout, callback=nil)
  @queue << {:fn => fn, :time => Time.now.to_f + timeout, :callback => callback}
end

#create_task(fn) ⇒ Object Also known as: add_task



13
14
15
# File 'lib/event_loop.rb', line 13

def create_task(fn)
  self.call_later(fn, 0)
end

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/event_loop.rb', line 21

def start
  while @queue.size > 0
    @queue.sort_by!{|i| i[:time].to_i}
    @item = @queue[0]
    ctime = Time.now.to_f
    if ctime >= @item[:time]
      @queue.delete_at 0
      @item[:fn].resume
      self.call_later(@item[:callback], 0) if @item[:callback] and not @item[:fn].alive?
    end

  end
end

#stopObject



35
36
37
# File 'lib/event_loop.rb', line 35

def stop

end