Sha256: 981a2ceaa6aca1d02d485c1c58032824d2beab0c2be3da250251b74d06b929e0

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'promise'

##
# A delayed-execution result, optimistcally evaluated in a new Thread.
# @example
#     x = future { sleep 5; 1 + 2 }
#     # do stuff...
#     y = x * 2     # => 6.  blocks unless 5 seconds has passed.
#
class Future < defined?(BasicObject) ? BasicObject : Object

  instance_methods.each { |m| undef_method m unless m =~ /__/ } unless defined?(BasicObject)

  ##
  # Create a new future
  #
  # @yield [] The block to evaluate optimistically
  # @return [Future]
  def initialize(block)
    @promise = promise &block
    @thread = ::Thread.new do
      @promise.force
    end
  end

  ##
  # The value of the future's evaluation.  Blocks until result available.
  #
  # @return [Any]
  def __force__
    @thread.join
    @promise
  end
  alias_method :force, :__force__

  ##
  # Does this promise support the given method?
  #
  # @param  [Symbol]
  # @return [true, false]
  def respond_to?(method)
    (method == :force) || (method == :__force__) || (__force__.respond_to?(method))
  end

  # @private
  def method_missing(method, *args, &block)
    @promise.send(method, *args, &block)
  end


end


module Kernel

  # Create a new future
  #
  # @example Evaluate an operation in another thread
  #     x = future { 3 + 3 }
  # @return       [Future]
  # @yield        [] A block to be optimistically evaluated in another thread
  # @yieldreturn  [Any] The return value of the block will be the evaluated value of the future. 
  def future(&block)
    Future.new(block)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
promise-0.2.0 lib/future.rb