Class: WebConsole::Fiber
- Inherits:
-
Object
- Object
- WebConsole::Fiber
- Defined in:
- lib/web_console/fiber.rb
Instance Attribute Summary collapse
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Fiber
constructor
A new instance of Fiber.
- #inspect ⇒ Object
- #resume(*args) ⇒ Object
- #yield(*args) ⇒ Object
Constructor Details
#initialize ⇒ Fiber
Returns a new instance of Fiber.
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
#thread ⇒ Object (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
.current ⇒ Object
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
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
#inspect ⇒ Object
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
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 |