Class: GReactor::TimedEvent

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

Overview

Every timed event is a member of the TimedEvent class and responds to it's methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval, repeat_limit = -1,, args = [], job = nil) ⇒ TimedEvent

Initialize a timed event.


12
13
14
15
16
17
18
# File 'lib/greactor/timers.rb', line 12

def initialize interval, repeat_limit = -1, args=[], job=nil
	@interval = interval
	@repeat_limit = repeat_limit ? repeat_limit.to_i : -1
	@job = job || (Proc.new { stop! })
	@next = Time.now + interval
	@args = args
end

Instance Attribute Details

#intervalObject

Sets/gets how often a timed event repeats, in seconds.


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

def interval
  @interval
end

#repeat_limitObject

Sets/gets how many times a timed event repeats. If set to false or -1, the timed event will repead until the application quits.


9
10
11
# File 'lib/greactor/timers.rb', line 9

def repeat_limit
  @repeat_limit
end

Instance Method Details

#done?(time = Time.now) ⇒ true, false

Returns true if the timer is finished.

If the timed event is due, this method will also add the event to the queue.

Returns:

  • (true, false)

31
32
33
34
35
36
37
38
# File 'lib/greactor/timers.rb', line 31

def done?(time = Time.now)
	return false unless @next <= time
	return true if @repeat_limit == 0
	@repeat_limit -= 1 if @repeat_limit.to_i > 0
	GReactor.queue @args, @job
	@next = time + @interval
	@repeat_limit == 0
end

#stop!GReactor::TimedEvent

stops a timed event.

Returns:


22
23
24
25
# File 'lib/greactor/timers.rb', line 22

def stop!
	@repeat_limit = 0
	self
end