X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FTak.pm;h=dfa19300a986dc6def3fc7e47538907a84e97741;hb=b1aab0cf42d520d69d4bd8cc8f7cc86d7d21f6d9;hp=33bc3784553f17a8c1cdefb5bd69f99fe8358c22;hpb=31a246e4b1ef71cc1991c1631573f6c80cdf41b5;p=scpubgit%2FTak.git diff --git a/lib/Tak.pm b/lib/Tak.pm index 33bc378..dfa1930 100644 --- a/lib/Tak.pm +++ b/lib/Tak.pm @@ -14,4 +14,25 @@ sub loop_until { $loop->loop_once until $_[1]; } +sub await_all { + my ($class, @requests) = @_; + @requests = grep !$_->is_done, @requests; + return unless @requests; + my %req = map +("$_" => "$_"), @requests; + my $done; + my %on_r = map { + my $orig = $_->{on_result}; + my $tag = $req{$_}; + ($_ => sub { delete $req{$tag}; $orig->(@_); $done = 1 unless keys %req; }) + } @requests; + my $call = sub { $class->loop_until($done) }; + foreach (@requests) { + my $req = $_; + my $inner = $call; + $call = sub { local $req->{on_result} = $on_r{$req}; $inner->() }; + } + $call->(); + return; +} + 1;