Sha256: b1add9efbc9ab49e2bbe1e6f4e7d2c2cbebba8e26618cbb29208ee67fcb4d7a9

Contents?: true

Size: 997 Bytes

Versions: 38

Compression:

Stored size: 997 Bytes

Contents

#!/usr/bin/env ruby

require_relative '../lib/async'

module Async::Methods
	def sleep(*args)
		Async::Task.current.sleep(*args)
	end

	def async(name)
		original_method = self.method(name)
		
		define_method(name) do |*args|
			Async::Reactor.run do |task|
				original_method.call(*args)
			end
		end
	end

	def await(&block)
		block.call.wait
	end
	
	def barrier!
		Async::Task.current.children.each(&:wait)
	end
end

include Async::Methods

async def count_chickens(area_name)
	3.times do |i|
		sleep rand
		
		puts "Found a chicken in the #{area_name}!"
	end
end

async def find_chicken(areas)
	puts "Searching for chicken..."
	
	sleep rand * 5
	
	return areas.sample
end

async def count_all_chckens
	# These methods all run at the same time.
	count_chickens("garden")
	count_chickens("house")
	count_chickens("tree")
	
	# Wait for all previous async work to complete...
	barrier!
	
	puts "There was a chicken in the #{find_chicken(["garden", "house", "tree"]).wait}"
end

count_all_chckens

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
async-1.24.1 examples/async_method.rb
async-1.24.0 examples/async_method.rb
async-1.23.0 examples/async_method.rb
async-1.22.2 examples/async_method.rb
async-1.22.1 examples/async_method.rb
async-1.22.0 examples/async_method.rb
async-1.21.0 examples/async_method.rb
async-1.20.1 examples/async_method.rb
async-1.20.0 examples/async_method.rb
async-1.19.0 examples/async_method.rb
async-1.18.0 examples/async_method.rb
async-1.17.1 examples/async_method.rb
async-1.17.0 examples/async_method.rb
async-1.16.0 examples/async_method.rb
async-1.15.5 examples/async_method.rb
async-1.15.4 examples/async_method.rb
async-1.15.3 examples/async_method.rb
async-1.15.2 examples/async_method.rb
async-1.15.1 examples/async_method.rb
async-1.15.0 examples/async_method.rb