Class: Async::Waterfall
- Inherits:
-
Object
- Object
- Async::Waterfall
- Defined in:
- lib/motion-asyncx/async.rb
Instance Attribute Summary collapse
-
#blocks ⇒ Object
Returns the value of attribute blocks.
Class Method Summary collapse
Instance Method Summary collapse
- #and_then(&block) ⇒ Object
-
#initialize ⇒ Waterfall
constructor
A new instance of Waterfall.
- #next_block(previous_result = nil) ⇒ Object
- #on_error(&block) ⇒ Object
- #on_success(&block) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ Waterfall
Returns a new instance of Waterfall.
4 5 6 7 8 9 |
# File 'lib/motion-asyncx/async.rb', line 4 def initialize @blocks = [] @on_error = nil @on_success = nil @current = 0 end |
Instance Attribute Details
#blocks ⇒ Object
Returns the value of attribute blocks.
3 4 5 |
# File 'lib/motion-asyncx/async.rb', line 3 def blocks @blocks end |
Class Method Details
.do(&block) ⇒ Object
28 29 30 31 32 |
# File 'lib/motion-asyncx/async.rb', line 28 def self.do(&block) chain = Waterfall.new chain.blocks << block chain end |
Instance Method Details
#and_then(&block) ⇒ Object
34 35 36 37 |
# File 'lib/motion-asyncx/async.rb', line 34 def and_then(&block) @blocks << block self end |
#next_block(previous_result = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/motion-asyncx/async.rb', line 11 def next_block(previous_result=nil) block = @blocks[@current] completion = lambda do |result=nil, error=nil| if error @on_error.call(result, error) if @on_error.is_a?(Proc) else @current += 1 if @blocks.length > @current next_block(result) elsif @on_success.is_a?(Proc) @on_success.call(result) end end end block.call(completion, previous_result) end |
#on_error(&block) ⇒ Object
44 45 46 47 |
# File 'lib/motion-asyncx/async.rb', line 44 def on_error(&block) @on_error = block self end |
#on_success(&block) ⇒ Object
39 40 41 42 |
# File 'lib/motion-asyncx/async.rb', line 39 def on_success(&block) @on_success = block self end |
#start ⇒ Object
49 50 51 |
# File 'lib/motion-asyncx/async.rb', line 49 def start next_block() end |