Class: Mon::Monad::FuturePromise
- Defined in:
- lib/monads/future.rb
Overview
FuturePromise represents an asynchronous operation that is still inflight, or complete.
Class Method Summary collapse
-
.perform(fun, args) ⇒ Object
Equivalent to calling Future::eventually { … }.
Instance Method Summary collapse
-
#bind(&fun) ⇒ Object
Take a block, and apply it to the value asynchronously, returing a future to represent the result.
-
#finalize ⇒ Object
Block until the operation completes, then return the result (wrapped as a FutureComplete).
-
#initialize(thread) ⇒ FuturePromise
constructor
A new instance of FuturePromise.
-
#pending? ⇒ Boolean
Are we still inflight?.
- #to_s ⇒ Object
-
#unwrap ⇒ Object
Block, then return the result (unwrapped).
Methods inherited from Future
#==, [], #eql?, #equals?, eventually, valid?
Methods included from ChainableMonad
#_, #coerce, #method_missing, #respond_to?
Constructor Details
#initialize(thread) ⇒ FuturePromise
Returns a new instance of FuturePromise.
74 75 76 |
# File 'lib/monads/future.rb', line 74 def initialize(thread) @thread = thread end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mon::Monad::ChainableMonad
Class Method Details
.perform(fun, args) ⇒ Object
Equivalent to calling Future::eventually { … }
79 80 81 82 83 84 85 86 |
# 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 |
Instance Method Details
#bind(&fun) ⇒ Object
Take a block, and apply it to the value asynchronously, returing a future to represent the result
90 91 92 |
# File 'lib/monads/future.rb', line 90 def bind &fun FuturePromise::perform(fun, [self]) end |
#finalize ⇒ Object
Block until the operation completes, then return the result (wrapped as a FutureComplete)
100 101 102 103 |
# File 'lib/monads/future.rb', line 100 def finalize value = @thread.value FutureComplete[(value.is_a? Future) ? value.unwrap : value] end |
#pending? ⇒ Boolean
Are we still inflight?
106 107 108 |
# File 'lib/monads/future.rb', line 106 def pending? @thread.alive? end |
#to_s ⇒ Object
110 111 112 |
# File 'lib/monads/future.rb', line 110 def to_s "FuturePromise[#{ @thread }]" end |
#unwrap ⇒ Object
Block, then return the result (unwrapped)
95 96 97 |
# File 'lib/monads/future.rb', line 95 def unwrap @thread.value end |