Class: WebConsole::Fiber

Inherits:
Object
  • Object
show all
Defined in:
lib/web_console/fiber.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFiber

Returns a new instance of Fiber.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
# File 'lib/web_console/fiber.rb', line 10

def initialize
  raise ArgumentError, 'new Fiber requires a block' unless block_given?

  @yield = Queue.new
  @resume = Queue.new

  @thread = Thread.new { @yield.push [ *yield(*@resume.pop) ] }
  @thread.abort_on_exception = true
  @thread[:fiber] = self
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



20
21
22
# File 'lib/web_console/fiber.rb', line 20

def thread
  @thread
end

Class Method Details

.currentObject



40
41
42
# File 'lib/web_console/fiber.rb', line 40

def self.current
  Thread.current[:fiber] or raise FiberError, 'not inside a fiber'
end

.yield(*args) ⇒ Object

Raises:



35
36
37
38
# File 'lib/web_console/fiber.rb', line 35

def self.yield(*args)
  raise FiberError, "can't yield from root fiber" unless fiber = Thread.current[:fiber]
  fiber.yield(*args)
end

Instance Method Details

#inspectObject



44
45
46
# File 'lib/web_console/fiber.rb', line 44

def inspect
  "#<#{self.class}:0x#{self.object_id.to_s(16)}>"
end

#resume(*args) ⇒ Object

Raises:



22
23
24
25
26
27
# File 'lib/web_console/fiber.rb', line 22

def resume(*args)
  raise FiberError, 'dead fiber called' unless @thread.alive?
  @resume.push(args)
  result = @yield.pop
  result.size > 1 ? result : result.first
end

#yield(*args) ⇒ Object



29
30
31
32
33
# File 'lib/web_console/fiber.rb', line 29

def yield(*args)
  @yield.push(args)
  result = @resume.pop
  result.size > 1 ? result : result.first
end