[3365] MacRuby/trunk/lib/dispatch/futures.rb
Revision: 3365 http://trac.macosforge.org/projects/ruby/changeset/3365 Author: ernest.prabhakar@gmail.com Date: 2010-01-29 15:40:06 -0800 (Fri, 29 Jan 2010) Log Message: ----------- Redid Futures to use Dispatch helper methods Modified Paths: -------------- MacRuby/trunk/lib/dispatch/futures.rb Modified: MacRuby/trunk/lib/dispatch/futures.rb =================================================================== --- MacRuby/trunk/lib/dispatch/futures.rb 2010-01-29 23:40:04 UTC (rev 3364) +++ MacRuby/trunk/lib/dispatch/futures.rb 2010-01-29 23:40:06 UTC (rev 3365) @@ -4,21 +4,18 @@ # Create a future that asynchronously dispatches the block # to a concurrent queue of the specified (optional) +priority+ def initialize(priority=nil, &block) - @group = Group.new @value = nil - Dispatch.group(@group, priority) { @value = block.call } + @group = Dispatch.fork(priority) { @value = block.call } end # Waits for the computation to finish, then returns the value # Duck-typed to lambda.call(void) - def call() - @group.wait - @value - end - - # Passes the value to the +callback+ block when it is available - # Duck-typed to group.notify(&block) - def notify(&callback) + # If a block is passed, invoke that asynchronously with the final value + def call(&callback) + if not block_given? + @group.wait + return @value + end @group.notify { callback.call(@value) } end end
participants (1)
-
source_changes@macosforge.org