Sha256: 75c32ef9f68a48a4a97f885a85aea5b2188513de2ef442bfa9f523ebbf289cd1

Contents?: true

Size: 1.33 KB

Versions: 10

Compression:

Stored size: 1.33 KB

Contents

module Plezi
	module EventMachine
		module_function

		QUEUE = []
		QUEUE_LOCKER = Mutex.new

		def queue args, job
			raise "Job missing" unless job
			QUEUE_LOCKER.synchronize { QUEUE << [job, args]}
			true
		end

		def do_job
			job, args = QUEUE_LOCKER.synchronize { QUEUE.shift }
			job ? (job.call(*args) || true) : false
		end

		def queue_empty?
			QUEUE.empty?
		end

	end

	module_function

	# Accepts a block and runs it asynchronously. This method runs asynchronously and returns immediately.
	#
	# use:
	#
	#      Plezi.run_async(arg1, arg2, arg3 ...) { |arg1, arg2, arg3...| do_something }
	#
	# the block will be run within the current context, allowing access to current methods and variables.
	def run_async *args, &block
		EventMachine.queue args, block
	end

	# This method runs asynchronously and returns immediately.
	#
	# This method accepts:
	# object:: an object who's method will be called.
	# method:: the method's name to be called. type: Symbol.
	# *args:: any arguments to be passed to the method.
	#
	# This method also accepts an optional block which will be run with the method's returned value within the existing context.
	#
	def callback object, method, *args, &block
		block ? EventMachine.queue( args, (proc { |ar| block.call( object.method(method).call(*ar) ) }) ) : EventMachine.queue(args, object.method(method) )
	end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
plezi-0.9.2 lib/plezi/eventmachine/queue.rb
plezi-0.9.1 lib/plezi/eventmachine/queue.rb
plezi-0.9.0 lib/plezi/eventmachine/queue.rb
plezi-0.8.7 lib/plezi/eventmachine/queue.rb
plezi-0.8.6 lib/plezi/eventmachine/queue.rb
plezi-0.8.5 lib/plezi/eventmachine/queue.rb
plezi-0.8.4 lib/plezi/eventmachine/queue.rb
plezi-0.8.3 lib/plezi/eventmachine/queue.rb
plezi-0.8.2 lib/plezi/eventmachine/queue.rb
plezi-0.8.1 lib/plezi/eventmachine/queue.rb