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, *args) ⇒ Object
Performs an action.
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 13 14 |
# File 'lib/asynchro/tracker.rb', line 4 def initialize @sequence = 0 if (block_given?) yield(self) Fiber.new do self.run! end.resume end end |
Instance Method Details
#finish(&block) ⇒ Object
Executes this block when all the actions to be performed have checked in as finsished.
61 62 63 |
# File 'lib/asynchro/tracker.rb', line 61 def finish(&block) (@finish ||= [ ]) << block end |
#finished? ⇒ Boolean
Returns true if this tracker has completed all supplied blocks, or false otherwise.
67 68 69 |
# File 'lib/asynchro/tracker.rb', line 67 def finished? !@operations or @operations.empty? end |
#perform(count = 1, *args) ⇒ 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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/asynchro/tracker.rb', line 20 def perform(count = 1, *args) @operations ||= { } _sequence = @sequence += 1 @operations[_sequence] = true fiber = Fiber.new do called = false should_resume = false callback = lambda { called = true if (should_resume) fiber.resume end } count.times do called = false yield(callback, *args) unless (called) should_resume = true Fiber.yield end end @operations.delete(_sequence) if (self.finished?) self.finish! end end (@fibers ||= [ ]) << fiber end |