Class: Async::ConditionsList

Inherits:
Object
  • Object
show all
Defined in:
lib/async/conditions_list.rb,
lib/async/conditions_list/version.rb

Overview

The ConditionsList class manages a collection of Async::Condition objects, allowing you to wait on multiple conditions and get accumulated result.

Initialization

To create a new ConditionsList, specify the number of conditions you want to manage:

conditions_list = Async::ConditionsList.new(size)

Methods

  • ‘wait`: Waits on all conditions in the list.

  • ‘signal(value = nil)`: Signals the current condition being waited on. If no condition has been waited on yet, it raises `ConditionsList::Error`.

Example Usage

require 'async'
require 'async/conditions_list'

conditions_list = Async::ConditionsList.new(3)

Sync do |task|
  task.async do
    conditions_list.wait
    puts "All conditions have been met!"
  end

  # Simulate asynchronous events
  task.sleep(1)
  conditions_list.signal
  task.sleep(1)
  conditions_list.signal
  task.sleep(1)
  conditions_list.signal
end

Error Handling

  • Raises ‘ConditionsList::Error` if `signal` is called before any condition has been waited on.

Notes

  • Ensure that the ‘wait` method is called before `signal` to avoid errors.

  • This class is intended to be used within the ‘Async` scheduler.

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ConditionsList

Returns a new instance of ConditionsList.



57
58
59
60
# File 'lib/async/conditions_list.rb', line 57

def initialize(size)
  @list = Array.new(size) { Async::Condition.new }
  @current_condition = nil
end

Instance Method Details

#signal(value = nil) ⇒ Object

Raises:



69
70
71
72
73
# File 'lib/async/conditions_list.rb', line 69

def signal(value = nil)
  raise Error, "No condition has been waited on yet." unless @current_condition

  @current_condition.signal(value)
end

#waitObject



62
63
64
65
66
67
# File 'lib/async/conditions_list.rb', line 62

def wait
  @list.map do |condition|
    @current_condition = condition
    condition.wait
  end
end