Sha256: 308ba122bfb2695c140d4e10e3b8de69114e70ebd270aa2c017737f05a24a61e

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

# Promise mechanism shamelessly stolen and modified from
# https://github.com/bhuga/promising-future/blob/master/lib/promise.rb

class Marty::PromiseProxy < BasicObject
  NOT_SET = ::Object.new.freeze
  METH_SET = ::Set[:marshal_load, :marshal_dump, :force, :__force__]

  instance_methods.each {|m| undef_method m unless m =~ /^(__.*|object_id)$/}

  def initialize(promise_id, timeout, attr=nil)
    marshal_load([promise_id, timeout, attr])
  end

  def marshal_dump
    [@promise.id, @timeout, @attr]
  end

  def marshal_load(args)
    promise_id, @timeout, @attr = args
    @promise = ::Marty::Promise.find(promise_id)
    @mutex   = ::Mutex.new
    @result  = NOT_SET
  end

  def __promise_id__
    @promise.id
  end

  ##
  # Force the evaluation of this promise immediately
  #
  # @return [Object]
  def __force__
    if @result.equal?(NOT_SET)
      @mutex.synchronize do
        if @result.equal?(NOT_SET)
          begin
            @result = @promise.wait_for_result(@timeout)
            @result = @result[@attr] if @attr && !@result["error"]
          rescue ::Exception => exc
            @result = ::Delorean::Engine.grok_runtime_exception(exc)
          end
        end
      end
    end

    # FIXME: the logic for shape of exceptions from Delorean is spread
    # all over the place.
    @result.is_a?(::Hash) &&
      @result["error"] ? ::Kernel.raise(@result["error"]) : @result
  end

  alias_method :force, :__force__

  ##
  # Does this promise support the given method?
  #
  # @param  [Symbol]
  # @return [Boolean]
  def respond_to?(method, include_all=false)
    METH_SET.member?(method) || __force__.respond_to?(method, include_all)
  end

  private

  def method_missing(method, *args, &block)
    __force__.__send__(method, *args, &block)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
marty-0.5.15 lib/marty/promise_proxy.rb~
marty-0.5.14 lib/marty/promise_proxy.rb~
marty-0.5.13 lib/marty/promise_proxy.rb~
marty-0.5.12 lib/marty/promise_proxy.rb~