return unless @requests;
my %req = map +("$_" => "$_"), @requests;
my $done;
- my %orig;
my %on_r = map {
- my $orig = $orig{$_} = $_->{on_result};
+ my $orig = $_->{on_result};
my $tag = $req{$_};
($_ => sub { delete $req{$tag}; $orig->(@_); $done = 1 unless keys %req; })
} @requests;
- $_->{on_result} = $on_r{$_} for @requests;
- $class->loop_until($done);
- $_->{on_result} = $orig{$_} for @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;
}