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.


55
56
57
58
59
60
61
62
63
# File 'lib/lsync/event_timer.rb', line 55

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

Instance Method Details

#joinObject

Wait for the timeout to complete nicely.


92
93
94
95
96
# File 'lib/lsync/event_timer.rb', line 92

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.


66
67
68
69
70
# File 'lib/lsync/event_timer.rb', line 66

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