Class: FiberSpace::FiberContainer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • opt (Hash) (defaults to: {})

    options for the Container.



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

#chainBoolean

Returns the chain flag.

Returns:

  • (Boolean)

    the chain flag



14
15
16
# File 'lib/fiber_space/container.rb', line 14

def chain
  @chain
end

#idInteger, ... (readonly)

Returns the ID.

Returns:

  • (Integer, String, Symbol)

    the ID



6
7
8
# File 'lib/fiber_space/container.rb', line 6

def id
  @id
end

#prioritySymbol (readonly)

Returns the priority.

Returns:

  • (Symbol)

    the priority



10
11
12
# File 'lib/fiber_space/container.rb', line 10

def priority
  @priority
end

#targetFiber, FiberContainer

Returns the target.

Returns:



18
19
20
# File 'lib/fiber_space/container.rb', line 18

def target
  @target
end

Instance Method Details

#<=>(other) ⇒ Object

Mutual comparison operator

Parameters:



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?

Returns:

  • (Boolean)


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

#resumeObject



50
51
52
# File 'lib/fiber_space/container.rb', line 50

def resume
  @source.resume(@cache) unless completed?
end