Class: Rvim::Lua::Loop::Scheduler
- Inherits:
-
Object
- Object
- Rvim::Lua::Loop::Scheduler
- Defined in:
- lib/rvim/lua/loop.rb
Instance Method Summary collapse
- #add(timeout_ms, repeat_ms, callback) ⇒ Object
- #close(id) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Scheduler
constructor
A new instance of Scheduler.
- #pump ⇒ Object
- #stop(id) ⇒ Object
Constructor Details
#initialize ⇒ Scheduler
Returns a new instance of Scheduler.
29 30 31 32 |
# File 'lib/rvim/lua/loop.rb', line 29 def initialize @timers = {} @next_id = 0 end |
Instance Method Details
#add(timeout_ms, repeat_ms, callback) ⇒ Object
34 35 36 37 38 |
# File 'lib/rvim/lua/loop.rb', line 34 def add(timeout_ms, repeat_ms, callback) id = (@next_id += 1) @timers[id] = Timer.new(id, monotonic + (timeout_ms / 1000.0), repeat_ms, callback, false) id end |
#close(id) ⇒ Object
45 46 47 |
# File 'lib/rvim/lua/loop.rb', line 45 def close(id) @timers.delete(id) end |
#empty? ⇒ Boolean
72 73 74 |
# File 'lib/rvim/lua/loop.rb', line 72 def empty? @timers.empty? end |
#pump ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rvim/lua/loop.rb', line 49 def pump now = monotonic fired = [] @timers.each_value do |t| next unless t.fire_now?(now) fired << t end fired.each do |t| begin t.callback&.call rescue StandardError # Swallow — a misbehaving timer shouldn't kill the loop. end if t.repeat_ms.to_i.positive? && !t.stopped t.deadline_at = now + (t.repeat_ms / 1000.0) else @timers.delete(t.id) end end fired.size end |
#stop(id) ⇒ Object
40 41 42 43 |
# File 'lib/rvim/lua/loop.rb', line 40 def stop(id) t = @timers[id] t.stopped = true if t end |