Module: Async::Await

Defined in:
lib/async/await.rb,
lib/async/await/version.rb,
lib/async/await/enumerable.rb

Defined Under Namespace

Modules: Enumerable

Constant Summary collapse

VERSION =
"0.7.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



10
11
12
# File 'lib/async/await.rb', line 10

def self.included(klass)
	klass.extend(self)
end

Instance Method Details

#async(name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/async/await.rb', line 30

def async(name)
	original_method = instance_method(name)
	
	remove_method(name)
	
	define_method(name) do |*arguments, &block|
		Async::Reactor.run do |task|
			original_method.bind(self).call(*arguments, &block)
		end
	end
end

#sync(name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/async/await.rb', line 14

def sync(name)
	original_method = instance_method(name)
	
	remove_method(name)
	
	define_method(name) do |*arguments, &block|
		if task = Task.current?
			original_method.bind(self).call(*arguments, &block)
		else
			Async::Reactor.run do
				original_method.bind(self).call(*arguments, &block)
			end.wait
		end
	end
end