Module: Concurrent::Promises::FactoryMethods

Extended by:
ReInclude
Includes:
Configuration, NewChannelIntegration
Included in:
Concurrent::Promises
Defined in:
lib/concurrent/promises.rb,
lib-edge/concurrent/edge/promises.rb,
lib-edge/concurrent/edge/old_channel_integration.rb

Overview

Container of all Future, Event factory methods. They are never constructed directly with new.

Defined Under Namespace

Modules: Configuration, NewChannelIntegration

Instance Method Summary collapse

Instance Method Details

#any_event(*futures_and_or_events) ⇒ Future

Shortcut of #any_event_on with default :io executor supplied.

Returns:

See Also:



294
295
296
# File 'lib/concurrent/promises.rb', line 294

def any_event(*futures_and_or_events)
  any_event_on default_executor, *futures_and_or_events
end

#any_event_on(default_executor, *futures_and_or_events) ⇒ Event

Creates new event which becomes resolved after first of the futures_and_or_events resolves. If resolved it does not propagate AbstractEventFuture#touch, leaving delayed futures un-executed if they are not required any more.

Parameters:

Returns:



304
305
306
# File 'lib/concurrent/promises.rb', line 304

def any_event_on(default_executor, *futures_and_or_events)
  AnyResolvedEventPromise.new_blocked_by(futures_and_or_events, default_executor).event
end

#any_fulfilled_future(*futures_and_or_events) ⇒ Future

Shortcut of #any_fulfilled_future_on with default :io executor supplied.

Returns:

See Also:



275
276
277
# File 'lib/concurrent/promises.rb', line 275

def any_fulfilled_future(*futures_and_or_events)
  any_fulfilled_future_on default_executor, *futures_and_or_events
end

#any_fulfilled_future_on(default_executor, *futures_and_or_events) ⇒ Future

Creates new future which is resolved after first of futures_and_or_events is fulfilled. Its result equals result of the first resolved future or if all futures_and_or_events reject, it has reason of the last resolved future. If resolved it does not propagate AbstractEventFuture#touch, leaving delayed futures un-executed if they are not required any more. If event is supplied, which does not have value and can be only resolved, it's represented as :fulfilled with value nil.

Parameters:

Returns:



288
289
290
# File 'lib/concurrent/promises.rb', line 288

def any_fulfilled_future_on(default_executor, *futures_and_or_events)
  AnyFulfilledFuturePromise.new_blocked_by(futures_and_or_events, default_executor).future
end

#any_resolved_future(*futures_and_or_events) ⇒ Future Also known as: any

Shortcut of #any_resolved_future_on with default :io executor supplied.

Returns:

See Also:



253
254
255
# File 'lib/concurrent/promises.rb', line 253

def any_resolved_future(*futures_and_or_events)
  any_resolved_future_on default_executor, *futures_and_or_events
end

#any_resolved_future_on(default_executor, *futures_and_or_events) ⇒ Future

Creates new future which is resolved after first futures_and_or_events is resolved. Its result equals result of the first resolved future. If resolved it does not propagate AbstractEventFuture#touch, leaving delayed futures un-executed if they are not required any more. If event is supplied, which does not have value and can be only resolved, it's represented as :fulfilled with value nil.

Parameters:

Returns:



269
270
271
# File 'lib/concurrent/promises.rb', line 269

def any_resolved_future_on(default_executor, *futures_and_or_events)
  AnyResolvedFuturePromise.new_blocked_by(futures_and_or_events, default_executor).future
end

#default_executorExecutor, :io, :fast Originally defined in module Configuration

Returns the executor which is used when none is supplied to a factory method. The method can be overridden in the receivers of include FactoryMethod

Returns:

  • (Executor, :io, :fast)

    the executor which is used when none is supplied to a factory method. The method can be overridden in the receivers of include FactoryMethod

#delay(*args, &task) ⇒ Future

Shortcut of #delay_on with default :io executor supplied.

Returns:

See Also:



184
185
186
# File 'lib/concurrent/promises.rb', line 184

def delay(*args, &task)
  delay_on default_executor, *args, &task
end

#delay_on(default_executor, *args) {|*args| ... } ⇒ Future

Constructs new Future which will be resolved after block is evaluated on default executor. The task will be evaluated only after the future is touched, see AbstractEventFuture#touch

Parameters:

  • default_executor (Executor, :io, :fast)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

  • args (Object)

    arguments which are passed to the task when it's executed. (It might be prepended with other arguments, see the @yeild section).

Yields:

  • (*args)

    to the task.

Yield Returns:

Returns:



192
193
194
# File 'lib/concurrent/promises.rb', line 192

def delay_on(default_executor, *args, &task)
  DelayPromise.new(default_executor).event.chain(*args, &task)
end

#fulfilled_future(value, default_executor = self.default_executor) ⇒ Future

Creates resolved future with will be fulfilled with the given value.

Parameters:

  • default_executor (Executor, :io, :fast) (defaults to: self.default_executor)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

Returns:



122
123
124
# File 'lib/concurrent/promises.rb', line 122

def fulfilled_future(value, default_executor = self.default_executor)
  resolved_future true, value, nil, default_executor
end

#future(*args, &task) ⇒ Future

Shortcut of #future_on with default :io executor supplied.

Returns:

See Also:



91
92
93
# File 'lib/concurrent/promises.rb', line 91

def future(*args, &task)
  future_on(default_executor, *args, &task)
end

#future_on(default_executor, *args) {|*args| ... } ⇒ Future

Constructs new Future which will be resolved after block is evaluated on default executor. Evaluation begins immediately.

Parameters:

  • default_executor (Executor, :io, :fast)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

  • args (Object)

    arguments which are passed to the task when it's executed. (It might be prepended with other arguments, see the @yeild section).

Yields:

  • (*args)

    to the task.

Yield Returns:

Returns:



105
106
107
# File 'lib/concurrent/promises.rb', line 105

def future_on(default_executor, *args, &task)
  ImmediateEventPromise.new(default_executor).future.then(*args, &task)
end

#create(nil, default_executor = self.default_executor) ⇒ Event #create(a_future, default_executor = self.default_executor) ⇒ Future #create(an_event, default_executor = self.default_executor) ⇒ Event #create(exception, default_executor = self.default_executor) ⇒ Future #create(value, default_executor = self.default_executor) ⇒ Future

General constructor. Behaves differently based on the argument's type. It's provided for convenience but it's better to be explicit.

Overloads:

  • #create(nil, default_executor = self.default_executor) ⇒ Event

    Returns resolved event.

    Parameters:

    • nil (nil)

    Returns:

    • (Event)

      resolved event.

  • #create(a_future, default_executor = self.default_executor) ⇒ Future

    Returns a future which will be resolved when a_future is.

    Parameters:

    Returns:

    • (Future)

      a future which will be resolved when a_future is.

  • #create(an_event, default_executor = self.default_executor) ⇒ Event

    Returns an event which will be resolved when an_event is.

    Parameters:

    Returns:

    • (Event)

      an event which will be resolved when an_event is.

  • #create(exception, default_executor = self.default_executor) ⇒ Future

    Returns a rejected future with the exception as its reason.

    Parameters:

    • exception (Exception)

    Returns:

    • (Future)

      a rejected future with the exception as its reason.

  • #create(value, default_executor = self.default_executor) ⇒ Future

    Returns a fulfilled future with the value.

    Parameters:

    • value (Object)

      when none of the above overloads fits

    Returns:

    • (Future)

      a fulfilled future with the value.

Parameters:

  • default_executor (Executor, :io, :fast) (defaults to: self.default_executor)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

Returns:

See Also:

  • resolved_event, fulfilled_future


168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/concurrent/promises.rb', line 168

def make_future(argument = nil, default_executor = self.default_executor)
  case argument
  when AbstractEventFuture
    # returning wrapper would change nothing
    argument
  when Exception
    rejected_future argument, default_executor
  when nil
    resolved_event default_executor
  else
    fulfilled_future argument, default_executor
  end
