Class: DeferrableGratification::Combinators::Join Abstract

Inherits:
DefaultDeferrable show all
Defined in:
lib/deferrable_gratification/combinators/join.rb

Overview

This class is 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.

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

Direct Known Subclasses

FirstSuccess, Successes

Defined Under Namespace

Classes: FirstSuccess, Sentinel, Successes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*operations) ⇒ Join

Prepare to wait for the completion of operations.

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

Parameters:

  • *operations (*Deferrable)

    deferred statuses of asynchronous operations to wait for.



19
20
21
22
23
# 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

Class Method Details

.setup!(*operations) ⇒ Join

Create a DeferrableGratification::Combinators::Join and register the callbacks.

Parameters:

  • *operations (*Deferrable)

    deferred statuses of asynchronous operations to wait for.

Returns:

  • (Join)

    Deferrable representing the join operation.



47
48
49
# File 'lib/deferrable_gratification/combinators/join.rb', line 47

def self.setup!(*operations)
  new(*operations).tap(&:setup!)
end

Instance Method Details

#setup!Object

Register callbacks and errbacks on the supplied operations to notify this DeferrableGratification::Combinators::Join of completion.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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