Files

Class/Module Index [+]

Quicksearch

Concurrent::Edge::Future

Represents a value which will become available in future. May fail with a reason instead.

Public Instance Methods

&(other) click to toggle source
Alias for: zip
add_callback(method, *args) click to toggle source

@!visibility private

# File lib/concurrent/edge/future.rb, line 745
def add_callback(method, *args)
  state = internal_state
  if completed?(state)
    call_callback method, state, *args
  else
    @Callbacks.push [method, *args]
    state = internal_state
    # take back if it was completed in the meanwhile
    call_callbacks state if completed?(state)
  end
  self
end
any(*futures) click to toggle source

@return [Future] which has first completed value from futures

# File lib/concurrent/edge/future.rb, line 650
def any(*futures)
  AnyPromise.new([self, *futures], @DefaultExecutor).future
end
Also aliased as: |
apply(block) click to toggle source

@!visibility private

# File lib/concurrent/edge/future.rb, line 759
def apply(block)
  internal_state.apply block
end
chain_completable(completable_future) click to toggle source
# File lib/concurrent/edge/future.rb, line 630
def chain_completable(completable_future)
  on_completion! { completable_future.complete_with internal_state }
end
Also aliased as: tangle
complete_with(state, raise_on_reassign = true) click to toggle source

@!visibility private

# File lib/concurrent/edge/future.rb, line 727
def complete_with(state, raise_on_reassign = true)
  if compare_and_set_internal_state(PENDING, state)
    @Waiters.clear
    synchronize { ns_broadcast }
    call_callbacks state
  else
    if raise_on_reassign
      log ERROR, 'Edge::Future', reason if reason # print otherwise hidden error
      raise(Concurrent::MultipleAssignmentError.new(
                "Future can be completed only once. Current result is #{result}, " +
                    "trying to set #{state.result}"))
    end
    return false
  end
  self
end
delay() click to toggle source

Inserts delay into the chain of Futures making rest of it lazy evaluated. @return [Future]

# File lib/concurrent/edge/future.rb, line 656
def delay
  ZipFutureEventPromise.new(self, Delay.new(@DefaultExecutor).future, @DefaultExecutor).future
end
exception(*args) click to toggle source

@example allows failed Future to be risen

raise Concurrent.future.fail
# File lib/concurrent/edge/future.rb, line 607
def exception(*args)
  raise 'obligation is not failed' unless failed?
  reason = internal_state.reason
  if reason.is_a?(::Array)
    reason.each { |e| log ERROR, 'Edge::Future', e }
    Concurrent::Error.new 'multiple exceptions, inspect log'
  else
    reason.exception(*args)
  end
end
failed?(state = internal_state) click to toggle source

Has Future been failed? @return [Boolean]

# File lib/concurrent/edge/future.rb, line 548
def failed?(state = internal_state)
  state.completed? && !state.success?
end
flat(level = 1) click to toggle source

zips with the Future in the value @example

Concurrent.future { Concurrent.future { 1 } }.flat.vale # => 1
# File lib/concurrent/edge/future.rb, line 645
def flat(level = 1)
  FlatPromise.new(self, level, @DefaultExecutor).future
end
fulfilled?() click to toggle source
# File lib/concurrent/edge/future.rb, line 541
def fulfilled?
  deprecated_method 'fulfilled?', 'success?'
  success?
end
on_failure(executor = nil, &callback) click to toggle source

@yield [reason] executed async on `executor` when failed? @return self

# File lib/concurrent/edge/future.rb, line 710
def on_failure(executor = nil, &callback)
  add_callback :pr_async_callback_on_failure, executor || @DefaultExecutor, callback
end
on_failure!(&callback) click to toggle source

@yield [reason] executed sync when failed? @return self

# File lib/concurrent/edge/future.rb, line 722
def on_failure!(&callback)
  add_callback :pr_callback_on_failure, callback
end
on_success(executor = nil, &callback) click to toggle source

@yield [value] executed async on `executor` when success @return self

# File lib/concurrent/edge/future.rb, line 704
def on_success(executor = nil, &callback)
  add_callback :pr_async_callback_on_success, executor || @DefaultExecutor, callback
end
on_success!(&callback) click to toggle source

@yield [value] executed sync when success @return self

# File lib/concurrent/edge/future.rb, line 716
def on_success!(&callback)
  add_callback :pr_callback_on_success, callback
end
reason(timeout = nil) click to toggle source

@return [Exception, nil] the reason of the Future's failure @!macro edge.timeout_nil @!macro edge.periodical_wait

