lib/promise.rb in promise-0.2.1 vs lib/promise.rb in promise-0.3.0
- old
+ new
@@ -41,29 +41,47 @@
# Force the evaluation of this promise immediately
#
# @return [Object]
def __force__
@mutex.synchronize do
- if @result.equal?(NOT_SET) && @error.equal?(NOT_SET)
+ if !__forced?
begin
@result = @block.call
rescue ::Exception => e
@error = e
end
end
- end if @result.equal?(NOT_SET) && @error.equal?(NOT_SET)
+ end unless __forced?
# BasicObject won't send raise to Kernel
@error.equal?(NOT_SET) ? @result : ::Kernel.raise(@error)
end
alias_method :force, :__force__
##
# Does this promise support the given method?
#
# @param [Symbol]
- # @return [Boolean]
+ # @return [true, false]
def respond_to?(method)
- :force.equal?(method) || :__force__.equal?(method) || __force__.respond_to?(method)
+ :__forced?.equal?(method) || :force.equal?(method) || :__force__.equal?(method) || __force__.respond_to?(method)
+ end
+
+ ##
+ # Returns true if klass.equal?(Promise) or the underlying block returns an
+ # instance of the given klass
+ #
+ # @param [Class]
+ # @return [true, false]
+ def is_a?(klass)
+ klass.equal?(::Promise) || __force__.is_a?(klass)
+ end
+
+ ##
+ # Returns true if this promise has finished executing
+ #
+ # @return [true, false]
+ def __forced?
+ !(@result.equal?(NOT_SET) && @error.equal?(NOT_SET))
end
private
def method_missing(method, *args, &block)