Skip to content

Add Async::Group for straight forward list of children nodes without an associated task. #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/async/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2022, by Samuel Williams.

require_relative 'list'
require_relative 'task'

module Async
class Group < Node
def initialize(parent = Task.current, **options)
super(parent, **options)
end

# Execute a child task and add it to the barrier.
# @asynchronous Executes the given block concurrently.
def async(*arguments, **options, &block)
task = Task.new(self, **options, &block)

task.run(*arguments)

return task
end

# Wait for all children tasks to finish.
def wait
self.children.each do |child|
child.wait
end
end
end
end
25 changes: 25 additions & 0 deletions test/async/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.

require 'async/group'
require 'sus/fixtures/async'

describe Async::Group do
include Sus::Fixtures::Async::ReactorContext

let(:group) {subject.new}

it 'can wait for all tasks to finish' do
task = group.async do
sleep 0.001
end

expect(task.status).to be == :running

group.wait

expect(task.status).to be == :complete
end
end