Class: LSync::EventTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/lsync/event_timer.rb

Overview

The EventTimer provides a simple time based callback mechanism in which events can be aggregated. If the timer is triggered once, it will take at most max time for the callback to be triggered.

Instance Method Summary collapse

Constructor Details

#initialize(max, &block) ⇒ EventTimer

Times are measured in seconds. Min specifies the minimum duration between callback invocations. Max specifies the maximum duration between callback invocations.



36
37
38
39
40
41
42
43
44
# File 'lib/lsync/event_timer.rb', line 36

def initialize(max, &block)
	@max = max
	
	@fired = nil
	@timeout = nil
	
	@callback = Proc.new(&block)
	@processing = Mutex.new
end

Instance Method Details

#joinObject

Wait for the timeout to complete nicely.



73
74
75
76
77
# File 'lib/lsync/event_timer.rb', line 73

def join
	if @timeout
		@timeout.thread.join
	end
end

#trigger!Object

Trigger the event timer such that within the specified time, the callback will be fired.



47
48
49
50
51
# File 'lib/lsync/event_timer.rb', line 47

def trigger!
	unless @timeout
		@timeout = Timeout.new(@max) { fire! }
	end
end