# File lib/concurrent/edge/future.rb, line 570
def reason(timeout = nil)
  touch
  internal_state.reason if wait_until_complete timeout
end
rejected?() click to toggle source
# File lib/concurrent/edge/future.rb, line 552
def rejected?
  deprecated_method 'rejected?', 'failed?'
  failed?
end
rescue(executor = nil, &callback) click to toggle source

@yield [reason] executed only on parent failure @return [Future]

# File lib/concurrent/edge/future.rb, line 638
def rescue(executor = nil, &callback)
  RescuePromise.new(self, @DefaultExecutor, executor || @DefaultExecutor, &callback).future
end
result(timeout = nil) click to toggle source

@return [Array(Boolean, Object, Exception), nil] triplet of success, value, reason @!macro edge.timeout_nil @!macro edge.periodical_wait

# File lib/concurrent/edge/future.rb, line 578
def result(timeout = nil)
  touch
  internal_state.result if wait_until_complete timeout
end
schedule(intended_time) click to toggle source

Schedules rest of the chain for execution with specified time or on specified time @return [Future]

# File lib/concurrent/edge/future.rb, line 662
def schedule(intended_time)
  chain do
    ZipFutureEventPromise.new(self,
                              ScheduledPromise.new(@DefaultExecutor, intended_time).event,
                              @DefaultExecutor).future
  end.flat
end
success?(state = internal_state) click to toggle source

Has Future been success? @return [Boolean]

# File lib/concurrent/edge/future.rb, line 537
def success?(state = internal_state)
  state.completed? && state.success?
end
tangle(completable_future) click to toggle source
Alias for: chain_completable
then(executor = nil, &callback) click to toggle source

@yield [value] executed only on parent success @return [Future]

# File lib/concurrent/edge/future.rb, line 620
def then(executor = nil, &callback)
  ThenPromise.new(self, @DefaultExecutor, executor || @DefaultExecutor, &callback).future
end
then_ask(actor) click to toggle source

Asks the actor with its value. @return [Future] new future with the response form the actor

# File lib/concurrent/edge/future.rb, line 626
def then_ask(actor)
  self.then { |v| actor.ask(v) }.flat
end
then_put(channel) click to toggle source

@note may block @note only proof of concept

# File lib/concurrent/edge/future.rb, line 698
def then_put(channel)
  on_success(:io) { |value| channel.put value }
end
then_select(*channels) click to toggle source

Zips with selected value form the suplied channels @return [Future]

# File lib/concurrent/edge/future.rb, line 672
def then_select(*channels)
  ZipPromise.new([self, Concurrent.select(*channels)], @DefaultExecutor).future
end
value(timeout = nil) click to toggle source

@return [Object, nil] the value of the Future when success, nil on timeout @!macro [attach] edge.timeout_nil

@note If the Future can have value `nil` then it cannot be distinquished from `nil` returned on timeout.
  In this case is better to use first `wait` then `value` (or similar).

@!macro edge.periodical_wait

# File lib/concurrent/edge/future.rb, line 562
def value(timeout = nil)
  touch
  internal_state.value if wait_until_complete timeout
end
value!(timeout = nil) click to toggle source

Wait until Future is complete? @param [Numeric] timeout the maximum time in second to wait. @raise reason on failure @return [Object, nil] @!macro edge.timeout_nil @!macro edge.periodical_wait

# File lib/concurrent/edge/future.rb, line 600
def value!(timeout = nil)
  touch
  internal_state.value if wait_until_complete! timeout
end
wait!(timeout = nil) click to toggle source

Wait until Future is complete? @param [Numeric] timeout the maximum time in second to wait. @raise reason on failure @return [Event, true, false] self or true/false if timeout is used @!macro edge.periodical_wait

# File lib/concurrent/edge/future.rb, line 588
def wait!(timeout = nil)
  touch
  result = wait_until_complete!(timeout)
  timeout ? result : self
end
with_default_executor(executor) click to toggle source

Changes default executor for rest of the chain @return [Future]

# File lib/concurrent/edge/future.rb, line 678
def with_default_executor(executor)
  FutureWrapperPromise.new(self, executor).future
end
zip(other) click to toggle source

Zip with future producing new Future @return [Future]

# File lib/concurrent/edge/future.rb, line 684
def zip(other)
  if other.is_a?(Future)
    ZipFutureFuturePromise.new(self, other, @DefaultExecutor).future
  else
    ZipFutureEventPromise.new(self, other, @DefaultExecutor).future
  end
end
Also aliased as: &
|(*futures) click to toggle source
Alias for: any

[Validate]

Generated with the Darkfish Rdoc Generator 2.