|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025, by Samuel Williams. |
| 5 | + |
| 6 | +module Async |
| 7 | + # Represents a flexible timeout that can be rescheduled or extended. |
| 8 | + # @public Since *Async v2.24*. |
| 9 | + class Timeout |
| 10 | + # Initialize a new timeout. |
| 11 | + def initialize(timers, handle) |
| 12 | + @timers = timers |
| 13 | + @handle = handle |
| 14 | + end |
| 15 | + |
| 16 | + # @returns [Numeric] The time remaining until the timeout occurs, in seconds. |
| 17 | + def duration |
| 18 | + @handle.time - @timers.now |
| 19 | + end |
| 20 | + |
| 21 | + # Update the duration of the timeout. |
| 22 | + # |
| 23 | + # The duration is relative to the current time, e.g. setting the duration to 5 means the timeout will occur in 5 seconds from now. |
| 24 | + # |
| 25 | + # @parameter value [Numeric] The new duration to assign to the timeout, in seconds. |
| 26 | + def duration=(value) |
| 27 | + self.reschedule(@timers.now + value) |
| 28 | + end |
| 29 | + |
| 30 | + # Adjust the timeout by the specified duration. |
| 31 | + # |
| 32 | + # The duration is relative to the timeout time, e.g. adjusting the timeout by 5 increases the current duration by 5 seconds. |
| 33 | + # |
| 34 | + # @parameter duration [Numeric] The duration to adjust the timeout by, in seconds. |
| 35 | + # @returns [Numeric] The new time at which the timeout will occur. |
| 36 | + def adjust(duration) |
| 37 | + self.reschedule(time + duration) |
| 38 | + end |
| 39 | + |
| 40 | + # @returns [Numeric] The time at which the timeout will occur, in seconds since {now}. |
| 41 | + def time |
| 42 | + @handle.time |
| 43 | + end |
| 44 | + |
| 45 | + # Assign a new time to the timeout, rescheduling it if necessary. |
| 46 | + # |
| 47 | + # @parameter value [Numeric] The new time to assign to the timeout. |
| 48 | + # @returns [Numeric] The new time at which the timeout will occur. |
| 49 | + def time=(value) |
| 50 | + self.reschedule(value) |
| 51 | + end |
| 52 | + |
| 53 | + # @returns [Numeric] The current time in the scheduler, relative to the time of this timeout, in seconds. |
| 54 | + def now |
| 55 | + @timers.now |
| 56 | + end |
| 57 | + |
| 58 | + # Cancel the timeout, preventing it from executing. |
| 59 | + def cancel! |
| 60 | + @handle.cancel! |
| 61 | + end |
| 62 | + |
| 63 | + # @returns [Boolean] Whether the timeout has been cancelled. |
| 64 | + def cancelled? |
| 65 | + @handle.cancelled? |
| 66 | + end |
| 67 | + |
| 68 | + # Raised when attempting to reschedule a cancelled timeout. |
| 69 | + class CancelledError < RuntimeError |
| 70 | + end |
| 71 | + |
| 72 | + # Reschedule the timeout to occur at the specified time. |
| 73 | + # |
| 74 | + # @parameter time [Numeric] The new time to schedule the timeout for. |
| 75 | + # @returns [Numeric] The new time at which the timeout will occur. |
| 76 | + private def reschedule(time) |
| 77 | + if block = @handle&.block |
| 78 | + @handle.cancel! |
| 79 | + |
| 80 | + @handle = @timers.schedule(time, block) |
| 81 | + |
| 82 | + return time |
| 83 | + else |
| 84 | + raise CancelledError, "Cannot reschedule a cancelled timeout!" |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments