Class: LSync::Timeout

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

Overview

Manages a callback that will be executed after a set duration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout, &block) ⇒ Timeout

Returns a new instance of Timeout.


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lsync/event_timer.rb', line 27

def initialize(timeout, &block)
	@cancelled = false
	
	@thread = Thread.new do
		sleep timeout
		
		unless @cancelled
			yield
		end
	end
end

Instance Attribute Details

#threadObject (readonly)

The thread on which the timeout is being waited.


40
41
42
# File 'lib/lsync/event_timer.rb', line 40

def thread
  @thread
end

Instance Method Details

#cancel!Object

Cancel the timeout if possible and ensure that the callback is not executed.


43
44
45
46
# File 'lib/lsync/event_timer.rb', line 43

def cancel!
	@cancelled = true
	@thread.exit
end