|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025, by Samuel Williams. |
| 5 | + |
| 6 | +require "fiber" |
| 7 | +require "console" |
| 8 | + |
| 9 | +module Async |
| 10 | + # Raised when a task is explicitly stopped. |
| 11 | + class Stop < Exception |
| 12 | + # Represents the source of the stop operation. |
| 13 | + class Cause < Exception |
| 14 | + if RUBY_VERSION >= "3.4" |
| 15 | + # @returns [Array(Thread::Backtrace::Location)] The backtrace of the caller. |
| 16 | + def self.backtrace |
| 17 | + caller_locations(2..-1) |
| 18 | + end |
| 19 | + else |
| 20 | + # @returns [Array(String)] The backtrace of the caller. |
| 21 | + def self.backtrace |
| 22 | + caller(2..-1) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + # Create a new cause of the stop operation, with the given message. |
| 27 | + # |
| 28 | + # @parameter message [String] The error message. |
| 29 | + # @returns [Cause] The cause of the stop operation. |
| 30 | + def self.for(message = "Task was stopped") |
| 31 | + instance = self.new(message) |
| 32 | + instance.set_backtrace(self.backtrace) |
| 33 | + return instance |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + if RUBY_VERSION < "3.5" |
| 38 | + # Create a new stop operation. |
| 39 | + # |
| 40 | + # This is a compatibility method for Ruby versions before 3.5 where cause is not propagated correctly when using {Fiber#raise} |
| 41 | + # |
| 42 | + # @parameter message [String | Hash] The error message or a hash containing the cause. |
| 43 | + def initialize(message = "Task was stopped") |
| 44 | + if message.is_a?(Hash) |
| 45 | + @cause = message[:cause] |
| 46 | + message = "Task was stopped" |
| 47 | + end |
| 48 | + |
| 49 | + super(message) |
| 50 | + end |
| 51 | + |
| 52 | + # @returns [Exception] The cause of the stop operation. |
| 53 | + # |
| 54 | + # This is a compatibility method for Ruby versions before 3.5 where cause is not propagated correctly when using {Fiber#raise}, we explicitly capture the cause here. |
| 55 | + def cause |
| 56 | + super || @cause |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + # Used to defer stopping the current task until later. |
| 61 | + class Later |
| 62 | + # Create a new stop later operation. |
| 63 | + # |
| 64 | + # @parameter task [Task] The task to stop later. |
| 65 | + # @parameter cause [Exception] The cause of the stop operation. |
| 66 | + def initialize(task, cause = nil) |
| 67 | + @task = task |
| 68 | + @cause = cause |
| 69 | + end |
| 70 | + |
| 71 | + # @returns [Boolean] Whether the task is alive. |
| 72 | + def alive? |
| 73 | + true |
| 74 | + end |
| 75 | + |
| 76 | + # Transfer control to the operation - this will stop the task. |
| 77 | + def transfer |
| 78 | + @task.stop(false, cause: @cause) |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments