class DeferrableGratification::Combinators::Join

Abstract base class for combinators that depend on a number of asynchronous operations (potentially executing in parallel).

@abstract Subclasses should override {#done?} to define whether they wait

for some or all of the operations to complete, and {#finish} to define
what they do when {#done?} returns true.

Public Class Methods

new(*operations) click to toggle source

Prepare to wait for the completion of operations.

Does not actually set up any callbacks or errbacks: call {#setup!} for that.

@param [*Deferrable] *operations deferred statuses of asynchronous

operations to wait for.
# File lib/deferrable_gratification/combinators/join.rb, line 19
def initialize(*operations)
  @operations = operations
  @successes = Array.new(@operations.size, Sentinel.new)
  @failures = Array.new(@operations.size, Sentinel.new)
end
setup!(*operations) click to toggle source

Create a {Join} and register the callbacks.

@param (see initialize)

@return [Join] Deferrable representing the join operation.

# File lib/deferrable_gratification/combinators/join.rb, line 47
def self.setup!(*operations)
  new(*operations).tap(&:setup!)
end

Public Instance Methods

setup!() click to toggle source

Register callbacks and errbacks on the supplied operations to notify this {Join} of completion.

# File lib/deferrable_gratification/combinators/join.rb, line 27
def setup!
  finish if done?

  @operations.each_with_index do |op, index|
    op.callback do |result|
      @successes[index] = result
      finish if done?
    end
    op.errback do |error|
      @failures[index] = error
      finish if done?
    end
  end
end

Private Instance Methods

all_completed?() click to toggle source
# File lib/deferrable_gratification/combinators/join.rb, line 107
def all_completed?
  successes.length + failures.length >= @operations.length
end
done?() click to toggle source
# File lib/deferrable_gratification/combinators/join.rb, line 111
def done?
  raise NotImplementedError, 'subclasses should override this'
end
failures() click to toggle source
# File lib/deferrable_gratification/combinators/join.rb, line 103
def failures
  without_sentinels(@failures)
end
finish() click to toggle source
# File lib/deferrable_gratification/combinators/join.rb, line 115
def finish
  raise NotImplementedError, 'subclasses should override this'
end
successes() click to toggle source
# File lib/deferrable_gratification/combinators/join.rb, line 99
def successes
  without_sentinels(@successes)
end
without_sentinels(ary) click to toggle source
# File lib/deferrable_gratification/combinators/join.rb, line 119
def without_sentinels(ary)
  ary.reject {|item| item.instance_of? Sentinel }
end