Skip to content

Commit

Permalink
allow all steps to increase active event counter
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 18, 2012
1 parent 58e042a commit 434199b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

3.32 2012-08-18
3.32 2012-08-19
- Added event sequentialization support to Mojo::IOLoop::Delay.
(judofyr, marcus, sri)
- Added tap method to Mojo::Base.
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -41,7 +41,7 @@ sub _step {
else { push @$unordered, @_ }

# Wait for more events
return $self->{counter} unless --$self->{counter} <= 0;
return $self->{counter} if --$self->{counter};

# Next step
my $cb = shift @{$self->{steps} ||= []};
Expand All @@ -50,7 +50,8 @@ sub _step {
$self->$cb(@args) if $cb;

# Finished
$self->emit('finish', @args) unless @{$self->{steps}} || $self->{finished}++;
$self->emit('finish', @args)
if !$self->{counter} && !@{$self->{steps}} && !$self->{finished}++;

return 0;
}
Expand Down
28 changes: 27 additions & 1 deletion t/mojo/delay.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 12;
use Test::More tests => 15;

# "And now to create an unstoppable army of between one million and two
# million zombies!"
Expand Down Expand Up @@ -73,6 +73,32 @@ is_deeply [$delay->wait], [2, 3, 2, 1, 4], 'right numbers';
is $finished, 1, 'finish event has been emitted once';
is_deeply $result, [2, 3, 2, 1, 4], 'right numbers';

# Finish steps with event
($result, $finished) = undef;
$delay = Mojo::IOLoop::Delay->new;
$delay->on(
finish => sub {
my ($delay, @numbers) = @_;
$result = \@numbers;
$finished++;
}
);
$delay->steps(
sub {
my $delay = shift;
my $cb = $delay->begin;
Mojo::IOLoop->timer(0 => sub { $cb->(1, 2, 3) });
},
sub {
my ($delay, @numbers) = @_;
my $cb = $delay->begin;
Mojo::IOLoop->timer(0 => sub { $cb->(undef, @numbers, 4) });
}
);
is_deeply [$delay->wait], [2, 3, 4], 'right numbers';
is $finished, 1, 'finish event has been emitted once';
is_deeply $result, [2, 3, 4], 'right numbers';

# Nested delays
($result, $finished) = undef;
$delay = Mojo::IOLoop->delay(
Expand Down

0 comments on commit 434199b

Please sign in to comment.