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.



107
108
109
110
111
# File 'lib/little_ghost/subagents/manager.rb', line 107

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

Instance Method Details

#reject(error) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/little_ghost/subagents/manager.rb', line 123

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

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

#resolve(value) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/little_ghost/subagents/manager.rb', line 113

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

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

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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/little_ghost/subagents/manager.rb', line 133

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