Class: PromptObjects::Execution::CancellationToken

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/execution/context.rb

Overview

Cooperative cancellation shared by a run and its attached descendants.

Instance Method Summary collapse

Constructor Details

#initializeCancellationToken

Returns a new instance of CancellationToken.



9
10
11
12
13
# File 'lib/prompt_objects/execution/context.rb', line 9

def initialize
  @cancelled = false
  @reason = nil
  @mutex = Mutex.new
end

Instance Method Details

#cancel(reason = "cancelled") ⇒ Object



15
16
17
18
19
20
# File 'lib/prompt_objects/execution/context.rb', line 15

def cancel(reason = "cancelled")
  @mutex.synchronize do
    @cancelled = true
    @reason = reason
  end
end

#cancelled?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/prompt_objects/execution/context.rb', line 22

def cancelled?
  @mutex.synchronize { @cancelled }
end

#check!Object

Raises:



30
31
32
# File 'lib/prompt_objects/execution/context.rb', line 30

def check!
  raise CancelledError, reason if cancelled?
end

#reasonObject



26
27
28
# File 'lib/prompt_objects/execution/context.rb', line 26

def reason
  @mutex.synchronize { @reason }
end