Class: Kward::DetachedRun
- Inherits:
-
Object
- Object
- Kward::DetachedRun
- Defined in:
- lib/kward/pty/detached_run.rb
Overview
Owns a command that continues after its terminal has been returned to Kward.
Instance Attribute Summary collapse
-
#sink ⇒ Object
readonly
Returns the value of attribute sink.
Instance Method Summary collapse
- #cancel! ⇒ Object
- #complete? ⇒ Boolean
-
#initialize(sink:, canceler: nil, &work) ⇒ DetachedRun
constructor
A new instance of DetachedRun.
- #join(timeout = nil) ⇒ Object
- #result ⇒ Object
Constructor Details
#initialize(sink:, canceler: nil, &work) ⇒ DetachedRun
Returns a new instance of DetachedRun.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kward/pty/detached_run.rb', line 9 def initialize(sink:, canceler: nil, &work) @sink = sink @canceler = canceler @mutex = Mutex.new @result = nil @thread = Thread.new do begin result = work.call @mutex.synchronize { @result = result } rescue StandardError => e @mutex.synchronize { @result = e } end end @thread.report_on_exception = false end |
Instance Attribute Details
#sink ⇒ Object (readonly)
Returns the value of attribute sink.
7 8 9 |
# File 'lib/kward/pty/detached_run.rb', line 7 def sink @sink end |
Instance Method Details
#cancel! ⇒ Object
40 41 42 |
# File 'lib/kward/pty/detached_run.rb', line 40 def cancel! @canceler&.call end |
#complete? ⇒ Boolean
25 26 27 |
# File 'lib/kward/pty/detached_run.rb', line 25 def complete? !@thread.alive? end |
#join(timeout = nil) ⇒ Object
36 37 38 |
# File 'lib/kward/pty/detached_run.rb', line 36 def join(timeout = nil) @thread.join(timeout) end |
#result ⇒ Object
29 30 31 32 33 34 |
# File 'lib/kward/pty/detached_run.rb', line 29 def result return unless complete? @thread.join @mutex.synchronize { @result } end |