Class: Async::Variable Deprecated

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

Overview

Deprecated.

Use Promise instead.

A synchronization primitive that allows one task to wait for another task to resolve a value.

Instance Method Summary collapse

Constructor Details

#initialize(condition = Condition.new) ⇒ Variable

Create a new variable.



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

def initialize(condition = Condition.new)
  warn("`Async::Variable` is deprecated, use `Async::Promise` instead.", category: :deprecated, uplevel: 1) if $VERBOSE
  
  @condition = condition
  @value = nil
end

Instance Method Details

#resolve(value = true) ⇒ Object

Resolve the value.

Signals all waiting tasks.



29
30
31
32
33
34
35
36
37
# File 'lib/async/variable.rb', line 29

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

#resolved?Boolean

Whether the value has been resolved.

Returns:

  • (Boolean)


47
48
49
# File 'lib/async/variable.rb', line 47

def resolved?
  @condition.nil?
end

#valueObject

Alias for #wait.



60
61
62
# File 'lib/async/variable.rb', line 60

def value
  self.wait
end

#value=(value) ⇒ Object

Alias for #resolve.



40
41
42
# File 'lib/async/variable.rb', line 40

def value=(value)
  self.resolve(value)
end

#waitObject

Wait for the value to be resolved.



54
55
56
57
# File 'lib/async/variable.rb', line 54

def wait
  @condition&.wait
  return @value
end