123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::Promises::ResolvableFuture

Overview

A Future which can be resolved by user.

Constant Summary

InternalStates - Included

PENDING, RESERVED, RESOLVED

Class Attribute Summary

Class Method Summary

AbstractEventFuture - Inherited

Synchronization::Object - Inherited

.atomic_attribute?, .atomic_attributes,
.attr_atomic

Creates methods for reading and writing to a instance variable with volatile (Java) semantic as .attr_volatile does.

.attr_volatile

Creates methods for reading and writing (as attr_accessor does) to a instance variable with volatile (Java) semantic.

.ensure_safe_initialization_when_final_fields_are_present

For testing purposes, quite slow.

.new

Has to be called by children.

.safe_initialization!, .define_initialize_atomic_fields

Synchronization::AbstractObject - Inherited

Instance Attribute Summary

Future - Inherited

#fulfilled?

Is it in fulfilled state?

#rejected?

Is it in rejected state?

AbstractEventFuture - Inherited

#pending?

Is it in pending state?

#resolved?

Is it in resolved state?

#touched?

For inspection.

Instance Method Summary

Resolvable - Included

#release,
#reserve

Reserves the event or future, if reserved others are prevented from resolving it.

Future - Inherited

#&

Alias for Future#zip.

#any

Creates a new event which will be resolved when the first of receiver, event_or_future resolves.

#delay

Creates new future dependent on receiver which will not evaluate until touched, see #touch.

#exception

Allows rejected Future to be risen with raise method.

#flat
#flat_event

Creates new event which will be resolved when the returned event by receiver is.

#flat_future

Creates new future which will have result of the future returned by receiver.

#inspect

Alias for Future#to_s.

#on_fulfillment

Shortcut of #on_fulfillment_using with default :io executor supplied.

#on_fulfillment!

Stores the callback to be executed synchronously on resolving thread after it is fulfilled.

#on_fulfillment_using

Stores the callback to be executed asynchronously on executor after it is fulfilled.

#on_rejection

Shortcut of #on_rejection_using with default :io executor supplied.

#on_rejection!

Stores the callback to be executed synchronously on resolving thread after it is rejected.

#on_rejection_using

Stores the callback to be executed asynchronously on executor after it is rejected.

#reason

Returns reason of future’s rejection.

#rescue

Shortcut of #rescue_on with default :io executor supplied.

#rescue_on

Chains the task to be executed asynchronously on executor after it rejects.

#result

Returns triplet fulfilled?, value, reason.

#run

Allows to use futures as green threads.

#schedule

Creates new event dependent on receiver scheduled to execute on/in intended_time.

#then

Shortcut of #then_on with default :io executor supplied.

#then_on

Chains the task to be executed asynchronously on executor after it fulfills.

#to_event

Converts future to event which is resolved when future is resolved by fulfillment or rejection.

#to_future

Returns self, since this is a future.

#to_s,
#value

Return value of the future.

#value!

Return value of the future.

#wait!

Wait (block the Thread) until receiver is #resolved?.

#with_default_executor

Crates new object with same class with the executor set as its new default executor.

#zip

Creates a new event or a future which will be resolved when receiver and other are.

#|

Alias for Future#any.

#apply, #async_callback_on_fulfillment, #async_callback_on_rejection, #callback_on_fulfillment, #callback_on_rejection, #callback_on_resolution, #rejected_resolution, #run_test, #wait_until_resolved!

Future::OldChannelIntegration - Included

#then_put,
#then_select

Zips with selected value form the suplied channels.

Future::NewChannelIntegration - Included

Future::FlatShortcuts - Included

Future::ActorIntegration - Included

#then_ask

Asks the actor with its value.

AbstractEventFuture - Inherited

#chain

Shortcut of #chain_on with default :io executor supplied.

#chain_on

Chains the task to be executed asynchronously on executor after it is resolved.

#chain_resolvable

Resolves the resolvable when receiver is resolved.

#default_executor

Returns default executor.

#inspect
#internal_state,
#on_resolution

Shortcut of #on_resolution_using with default :io executor supplied.

#on_resolution!

Stores the callback to be executed synchronously on resolving thread after it is resolved.

#on_resolution_using

Stores the callback to be executed asynchronously on executor after it is resolved.

#state

Returns its state.

#tangle
#to_s,
#touch

Propagates touch.

#wait

Wait (block the Thread) until receiver is #resolved?.

#with_default_executor

Crates new object with same class with the executor set as its new default executor.

#add_callback, #add_callback_clear_delayed_node, #add_callback_notify_blocked, #async_callback_on_resolution,
#blocks

For inspection.

