Async::AwaitSourceAsyncAwait

module Await

Provide a way to wrap methods so that they can be run synchronously or asynchronously in an event loop.

Nested

Definitions

def self.included(klass)

A hook that is called when the module is included in a class in order to provide the class with the methods defined in this module.

Implementation

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

def sync(name)

Wrap the method with the given name in a block that will run the method synchronously in an event loop.

Signature

parameter name Symbol

The name of the method to wrap.

Implementation

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
	
	return name
end

def async(name)

Wrap the method with the given name in a block that will run the method asynchronously in an event loop.

Signature

parameter name Symbol

The name of the method to wrap.

Implementation

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
	
	return name
end