Skip to content
Kward Search API index

Class: Kward::DetachedRun

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#sinkObject (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

Returns:

  • (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

#resultObject



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