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 740
def add_callback(method, *args)
  state = @State.get
  if completed?(state)
    call_callback method, state, *args
  else
    @Callbacks.push [method, *args]
    state = @State.get
    # 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 645
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 754
def apply(block)
  @State.get.apply block
end
chain_completable(completable_future) click to toggle source
# File lib/concurrent/edge/future.rb, line 625
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 722
def complete_with(state, raise_on_reassign = true)
  if @State.compare_and_set(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 651
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 602
def exception(*args)
  raise 'obligation is not failed' unless failed?
  reason = @State.get.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 = @State.get) click to toggle source

Has Future been failed? @return [Boolean]

# File lib/concurrent/edge/future.rb, line 543
def failed?(state = @State.get)
  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 640
def flat(level = 1)
  FlatPromise.new(self, level, @DefaultExecutor).future
end
fulfilled?() click to toggle source
# File lib/concurrent/edge/future.rb, line 536
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 705
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 717
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 699
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 711
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 565
def reason(timeout = nil)
  touch
  @State.get.reason if wait_until_complete timeout
end
rejected?() click to toggle source
# File lib/concurrent/edge/future.rb, line 547
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 633
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 573
def result(timeout = nil)
  touch
  @State.get.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 657
def schedule(intended_time)
  chain do
    ZipFutureEventPromise.new(self,
                              ScheduledPromise.new(@DefaultExecutor, intended_time).event,
                              @DefaultExecutor).future
  end.flat
end
success?(state = @State.get) click to toggle source

Has Future been success? @return [Boolean]

# File lib/concurrent/edge/future.rb, line 532
def success?(state = @State.get)
  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 615
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 621
def then_ask(actor)
  self.then { |v| actor.ask(v) }.flat
end
then_push(channel) click to toggle source

@note may block @note only proof of concept

# File lib/concurrent/edge/future.rb, line 693
def then_push(channel)
  on_success(:io) { |value| channel.push 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 667
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 557
def value(timeout = nil)
  touch
  @State.get.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 595
def value!(timeout = nil)
  touch
  @State.get.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 583
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 673
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 679
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.