Sha256: a4146c469c694df60a99282a943d98c6c61726e8a71f4d5d0499d80e00f174eb

Contents?: true

Size: 872 Bytes

Versions: 1

Compression:

Stored size: 872 Bytes

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
  
  instance_methods.each { |m| undef_method m unless m =~ /__/ }

  ##
  # @param [Proc] block
  # @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

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


end


module Kernel

  # Create a new future 
  # @example
  #     x = future { 3 + 3 }
  def future(&block)
    Future.new(block)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
promise-0.1.0 lib/future.rb