end

#rejected_future(reason, default_executor = self.default_executor) ⇒ Future

Creates resolved future with will be rejected with the given reason.

Parameters:

  • default_executor (Executor, :io, :fast) (defaults to: self.default_executor)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

Returns:



130
131
132
# File 'lib/concurrent/promises.rb', line 130

def rejected_future(reason, default_executor = self.default_executor)
  resolved_future false, nil, reason, default_executor
end

#resolvable_eventResolvableEvent

Shortcut of #resolvable_event_on with default :io executor supplied.

Returns:

See Also:



60
61
62
# File 'lib/concurrent/promises.rb', line 60

def resolvable_event
  resolvable_event_on default_executor
end

#resolvable_event_on(default_executor = self.default_executor) ⇒ ResolvableEvent

Created resolvable event, user is responsible for resolving the event once by ResolvableEvent#resolve.

Parameters:

  • default_executor (Executor, :io, :fast) (defaults to: self.default_executor)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

Returns:



69
70
71
# File 'lib/concurrent/promises.rb', line 69

def resolvable_event_on(default_executor = self.default_executor)
  ResolvableEventPromise.new(default_executor).future
end

#resolvable_futureResolvableFuture

Shortcut of #resolvable_future_on with default :io executor supplied.



75
76
77
# File 'lib/concurrent/promises.rb', line 75

def resolvable_future
  resolvable_future_on default_executor
end

#resolvable_future_on(default_executor = self.default_executor) ⇒ ResolvableFuture

Creates resolvable future, user is responsible for resolving the future once by ResolvableFuture#resolve, ResolvableFuture#fulfill, or ResolvableFuture#reject

Parameters:

  • default_executor (Executor, :io, :fast) (defaults to: self.default_executor)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

Returns:



85
86
87
# File 'lib/concurrent/promises.rb', line 85

def resolvable_future_on(default_executor = self.default_executor)
  ResolvableFuturePromise.new(default_executor).future
end

#resolved_event(default_executor = self.default_executor) ⇒ Event

Creates resolved event.

Parameters:

  • default_executor (Executor, :io, :fast) (defaults to: self.default_executor)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

Returns:



138
139
140
# File 'lib/concurrent/promises.rb', line 138

def resolved_event(default_executor = self.default_executor)
  ImmediateEventPromise.new(default_executor).event
end

#resolved_future(fulfilled, value, reason, default_executor = self.default_executor) ⇒ Future

Creates resolved future with will be either fulfilled with the given value or rejection with the given reason.

Parameters:

  • default_executor (Executor, :io, :fast) (defaults to: self.default_executor)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

Returns:



114
115
116
# File 'lib/concurrent/promises.rb', line 114

def resolved_future(fulfilled, value, reason, default_executor = self.default_executor)
  ImmediateFuturePromise.new(default_executor, fulfilled, value, reason).future
end

#schedule(intended_time, *args, &task) ⇒ Future

Shortcut of #schedule_on with default :io executor supplied.

Returns:

See Also:



198
199
200
# File 'lib/concurrent/promises.rb', line 198

def schedule(intended_time, *args, &task)
  schedule_on default_executor, intended_time, *args, &task
end

#schedule_on(default_executor, intended_time, *args) {|*args| ... } ⇒ Future

Constructs new Future which will be resolved after block is evaluated on default executor. The task is planned for execution in intended_time.

Parameters:

  • default_executor (Executor, :io, :fast)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

  • args (Object)

    arguments which are passed to the task when it's executed. (It might be prepended with other arguments, see the @yeild section).

  • intended_time (Numeric, Time)

    Numeric means to run in intended_time seconds. Time means to run on intended_time.

Yields:

  • (*args)

    to the task.

Yield Returns:

Returns:



209
210
211
# File 'lib/concurrent/promises.rb', line 209

def schedule_on(default_executor, intended_time, *args, &task)
  ScheduledPromise.new(default_executor, intended_time).event.chain(*args, &task)
end

#select_channel(*channels) ⇒ Future Originally defined in module NewChannelIntegration

