Class: EventLoop::Loop::Timer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#eventObject (readonly)

Returns the value of attribute event.



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

def event
  @event
end

#intervalObject (readonly)

Returns the value of attribute interval.



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

def interval
  @interval
end

#last_fired_atObject (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_timeObject (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_timeObject



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

Returns:

  • (Boolean)


42
43
44
# File 'lib/event_loop/loop.rb', line 42

def due?(now=Time.now)
  now >= @next_fire_time
end

#finished?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/event_loop/loop.rb', line 46

def finished?
  !@repeat && @last_fired_at
end

#last_interval_startObject



24
25
26
# File 'lib/event_loop/loop.rb', line 24

def last_interval_start
  @last_fired_at || @started_at
end

#rescheduleObject



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

def reschedule
  @next_fire_time = calculate_next_fire_time
end

#set_fired_timeObject



38
39
40
# File 'lib/event_loop/loop.rb', line 38

def set_fired_time
  @last_fired_at = Time.now
end