Class: GRI::Loop
Instance Attribute Summary collapse
-
#collectors ⇒ Object
readonly
Returns the value of attribute collectors.
Instance Method Summary collapse
- #attach(collector) ⇒ Object
- #detach(collector) ⇒ Object
- #has_active_watchers? ⇒ Boolean
-
#initialize ⇒ Loop
constructor
A new instance of Loop.
- #next_tick(&cb) ⇒ Object
- #on_detach(&on_detach) ⇒ Object
- #run ⇒ Object
- #run_once ⇒ Object
- #watch(io, flag, to, handler) ⇒ Object
Constructor Details
#initialize ⇒ Loop
Returns a new instance of Loop.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/gri/loop.rb', line 5 def initialize @collectors = {} @rs = [] @ws = [] @handlers = {} @times = {} @tos = {} @pt = Time.now @procs = [] end |
Instance Attribute Details
#collectors ⇒ Object (readonly)
Returns the value of attribute collectors.
3 4 5 |
# File 'lib/gri/loop.rb', line 3 def collectors @collectors end |
Instance Method Details
#attach(collector) ⇒ Object
17 18 19 20 |
# File 'lib/gri/loop.rb', line 17 def attach collector @collectors[collector] = true collector.attach self end |
#detach(collector) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gri/loop.rb', line 22 def detach collector collector.on_detach @collectors.delete collector del_ios = @handlers.select {|k, v| v == collector} del_ios.each {|io, handler| @handlers.delete io @times.delete io @tos.delete io @rs.delete io @ws.delete io } @on_detach.call end |
#has_active_watchers? ⇒ Boolean
60 61 62 |
# File 'lib/gri/loop.rb', line 60 def has_active_watchers? !(@rs.empty? and @ws.empty? and @collectors.empty? and @procs.empty?) end |
#next_tick(&cb) ⇒ Object
40 41 42 |
# File 'lib/gri/loop.rb', line 40 def next_tick &cb @procs.push cb end |
#on_detach(&on_detach) ⇒ Object
36 37 38 |
# File 'lib/gri/loop.rb', line 36 def on_detach &on_detach @on_detach = on_detach end |
#run ⇒ Object
64 65 66 67 68 |
# File 'lib/gri/loop.rb', line 64 def run while has_active_watchers? run_once end end |
#run_once ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/gri/loop.rb', line 70 def run_once while (cb = @procs.shift) cb.call end if (a = IO.select(@rs, @ws, nil, 1)) rs, ws = a for io in rs @rs.delete io if (h = @handlers[io]) h.on_readable io end end for io in ws @ws.delete io if (h = @handlers[io]) h.on_writable io end end end if @pt.sec != (now = Time.now).sec for io in @rs + @ws if (t = @times[io]) and (to = @tos[io]) > 0 and (now - t >= to) @rs.delete io @ws.delete io if (h = @handlers[io]) h.retry end end end for c in @collectors.keys if c.attached_at and c.attached_at + c.timeout < now c.on_timeout detach c end end @pt = now end end |
#watch(io, flag, to, handler) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/gri/loop.rb', line 44 def watch io, flag, to, handler now = Time.now @handlers[io] = handler @times[io] = now @tos[io] = to case flag when :r @rs.push io when :w @ws.push io when :rw @rs.push io @ws.push io end end |