Sha256: 57daf6ef7fe769bc4b10a494698d004dc1c3fab6f6274473d937b26f37c85759

Contents?: true

Size: 865 Bytes

Versions: 32

Compression:

Stored size: 865 Bytes

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2022, by Samuel Williams.
# Copyright, 2020, by Brian Morearty.

require_relative "../async/reactor"

# Extensions to all Ruby objects.
module Kernel
	# Run the given block of code synchronously, but within a reactor if not already in one.
	#
	# @yields {|task| ...} The block that will execute asynchronously.
	# 	@parameter task [Async::Task] The task that is executing the given block.
	#
	# @public Since `stable-v1`.
	# @asynchronous Will block until given block completes executing.
	def Sync(&block)
		if task = ::Async::Task.current?
			yield task
		else
			# This calls Fiber.set_scheduler(self):
			reactor = Async::Reactor.new
			
			begin
				return reactor.run(finished: ::Async::Condition.new, &block).wait
			ensure
				Fiber.set_scheduler(nil)
			end
		end
	end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
async-2.15.3 lib/kernel/sync.rb
async-2.15.2 lib/kernel/sync.rb
async-2.15.1 lib/kernel/sync.rb
async-2.15.0 lib/kernel/sync.rb
async-2.14.2 lib/kernel/sync.rb
async-2.14.1 lib/kernel/sync.rb
async-2.14.0 lib/kernel/sync.rb
async-2.13.0 lib/kernel/sync.rb
async-2.12.1 lib/kernel/sync.rb
async-2.12.0 lib/kernel/sync.rb
async-2.11.0 lib/kernel/sync.rb
async-2.10.2 lib/kernel/sync.rb
async-2.10.1 lib/kernel/sync.rb
async-2.10.0 lib/kernel/sync.rb
async-2.9.0 lib/kernel/sync.rb
async-2.8.2 lib/kernel/sync.rb
async-2.8.1 lib/kernel/sync.rb
async-2.8.0 lib/kernel/sync.rb
async-2.7.0 lib/kernel/sync.rb
async-2.6.5 lib/kernel/sync.rb