#call_callback, #call_callbacks, #callback_clear_delayed_node, #callback_notify_blocked,
#callbacks

For inspection.

#compare_and_set_internal_state

Sets the internal_state to new_internal_state if the current internal_state is expected_internal_state.

#internal_state=

Set the internal_state.

#promise

For inspection.

#resolve_with,
#swap_internal_state

Set the internal_state to new_internal_state and return the old internal_state.

#update_internal_state

Updates the internal_state using the block.

#wait_until_resolved,
#waiting_threads

For inspection.

#with_async, #with_hidden_resolvable

Synchronization::Object - Inherited

Synchronization::Volatile - Included

Synchronization::AbstractObject - Inherited

Constructor Details

This class inherits a constructor from Concurrent::Promises::AbstractEventFuture

Instance Method Details

#evaluate_to(*args) {|*args| ... } ⇒ self

Evaluates the block and sets its result as future’s value fulfilling, if the block raises an exception the future rejects with it.

Yields:

  • (*args)

    to the block.

Yield Returns:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1395

def evaluate_to(*args, &block)
  promise.evaluate_to(*args, block)
end

#evaluate_to!(*args) {|*args| ... } ⇒ self

Evaluates the block and sets its result as future’s value fulfilling, if the block raises an exception the future rejects with it.

Yields:

  • (*args)

    to the block.

Yield Returns:

Raises:

  • (Exception)

    also raise reason on rejection.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1406

def evaluate_to!(*args, &block)
  promise.evaluate_to(*args, block).wait!
end

#fulfill(value, raise_on_reassign = true, reserved = false) ⇒ self, false

Makes the future fulfilled with #value, which triggers all dependent futures.

Parameters:

  • value (Object)
  • raise_on_reassign (Boolean) (defaults to: true)

    should method raise exception if already resolved

  • reserved (true, false) (defaults to: false)

    Set to true if the resolvable is #reserved by you, marks resolution of reserved resolvable events and futures explicitly. Advanced feature, ignore unless you use Resolvable#reserve from edge.

Returns:

  • (self, false)

    false is returned when raise_on_reassign is false and the receiver is already resolved.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1375

def fulfill(value, raise_on_reassign = true, reserved = false)
  resolve_with Fulfilled.new(value), raise_on_reassign, reserved
end

#reason(timeout = nil, timeout_value = nil, resolve_on_timeout = nil) ⇒ Exception, ...

Behaves as Future#reason but has one additional optional argument resolve_on_timeout.

Parameters:

  • resolve_on_timeout (::Array(true, Object, nil), ::Array(false, nil, Exception), nil) (defaults to: nil)

    If it times out and the argument is not nil it will also resolve the future to the provided resolution.

See Also:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1503

def reason(timeout = nil, timeout_value = nil, resolve_on_timeout = nil)
  if wait_until_resolved timeout
    internal_state.reason
  else
    if resolve_on_timeout
      unless resolve(*resolve_on_timeout, false)
        # if it fails to resolve it was resolved in the meantime
        # so return value as if there was no timeout
        return internal_state.reason
      end
    end
    timeout_value
  end
end

#reject(reason, raise_on_reassign = true, reserved = false) ⇒ self, false

Makes the future rejected with #reason, which triggers all dependent futures.

Parameters:

  • reason (Object)
  • raise_on_reassign (Boolean) (defaults to: true)

    should method raise exception if already resolved

  • reserved (true, false) (defaults to: false)

    Set to true if the resolvable is #reserved by you, marks resolution of reserved resolvable events and futures explicitly. Advanced feature, ignore unless you use Resolvable#reserve from edge.

Returns:

  • (self, false)

    false is returned when raise_on_reassign is false and the receiver is already resolved.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1385

def reject(reason, raise_on_reassign = true, reserved = false)
  resolve_with Rejected.new(reason), raise_on_reassign, reserved
end

#resolve(fulfilled = true, value = nil, reason = nil, raise_on_reassign = true, reserved = false) ⇒ self, false

Makes the future resolved with result of triplet fulfilled?, #value, #reason, which triggers all dependent futures.

Parameters:

  • fulfilled (true, false) (defaults to: true)
  • value (Object) (defaults to: nil)
  • reason (Object) (defaults to: nil)
  • raise_on_reassign (Boolean) (defaults to: true)

    should method raise exception if already resolved

  • reserved (true, false) (defaults to: false)

    Set to true if the resolvable is #reserved by you, marks resolution of reserved resolvable events and futures explicitly. Advanced feature, ignore unless you use Resolvable#reserve from edge.

Returns:

  • (self, false)

    false is returned when raise_on_reassign is false and the receiver is already resolved.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1365

