module Polyphony::GlobalAPI
Global API methods to be included in ::Object
Public Instance Methods
after(interval, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 11 def after(interval, &block) spin do sleep interval block.() end end
cancel_after(interval, with_exception: Polyphony::Cancel, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 18 def cancel_after(interval, with_exception: Polyphony::Cancel, &block) if !block cancel_after_blockless_canceller(Fiber.current, interval, with_exception) elsif block.arity > 0 cancel_after_with_block(Fiber.current, interval, with_exception, &block) else Polyphony.backend_timeout(interval, with_exception, &block) end end
cancel_after_blockless_canceller(fiber, interval, with_exception)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 28 def cancel_after_blockless_canceller(fiber, interval, with_exception) spin do sleep interval exception = cancel_exception(with_exception) exception.raising_fiber = nil fiber.schedule exception end end
cancel_after_with_block(fiber, interval, with_exception, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 37 def cancel_after_with_block(fiber, interval, with_exception, &block) canceller = cancel_after_blockless_canceller(fiber, interval, with_exception) block.call(canceller) ensure canceller.stop end
cancel_exception(exception)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 44 def cancel_exception(exception) case exception when Class then exception.new when Array then exception[0].new(exception[1]) else RuntimeError.new(exception) end end
every(interval, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 84 def every(interval, &block) Polyphony.backend_timer_loop(interval, &block) end
move_on_after(interval, with_value: nil, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 88 def move_on_after(interval, with_value: nil, &block) if !block move_on_blockless_canceller(Fiber.current, interval, with_value) elsif block.arity > 0 move_on_after_with_block(Fiber.current, interval, with_value, &block) else Polyphony.backend_timeout(interval, nil, with_value, &block) end end
move_on_after_with_block(fiber, interval, with_value, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 105 def move_on_after_with_block(fiber, interval, with_value, &block) canceller = spin do sleep interval fiber.schedule Polyphony::MoveOn.new(with_value) end block.call(canceller) rescue Polyphony::MoveOn => e e.value ensure canceller.stop end
move_on_blockless_canceller(fiber, interval, with_value)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 98 def move_on_blockless_canceller(fiber, interval, with_value) spin do sleep interval fiber.schedule with_value end end
receive()
click to toggle source
# File lib/polyphony/core/global_api.rb, line 117 def receive Fiber.current.receive end
receive_all_pending()
click to toggle source
# File lib/polyphony/core/global_api.rb, line 121 def receive_all_pending Fiber.current.receive_all_pending end
sleep(duration = nil)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 129 def sleep(duration = nil) return sleep_forever unless duration Polyphony.backend_sleep duration end
sleep_forever()
click to toggle source
# File lib/polyphony/core/global_api.rb, line 135 def sleep_forever Polyphony.backend_wait_event(true) end
spin(tag = nil, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 52 def spin(tag = nil, &block) Fiber.current.spin(tag, caller, &block) end
spin_loop(tag = nil, rate: nil, interval: nil, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 56 def spin_loop(tag = nil, rate: nil, interval: nil, &block) if rate || interval Fiber.current.spin(tag, caller) do throttled_loop(rate: rate, interval: interval, &block) end else spin_looped_block(tag, caller, block) end end
spin_looped_block(tag, caller, block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 66 def spin_looped_block(tag, caller, block) Fiber.current.spin(tag, caller) do block.call while true rescue LocalJumpError, StopIteration # break called or StopIteration raised end end
spin_scope() { || ... }
click to toggle source
# File lib/polyphony/core/global_api.rb, line 74 def spin_scope raise unless block_given? spin do result = yield Fiber.current.await_all_children result end.await end
supervise(*args, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 125 def supervise(*args, &block) Fiber.current.supervise(*args, &block) end
throttled_loop(rate = nil, **opts, &block)
click to toggle source
# File lib/polyphony/core/global_api.rb, line 139 def throttled_loop(rate = nil, **opts, &block) throttler = Polyphony::Throttler.new(rate || opts) if opts[:count] opts[:count].times { |_i| throttler.(&block) } else while true throttler.(&block) end end rescue LocalJumpError, StopIteration # break called or StopIteration raised ensure throttler&.stop end