Class: EventLoop::Loop::Timer
- Inherits:
-
Object
- Object
- EventLoop::Loop::Timer
- Defined in:
- lib/event_loop/loop.rb
Instance Attribute Summary collapse
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#last_fired_at ⇒ Object
readonly
Returns the value of attribute last_fired_at.
-
#next_fire_time ⇒ Object
readonly
Returns the value of attribute next_fire_time.
Instance Method Summary collapse
- #advance(amount) ⇒ Object
- #calculate_next_fire_time ⇒ Object
- #due?(now = Time.now) ⇒ Boolean
- #finished? ⇒ Boolean
-
#initialize(interval, event, repeat = false) ⇒ Timer
constructor
A new instance of Timer.
- #last_interval_start ⇒ Object
- #reschedule ⇒ Object
- #set_fired_time ⇒ Object
Constructor Details
#initialize(interval, event, repeat = false) ⇒ Timer
Returns a new instance of Timer.
7 8 9 10 11 12 13 14 |
# File 'lib/event_loop/loop.rb', line 7 def initialize(interval, event, repeat=false) @interval = interval @event = event @repeat = repeat @started_at = Time.now @last_fired_at = nil reschedule end |
Instance Attribute Details
#event ⇒ Object (readonly)
Returns the value of attribute event.
5 6 7 |
# File 'lib/event_loop/loop.rb', line 5 def event @event end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
5 6 7 |
# File 'lib/event_loop/loop.rb', line 5 def interval @interval end |
#last_fired_at ⇒ Object (readonly)
Returns the value of attribute last_fired_at.
5 6 7 |
# File 'lib/event_loop/loop.rb', line 5 def last_fired_at @last_fired_at end |
#next_fire_time ⇒ Object (readonly)
Returns the value of attribute next_fire_time.
5 6 7 |
# File 'lib/event_loop/loop.rb', line 5 def next_fire_time @next_fire_time end |
Instance Method Details
#advance(amount) ⇒ Object
20 21 22 |
# File 'lib/event_loop/loop.rb', line 20 def advance(amount) @next_fire_time -= amount end |
#calculate_next_fire_time ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/event_loop/loop.rb', line 28 def calculate_next_fire_time now = Time.now return now if @interval == 0 fire_time = @last_fired_at || now while fire_time <= now fire_time += @interval end fire_time end |
#due?(now = Time.now) ⇒ Boolean
42 43 44 |
# File 'lib/event_loop/loop.rb', line 42 def due?(now=Time.now) now >= @next_fire_time end |
#finished? ⇒ Boolean
46 47 48 |
# File 'lib/event_loop/loop.rb', line 46 def finished? !@repeat && @last_fired_at end |
#last_interval_start ⇒ Object
24 25 26 |
# File 'lib/event_loop/loop.rb', line 24 def last_interval_start @last_fired_at || @started_at end |
#reschedule ⇒ Object
16 17 18 |
# File 'lib/event_loop/loop.rb', line 16 def reschedule @next_fire_time = calculate_next_fire_time end |
#set_fired_time ⇒ Object
38 39 40 |
# File 'lib/event_loop/loop.rb', line 38 def set_fired_time @last_fired_at = Time.now end |