Class: Asynchro::Tracker
- Inherits:
-
Object
- Object
- Asynchro::Tracker
- Defined in:
- lib/asynchro/tracker.rb
Instance Method Summary collapse
-
#finish(&block) ⇒ Object
Executes this block when all the actions to be performed have checked in as finsished.
-
#finished? ⇒ Boolean
Returns true if this tracker has completed all supplied blocks, or false otherwise.
-
#initialize ⇒ Tracker
constructor
Creates a new tracker.
-
#perform(count = 1, &block) ⇒ Object
Performs an action.
-
#run! ⇒ Object
Runs through the tasks to perform for this tracker.
Constructor Details
#initialize ⇒ Tracker
Creates a new tracker. If a block is given, this block is called with the new instance as an argument, and the tracker is automatically run.
4 5 6 7 8 9 10 11 12 |
# File 'lib/asynchro/tracker.rb', line 4 def initialize @sequence = 0 if (block_given?) yield(self) self.run! end end |
Instance Method Details
#finish(&block) ⇒ Object
Executes this block when all the actions to be performed have checked in as finsished.
27 28 29 30 |
# File 'lib/asynchro/tracker.rb', line 27 def finish(&block) @finish ||= [ ] @finish << block end |
#finished? ⇒ Boolean
Returns true if this tracker has completed all supplied blocks, or false otherwise.
34 35 36 |
# File 'lib/asynchro/tracker.rb', line 34 def finished? !@blocks or @blocks.empty? end |
#perform(count = 1, &block) ⇒ Object
Performs an action. The supplied block will be called with a callback tracking Proc that should be triggered with ‘call` as many times as are specified in the `count` argument. When the correct number of calls have been made, this action is considered finished.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/asynchro/tracker.rb', line 42 def perform(count = 1, &block) @blocks ||= { } _sequence = @sequence += 1 @blocks[_sequence] = count callback = lambda { if (@blocks[_sequence]) if ((@blocks[_sequence] -=1) <= 0) @blocks.delete(_sequence) end if (self.finished?) @finish and @finish.each(&:call) end end } @procs ||= [ ] @procs << lambda { block.call(callback) } end |
#run! ⇒ Object
Runs through the tasks to perform for this tracker. Should only be called if this object is initialized without a supplied block as in that case, this would have been called already.
17 18 19 20 21 22 23 |
# File 'lib/asynchro/tracker.rb', line 17 def run! if (@procs) @procs.each(&:call) else @finish and @finish.each(&:call) end end |