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.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lsync/event_timer.rb', line 8

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.



21
22
23
# File 'lib/lsync/event_timer.rb', line 21

def thread
  @thread
end

Instance Method Details

#cancel!Object

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



24
25
26
27
# File 'lib/lsync/event_timer.rb', line 24

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