lib/polyphony/core/timer.rb
# call-seq:
#   timer.move_on_after(interval) { ... }
#   timer.move_on_after(interval, with_value: value) { ... }
#   timer.move_on_after(interval) { |canceller| ... }
#   timer.move_on_after(interval, with_value: value) { |canceller| ... }
#
# Runs the given block after setting up a cancellation timer for
# cancellation. If the cancellation timer elapses, the execution will be
# interrupted with a `Polyphony::MoveOn` exception, which will be rescued,
# and with cause the operation to return the given value.
#
# This method should be used when a timeout is to be handled locally,
# without generating an exception that is to propagated down the call stack
# or up the fiber tree.
#
# Example of normal use:
#
#   timer.move_on_after(10) {
#     sleep 60
#     42
#   } #=> nil
#
#   timer.move_on_after(10, with_value: :oops) {
#     sleep 60
#     42
#   } #=> :oops
#
# The timeout period can be reset using `Timer#reset`, as shown in the
# following example:
#
#   timer.move_on_after(10) do
#     loop do
#       msg = socket.gets
#       timer.reset
#       handle_msg(msg)
#     end
#   end
#
# @param interval [Number] timout in seconds
# @param with_value: [any] return value in case of timeout
# @param &block [Proc] block to execute
# @return [any] block's return value


Runs the given block after setting up a cancellation timer for
cancellation. If the cancellation timer elapses, the execution will be
interrupted with a `Polyphony::MoveOn` exception, which will be rescued,
and with cause the operation to return the given value.

This method should be used when a timeout is to be handled locally,
without generating an exception that is to propagated down the call stack
or up the fiber tree.

Example of normal use.





The timeout period can be reset using `Timer#reset`, as shown in the
following example.


Runs the given block after setting up a cancellation timer for cancellation . If the cancellation timer elapses the execution will be interrupted with a ` Polyphony :: MoveOn ` exception which will be rescued and with cause the operation to return the given value . This method should be used when a timeout is to be handled locally without generating an exception that is to propagated down the call stack or up the fiber tree . Example of normal use . The timeout period can be reset using ` Timer#reset ` as shown in the following example .
def move_on_after(interval, with_value: nil)
      fiber = Fiber.current
      @timeouts[fiber] = {
        interval: interval,
        target_stamp: now + interval,
        exception: [Polyphony::MoveOn, with_value]
      }
      yield
    rescue Polyphony::MoveOn => e
      e.value
    ensure
      @timeouts.delete(fiber)
    end
def move_on_after ( interval , with_value : nil ) fiber = Fiber . current @timeouts [ fiber ] = { interval : interval , target_stamp : now + interval , exception : [ Polyphony :: MoveOn , with_value ] } yield rescue Polyphony :: MoveOn => e e . value ensure @timeouts . delete ( fiber ) end
call-seq:
timer.move_on_after(interval) { ... }
timer.move_on_after(interval, with_value: value) { ... }
timer.move_on_after(interval) { |canceller| ... }
timer.move_on_after(interval, with_value: value) { |canceller| ... }
call - seq : timer . move_on_after ( interval ) { ... } timer . move_on_after ( interval with_value : value ) { ... } timer . move_on_after ( interval ) { |canceller| ... } timer . move_on_after ( interval with_value : value ) { |canceller| ... }
block's return value
block ' s return value
any
interval
timout in seconds
timout in seconds
with_value
<not_specific>
with_value:
<not_specific>
return value in case of timeout
return value in case of timeout
&block
<not_specific>
block to execute
block to execute