Skip to content

Commit

Permalink
Implement initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
HolyWalley committed Oct 6, 2024
1 parent f52cd27 commit 16fda90
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 26 deletions.
11 changes: 9 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.6
TargetRubyVersion: 3.1

Style/StringLiterals:
Enabled: true
Expand All @@ -10,4 +10,11 @@ Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes

Layout/LineLength:
Max: 120
Max: 140
Exclude:
- spec/**/*

Metrics/BlockLength:
CountAsOne: ["array", "heredoc", "method_call"]
Exclude:
- spec/**/*
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"

gem "rubocop", "~> 1.21"

gem "async-rspec", "~> 1.17"
83 changes: 83 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
PATH
remote: .
specs:
async-conditions_list (0.1.0)
async (~> 2.17)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
async (2.17.0)
console (~> 1.26)
fiber-annotation
io-event (~> 1.6, >= 1.6.5)
async-rspec (1.17.0)
rspec (~> 3.0)
rspec-files (~> 1.0)
rspec-memory (~> 1.0)
console (1.27.0)
fiber-annotation
fiber-local (~> 1.1)
json
diff-lcs (1.5.1)
fiber-annotation (0.2.0)
fiber-local (1.1.0)
fiber-storage
fiber-storage (1.0.0)
io-event (1.6.5)
json (2.7.2)
language_server-protocol (3.17.0.3)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
racc (1.8.1)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.2)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.1)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-files (1.1.3)
rspec (~> 3.0)
rspec-memory (1.0.4)
rspec (~> 3.0)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.66.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.4, < 3.0)
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (2.6.0)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
async-conditions_list!
async-rspec (~> 1.17)
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.21)

BUNDLED WITH
2.5.3
35 changes: 24 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
# Async::ConditionsList

TODO: Delete this and the text below, and describe your gem

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/async/conditions_list`. To experiment with that code, run `bin/console` for an interactive prompt.
An extension to Async gem that allows to create a list of condition, wait for it and receive an array of results

## Installation

TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.

Install the gem and add to the application's Gemfile by executing:

$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG

## Usage

TODO: Write usage instructions here
```ruby
require 'async'
require 'async/conditions_list'

conditions_list = Async::ConditionsList.new(3)

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

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

## Development

Expand All @@ -28,4 +41,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/async-conditions_list.
Bug reports and pull requests are welcome on GitHub at https://github.com/holywalley/async-conditions_list.
69 changes: 67 additions & 2 deletions lib/async/conditions_list.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,75 @@
# frozen_string_literal: true

require_relative "conditions_list/version"
require "async"

module Async
module ConditionsList
# 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.
#
class ConditionsList
class Error < StandardError; end
# Your code goes here...

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

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

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

@current_condition.signal(value)
end
end
end
2 changes: 1 addition & 1 deletion lib/async/conditions_list/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Async
module ConditionsList
class ConditionsList
VERSION = "0.1.0"
end
end
6 changes: 0 additions & 6 deletions sig/async/conditions_list.rbs

This file was deleted.

31 changes: 27 additions & 4 deletions spec/async/conditions_list_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
# frozen_string_literal: true

require "async"
require "async/conditions_list"
require "async/rspec"

RSpec.describe Async::ConditionsList do
it "has a version number" do
expect(Async::ConditionsList::VERSION).not_to be nil
include_context Async::RSpec::Reactor

let(:size) { 3 }
let(:conditions_list) { Async::ConditionsList.new(size) }

it "waits on all conditions in the list and returns composed result" do
reactor.async do |task|
wait_task = task.async { conditions_list.wait }

size.times { |i| conditions_list.signal(i) }

result = wait_task.wait

expect(result).to eq([0, 1, 2])
end
end

it "does something useful" do
expect(false).to eq(true)
describe "#signal" do
context "when no condition has been waited on" do
it "raises an Error" do
expect do
conditions_list.signal
end.to raise_error(Async::ConditionsList::Error, "No condition has been waited on yet.")
end
end
end
end

0 comments on commit 16fda90

Please sign in to comment.