Class: Async::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/async/variable.rb

Instance Method Summary collapse

Constructor Details

#initialize(condition = Condition.new) ⇒ Variable

Returns a new instance of Variable.



10
11
12
13
# File 'lib/async/variable.rb', line 10

def initialize(condition = Condition.new)
  @condition = condition
  @value = nil
end

Instance Method Details

#resolve(value = true) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/async/variable.rb', line 15

def resolve(value = true)
  @value = value
  condition = @condition
  @condition = nil
  
  self.freeze
  
  condition.signal(value)
end

#resolved?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/async/variable.rb', line 25

def resolved?
  @condition.nil?
end

#valueObject



29
30
31
32
# File 'lib/async/variable.rb', line 29

def value
  @condition&.wait
  return @value
end

#waitObject



34
35
36
# File 'lib/async/variable.rb', line 34

def wait
  self.value
end