Class: FiberSpace::FiberContainer
- Inherits:
-
Object
- Object
- FiberSpace::FiberContainer
- Defined in:
- lib/fiber_space/container.rb
Overview
A FiberContainer wraps a source Fiber with attributes for maintaining a specific state. It allows storing and transferring/resuming of a target who is called once the source fiber has completed it’s work.
Instance Attribute Summary collapse
-
#chain ⇒ Boolean
The chain flag.
-
#id ⇒ Integer, ...
readonly
The ID.
-
#priority ⇒ Symbol
readonly
The priority.
-
#target ⇒ Fiber, FiberContainer
The target.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Mutual comparison operator.
-
#completed? ⇒ Boolean
Is the fiber completed?.
-
#initialize(opt = {}, &work) ⇒ FiberContainer
constructor
Constructs a new FiberContainer * cache [Array] cached arguments * id [Integer, Symbol] id of the Fiber.
- #resume ⇒ Object
Constructor Details
#initialize(opt = {}, &work) ⇒ FiberContainer
Constructs a new FiberContainer
-
cache [Array] cached arguments
-
id [Integer, Symbol] id of the Fiber.
-
count [Integer, Symbol] execution count.
-
target [Fiber, NilClass] target Fiber.
-
priority [Symbol] priority of the Fiber
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fiber_space/container.rb', line 28 def initialize(opt = {}, &work) @id = opt[:id] || Druuid.gen @count = opt[:count] || 1 @target = opt[:target] || nil @priority = opt[:priority] || :LOW @cache = opt[:cache] || [] @source = opt[:source] || Fiber.new do |cached| loop do if completed? case @target when FiberContainer then @target.completed? ? break : @target.resume when Fiber then @target.resume if @target.alive? else break end else @count -= 1 unless @count == :infinite Fiber.yield(yield(cached)) end end end end |
Instance Attribute Details
#chain ⇒ Boolean
Returns the chain flag.
14 15 16 |
# File 'lib/fiber_space/container.rb', line 14 def chain @chain end |
#id ⇒ Integer, ... (readonly)
Returns the ID.
6 7 8 |
# File 'lib/fiber_space/container.rb', line 6 def id @id end |
#priority ⇒ Symbol (readonly)
Returns the priority.
10 11 12 |
# File 'lib/fiber_space/container.rb', line 10 def priority @priority end |
#target ⇒ Fiber, FiberContainer
Returns the target.
18 19 20 |
# File 'lib/fiber_space/container.rb', line 18 def target @target end |
Instance Method Details
#<=>(other) ⇒ Object
Mutual comparison operator
66 67 68 |
# File 'lib/fiber_space/container.rb', line 66 def <=>(other) FiberSpace::FIBER_PRIORITIES[@priority] <=> FiberSpace::FIBER_PRIORITIES[other.priority] end |
#completed? ⇒ Boolean
Is the fiber completed?
56 57 58 59 60 61 62 |
# File 'lib/fiber_space/container.rb', line 56 def completed? if @count == :infinite !@source.alive? else @count == :once || @count.zero? || @count.negative? || !@source.alive? end end |
#resume ⇒ Object
50 51 52 |
# File 'lib/fiber_space/container.rb', line 50 def resume @source.resume(@cache) unless completed? end |