Class: EMPromise::Trampoline
- Inherits:
-
Object
- Object
- EMPromise::Trampoline
- Defined in:
- lib/em_promise.rb
Defined Under Namespace
Classes: SubmittedBlock
Instance Attribute Summary collapse
-
#ready ⇒ Object
Returns the value of attribute ready.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(&initial_blk) ⇒ Trampoline
constructor
A new instance of Trampoline.
- #submit(&blk) ⇒ Object
Constructor Details
#initialize(&initial_blk) ⇒ Trampoline
Returns a new instance of Trampoline.
21 22 23 24 25 26 27 28 29 |
# File 'lib/em_promise.rb', line 21 def initialize(&initial_blk) @ready = true @fiber = Fiber.new { |blk| Thread.current[:_em_promise_trampoline] = self self.ready = false Trampoline.yield(blk.call) } @fiber.resume(&initial_blk) if initial_blk end |
Instance Attribute Details
#ready ⇒ Object
Returns the value of attribute ready.
9 10 11 |
# File 'lib/em_promise.rb', line 9 def ready @ready end |
Class Method Details
.yield(arg = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/em_promise.rb', line 41 def self.yield(arg=nil) tramp = Thread.current[:_em_promise_trampoline] raise "Trampoline already ready?" if tramp&.ready loop do tramp&.ready = true blk = Fiber.yield arg tramp&.ready = false return arg unless blk.is_a?(SubmittedBlock) arg = blk.call end end |
Instance Method Details
#submit(&blk) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/em_promise.rb', line 31 def submit(&blk) EM.next_tick do if ready @fiber.resume(SubmittedBlock.new(blk)) else submit(&blk) end end end |