class Mon::Monad::FuturePromise

FuturePromise represents an asynchronous operation that is still inflight, or complete.

Public Class Methods

perform(fun, args) click to toggle source

Equivalent to calling Future::eventually { … }

# File lib/monads/future.rb, line 79
def self::perform(fun, args)
  if args.length == 1 and args[0].is_a? FuturePromise
    # We're waiting on something, but we don't want to block
    FuturePromise.new(Thread.new { fun.call(args[0].unwrap) })
  else
    FuturePromise.new(Thread.new(args) { |args| fun.call(*args) })
  end
end

Protected Class Methods

new(thread) click to toggle source
# File lib/monads/future.rb, line 74
def initialize(thread)
  @thread = thread
end

Public Instance Methods

bind(&fun) click to toggle source

Take a block, and apply it to the value asynchronously, returing a future to represent the result

# File lib/monads/future.rb, line 90
def bind &fun
  FuturePromise::perform(fun, [self])
end
finalize() click to toggle source

Block until the operation completes, then return the result (wrapped as a FutureComplete)

# File lib/monads/future.rb, line 100
def finalize
  value = @thread.value
  FutureComplete[(value.is_a? Future) ? value.unwrap : value]
end
pending?() click to toggle source

Are we still inflight?

# File lib/monads/future.rb, line 106
def pending?
  @thread.alive?
end
to_s() click to toggle source
# File lib/monads/future.rb, line 110
def to_s
  "FuturePromise[#{ @thread }]"
end
unwrap() click to toggle source

Block, then return the result (unwrapped)

# File lib/monads/future.rb, line 95
def unwrap
  @thread.value
end