Class: LittleGhost::Subagents::Manager::Completion

Inherits:
Object
  • Object
show all
Defined in:
lib/little_ghost/subagents/manager.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initializeCompletion

Returns a new instance of Completion.



102
103
104
105
106
# File 'lib/little_ghost/subagents/manager.rb', line 102

def initialize
  @mutex = Mutex.new
  @condition = ConditionVariable.new
  @resolved = false
end

Instance Method Details

#reject(error) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/little_ghost/subagents/manager.rb', line 118

def reject(error)
  @mutex.synchronize do
    return if @resolved

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

#resolve(value) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/little_ghost/subagents/manager.rb', line 108

def resolve(value)
  @mutex.synchronize do
    return if @resolved

    @value = value
    @resolved = true
    @condition.broadcast
  end
end

#value(cancellation_token: nil, deadline: nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/little_ghost/subagents/manager.rb', line 128

def value(cancellation_token: nil, deadline: nil)
  @mutex.synchronize do
    until @resolved
      cancellation_token&.raise_if_cancelled!
      if deadline && Time.now >= deadline
        raise DeadlineExceededError, "The run deadline was reached"
      end

      timeout = deadline ? [deadline - Time.now, 0.05].min : 0.05
      @condition.wait(@mutex, [timeout, 0].max)
    end
    raise @error if @error

    @value
  end
end