Selects a channel which is ready to be read from.

Parameters:

Returns:

  • (Future)

    a future which is fulfilled with pair [channel, message] when one of the channels is available for reading

#zip_events(*futures_and_or_events) ⇒ Event

Shortcut of #zip_events_on with default :io executor supplied.

Returns:

See Also:



237
238
239
# File 'lib/concurrent/promises.rb', line 237

def zip_events(*futures_and_or_events)
  zip_events_on default_executor, *futures_and_or_events
end

#zip_events_on(default_executor, *futures_and_or_events) ⇒ Event

Creates new event which is resolved after all futures_and_or_events are resolved. (Future is resolved when fulfilled or rejected.)

Parameters:

Returns:



247
248
249
# File 'lib/concurrent/promises.rb', line 247

def zip_events_on(default_executor, *futures_and_or_events)
  ZipEventsPromise.new_blocked_by(futures_and_or_events, default_executor).event
end

#zip_futures(*futures_and_or_events) ⇒ Future Also known as: zip

Shortcut of #zip_futures_on with default :io executor supplied.

Returns:

See Also:



215
216
217
# File 'lib/concurrent/promises.rb', line 215

def zip_futures(*futures_and_or_events)
  zip_futures_on default_executor, *futures_and_or_events
end

#zip_futures_on(default_executor, *futures_and_or_events) ⇒ Future

Creates new future which is resolved after all futures_and_or_events are resolved. Its value is array of zipped future values. Its reason is array of reasons for rejection. If there is an error it rejects. If event is supplied, which does not have value and can be only resolved, it's represented as :fulfilled with value nil.

Parameters:

Returns:



229
230
231
# File 'lib/concurrent/promises.rb', line 229

def zip_futures_on(default_executor, *futures_and_or_events)
  ZipFuturesPromise.new_blocked_by(futures_and_or_events, default_executor).future
end

#zip_futures_over(enumerable, &future_factory) ⇒ Future

Note:

Edge Features are under active development and may change frequently.

  • Deprecations are not added before incompatible changes.
  • Edge version: major is always 0, minor bump means incompatible change, patch bump means compatible change.
  • Edge features may also lack tests and documentation.
  • Features developed in concurrent-ruby-edge are expected to move to concurrent-ruby when finalised.

Shortcut of #zip_futures_over_on with default :io executor supplied.

Returns:

See Also:



179
180
181
# File 'lib-edge/concurrent/edge/promises.rb', line 179

def zip_futures_over(enumerable, &future_factory)
  zip_futures_over_on default_executor, enumerable, &future_factory
end

#zip_futures_over_on(default_executor, enumerable) {|element| ... } ⇒ Future

Note:

Edge Features are under active development and may change frequently.

  • Deprecations are not added before incompatible changes.
  • Edge version: major is always 0, minor bump means incompatible change, patch bump means compatible change.
  • Edge features may also lack tests and documentation.
  • Features developed in concurrent-ruby-edge are expected to move to concurrent-ruby when finalised.

Creates new future which is resolved after all the futures created by future_factory from enumerable elements are resolved. Simplified it does: zip(*enumerable.map { |e| future e, &future_factory })

Examples:

# `#succ` calls are executed in parallel
zip_futures_over_on(:io, [1, 2], &:succ).value! # => [2, 3]

Parameters:

  • default_executor (Executor, :io, :fast)

    Instance of an executor or a name of the global executor. Default executor propagates to chained futures unless overridden with executor parameter or changed with AbstractEventFuture#with_default_executor.

  • enumerable (Enumerable)

Yields:

  • a task to be executed in future

Yield Parameters:

  • element (Object)

    from enumerable

Yield Returns:

  • (Object)

    a value of the future

Returns:



197
198
199
200
# File 'lib-edge/concurrent/edge/promises.rb', line 197

def zip_futures_over_on(default_executor, enumerable, &future_factory)
  # ZipFuturesPromise.new_blocked_by(futures_and_or_events, default_executor).future
  zip_futures_on(default_executor, *enumerable.map { |e| future e, &future_factory })
end