Class: Citrus::Utils::CountDownLatch

Inherits:
Object
  • Object
show all
Defined in:
lib/citrus/util/countdown_latch.rb

Overview

CountDownLatch

Instance Method Summary collapse

Constructor Details

#initialize(count, args = {}, &block) ⇒ CountDownLatch

Create a count down latch

Parameters:

  • count (Integer)
  • args (Hash) (defaults to: {})


18
19
20
21
22
23
24
25
26
# File 'lib/citrus/util/countdown_latch.rb', line 18

def initialize count, args={}, &block
  @count = count
  @block = block
  if args[:timeout]
    @timer = EM::Timer.new(args[:timeout]) {
      @block.respond_to? :call and @block.call true
    }
  end
end

Instance Method Details

#doneObject

Called when a task finish count down



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/citrus/util/countdown_latch.rb', line 29

def done
  unless @count > 0
    throw Exception.new 'illegal state'
  end

  @count -= 1
  if @count == 0
    @timer.cancel if @timer
    @block.respond_to? :call and @block.call
  end
end