def resolve(fulfilled = true, value = nil, reason = nil, raise_on_reassign = true, reserved = false)
  resolve_with(fulfilled ? Fulfilled.new(value) : Rejected.new(reason), raise_on_reassign, reserved)
end

#result(timeout = nil, resolve_on_timeout = nil) ⇒ ::Array(Boolean, Object, Exception)?

Behaves as Future#result but has one additional optional argument resolve_on_timeout.

Parameters:

  • resolve_on_timeout (::Array(true, Object, nil), ::Array(false, nil, Exception), nil) (defaults to: nil)

    If it times out and the argument is not nil it will also resolve the future to the provided resolution.

See Also:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1524

def result(timeout = nil, resolve_on_timeout = nil)
  if wait_until_resolved timeout
    internal_state.result
  else
    if resolve_on_timeout
      unless resolve(*resolve_on_timeout, false)
        # if it fails to resolve it was resolved in the meantime
        # so return value as if there was no timeout
        internal_state.result
      end
    end
    # otherwise returns nil
  end
end

#value(timeout = nil, timeout_value = nil, resolve_on_timeout = nil) ⇒ Object, ...

Behaves as Future#value but has one additional optional argument resolve_on_timeout.

Parameters:

  • resolve_on_timeout (::Array(true, Object, nil), ::Array(false, nil, Exception), nil) (defaults to: nil)

    If it times out and the argument is not nil it will also resolve the future to the provided resolution.

See Also:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1459

def value(timeout = nil, timeout_value = nil, resolve_on_timeout = nil)
  if wait_until_resolved timeout
    internal_state.value
  else
    if resolve_on_timeout
      unless resolve(*resolve_on_timeout, false)
        # if it fails to resolve it was resolved in the meantime
        # so return value as if there was no timeout
        return internal_state.value
      end
    end
    timeout_value
  end
end

#value!(timeout = nil, timeout_value = nil, resolve_on_timeout = nil) ⇒ Object, ...

Behaves as Future#value! but has one additional optional argument resolve_on_timeout.

Parameters:

  • resolve_on_timeout (::Array(true, Object, nil), ::Array(false, nil, Exception), nil) (defaults to: nil)

    If it times out and the argument is not nil it will also resolve the future to the provided resolution.

Raises:

  • (Exception)

    #reason on rejection

See Also:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1481

def value!(timeout = nil, timeout_value = nil, resolve_on_timeout = nil)
  if wait_until_resolved! timeout
    internal_state.value
  else
    if resolve_on_timeout
      unless resolve(*resolve_on_timeout, false)
        # if it fails to resolve it was resolved in the meantime
        # so return value as if there was no timeout
        raise self if rejected?
        return internal_state.value
      end
    end
    timeout_value
  end
end

#wait(timeout = nil, resolve_on_timeout = nil) ⇒ self, ...

Behaves as AbstractEventFuture#wait but has one additional optional argument resolve_on_timeout.

Parameters:

  • resolve_on_timeout (::Array(true, Object, nil), ::Array(false, nil, Exception), nil) (defaults to: nil)

    If it times out and the argument is not nil it will also resolve the future to the provided resolution.

See Also:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1421

def wait(timeout = nil, resolve_on_timeout = nil)
  super(timeout) or if resolve_on_timeout
                      # if it fails to resolve it was resolved in the meantime
                      # so return true as if there was no timeout
                      !resolve(*resolve_on_timeout, false)
                    else
                      false
                    end
end

#wait!(timeout = nil, resolve_on_timeout = nil) ⇒ self, ...

Behaves as Future#wait! but has one additional optional argument resolve_on_timeout.

Parameters:

  • resolve_on_timeout (::Array(true, Object, nil), ::Array(false, nil, Exception), nil) (defaults to: nil)

    If it times out and the argument is not nil it will also resolve the future to the provided resolution.

Raises:

  • (Exception)

    #reason on rejection

See Also:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1438

def wait!(timeout = nil, resolve_on_timeout = nil)
  super(timeout) or if resolve_on_timeout
                      if resolve(*resolve_on_timeout, false)
                        false
                      else
                        # if it fails to resolve it was resolved in the meantime
                        # so return true as if there was no timeout
                        raise self if rejected?
                        true
                      end
                    else
                      false
                    end
end

#with_hidden_resolvableFuture

Creates new future wrapping receiver, effectively hiding the resolve method and similar.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/promises.rb', line 1542

def with_hidden_resolvable
  @with_hidden_resolvable ||= FutureWrapperPromise.new_blocked_by1(self, @DefaultExecutor).future
end