Class: CLI::UI::WorkQueue::Future

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/cli/ui/work_queue.rb

Instance Method Summary collapse

Methods included from T::Sig

sig

Constructor Details

#initializeFuture

Returns a new instance of Future.



13
14
15
16
17
18
19
20
# File 'lib/cli/ui/work_queue.rb', line 13

def initialize
  @mutex = T.let(Mutex.new, Mutex)
  @condition = T.let(ConditionVariable.new, ConditionVariable)
  @completed = T.let(false, T::Boolean)
  @started = T.let(false, T::Boolean)
  @result = T.let(nil, T.untyped)
  @error = T.let(nil, T.nilable(Exception))
end

Instance Method Details

#complete(result) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/cli/ui/work_queue.rb', line 23

def complete(result)
  @mutex.synchronize do
    @completed = true
    @result = result
    @condition.broadcast
  end
end

#completed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/cli/ui/work_queue.rb', line 53

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

#fail(error) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/cli/ui/work_queue.rb', line 32

def fail(error)
  @mutex.synchronize do
    return if @completed

    @completed = true
    @error = error
    @condition.broadcast
  end
end

#startObject



63
64
65
66
67
68
# File 'lib/cli/ui/work_queue.rb', line 63

def start
  @mutex.synchronize do
    @started = true
    @condition.broadcast
  end
end

#started?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cli/ui/work_queue.rb', line 58

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

#valueObject



43
44
45
46
47
48
49
50
# File 'lib/cli/ui/work_queue.rb', line 43

def value
  @mutex.synchronize do
    @condition.wait(@mutex) until @completed
    raise @error if @error

    @result
  end
end