module Concurrent::Edge::FutureShortcuts

Provides edge features, which will be added to or replace features in main gem.

Contains new unified implementation of Futures and Promises which combines Features of previous `Future`, `Promise`, `IVar`, `Event`, `Probe`, `dataflow`, `Delay`, `TimerTask` into single framework. It uses extensively new synchronization layer to make all the paths lock-free with exception of blocking threads on `#wait`. It offers better performance and does not block threads (exception being wait and similar methods where it's intended).

## Examples {include:file:examples/edge_futures.out.rb}

@!macro edge_warning

Public Instance Methods

any(*futures)
Alias for: any_complete
any_complete(*futures) click to toggle source

Constructs new {Future} which is completed after first of the futures is complete. @param [Event] futures @return [Future]

# File lib/concurrent/edge/future.rb, line 101
def any_complete(*futures)
  AnyCompletePromise.new(futures, :io).future
end
Also aliased as: any
any_successful(*futures) click to toggle source

Constructs new {Future} which becomes succeeded after first of the futures succeedes or failed if all futures fail (reason is last error). @param [Event] futures @return [Future]

# File lib/concurrent/edge/future.rb, line 111
def any_successful(*futures)
  AnySuccessfulPromise.new(futures, :io).future
end
async(default_executor = :io, &task)
Alias for: future
completed_event(default_executor = :io) click to toggle source

@return [Event] which is already completed

# File lib/concurrent/edge/future.rb, line 59
def completed_event(default_executor = :io)
  ImmediateEventPromise.new(default_executor).event
end
completed_future(success, value, reason, default_executor = :io) click to toggle source

@return [Future] which is already completed

# File lib/concurrent/edge/future.rb, line 44
def completed_future(success, value, reason, default_executor = :io)
  ImmediateFuturePromise.new(default_executor, success, value, reason).future
end
delay(default_executor = :io, &task) click to toggle source

Constructs new Future which will evaluate to the block after requested by calling `#wait`, `#value`, `#value!`, etc. on it or on any of the chained futures. @return [Future]

# File lib/concurrent/edge/future.rb, line 68
def delay(default_executor = :io, &task)
  DelayPromise.new(default_executor).future.then(&task)
end
event(default_executor = :io) click to toggle source

User is responsible for completing the event once by {Edge::CompletableEvent#complete} @return [CompletableEvent]

# File lib/concurrent/edge/future.rb, line 25
def event(default_executor = :io)
  CompletableEventPromise.new(default_executor).future
end
failed_future(reason, default_executor = :io) click to toggle source

@return [Future] which is already completed in failed state with reason

# File lib/concurrent/edge/future.rb, line 54
def failed_future(reason, default_executor = :io)
  completed_future false, nil, reason, default_executor
end
future(default_executor = :io, &task) click to toggle source

@overload future(default_executor = :io, &task)

Constructs new Future which will be completed after block is evaluated on executor. Evaluation begins immediately.
@return [Future]

@overload future(default_executor = :io)

User is responsible for completing the future once by {Edge::CompletableFuture#success} or {Edge::CompletableFuture#fail}
@return [CompletableFuture]
# File lib/concurrent/edge/future.rb, line 35
def future(default_executor = :io, &task)
  if task
    ImmediateEventPromise.new(default_executor).future.then(&task)
  else
    CompletableFuturePromise.new(default_executor).future
  end
end
Also aliased as: async
post(*args, &job) click to toggle source

post job on :io executor @return [true, false]

# File lib/concurrent/edge/future.rb, line 136
def post(*args, &job)
  post_on(:io, *args, &job)
end
post!(*args, &job) click to toggle source

post job on :fast executor @return [true, false]

# File lib/concurrent/edge/future.rb, line 130
def post!(*args, &job)
  post_on(:fast, *args, &job)
end
post_on(executor, *args, &job) click to toggle source

post job on executor @return [true, false]

# File lib/concurrent/edge/future.rb, line 142
def post_on(executor, *args, &job)
  Concurrent.executor(executor).post(*args, &job)
end
schedule(intended_time, default_executor = :io, &task) click to toggle source

Schedules the block to be executed on executor in given intended_time. @param [Numeric, Time] intended_time Numeric => run in `intended_time` seconds. Time => eun on time. @return [Future]

# File lib/concurrent/edge/future.rb, line 75
def schedule(intended_time, default_executor = :io, &task)
  ScheduledPromise.new(default_executor, intended_time).future.then(&task)
end
select(*channels) click to toggle source

only proof of concept @return [Future]

# File lib/concurrent/edge/future.rb, line 117
def select(*channels)
  future do
    # noinspection RubyArgCount
    Channel.select do |s|
      channels.each do |ch|
        s.take(ch) { |value| [value, ch] }
      end
    end
  end
end
succeeded_future(value, default_executor = :io) click to toggle source

@return [Future] which is already completed in success state with value

# File lib/concurrent/edge/future.rb, line 49
def succeeded_future(value, default_executor = :io)
  completed_future true, value, nil, default_executor
end
zip(*futures_and_or_events)
Alias for: zip_futures
zip_events(*futures_and_or_events) click to toggle source

Constructs new {Event} which is completed after all futures_and_or_events are complete (Future is completed when Success or Failed). @param [Event] futures_and_or_events @return [Event]

# File lib/concurrent/edge/future.rb, line 94
def zip_events(*futures_and_or_events)
  ZipEventsPromise.new(futures_and_or_events, :io).future
end
zip_futures(*futures_and_or_events) click to toggle source

Constructs new {Future} which is completed after all futures_and_or_events are complete. Its value is array of dependent future values. If there is an error it fails with the first one. Event does not have a value so it's represented by nil in the array of values. @param [Event] futures_and_or_events @return [Future]

# File lib/concurrent/edge/future.rb, line 84
def zip_futures(*futures_and_or_events)
  ZipFuturesPromise.new(futures_and_or_events, :io).future
end
Also aliased as: zip