Skip to content

Commit

Permalink
fixed another timing bug in Mojo::IOLoop::Delay
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 5, 2013
1 parent 3017f01 commit 3afd358
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

3.93 2013-04-04
3.93 2013-04-05
- Deprecated Mojo::IOLoop::Delay::end in favor of generated callbacks.
- Improved Mojo::IOLoop::Delay to be able to generate callbacks that can
capture all arguments.
Expand Down
18 changes: 11 additions & 7 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -8,8 +8,8 @@ has ioloop => sub { Mojo::IOLoop->singleton };

sub begin {
my ($self, $ignore) = @_;
$self->{counter}++;
my $id = $self->{id}++;
$self->{pending}++;
my $id = $self->{counter}++;
return sub { $ignore // 1 and shift; $self->_step($id, @_) };
}

Expand Down Expand Up @@ -43,17 +43,21 @@ sub _step {
# DEPRECATED in Rainbow!
else { push @{$self->{unordered}}, @_ }

return $self->{counter} if --$self->{counter} || $self->{step};

return $self->{pending} if --$self->{pending} || $self->{step};
my @args = (map {@$_} grep {defined} @{delete($self->{args}) || []});

# DEPRECATED in Rainbow!
push @args, @{delete($self->{unordered}) || []};

local $self->{step} = 1;
if (my $cb = shift @{$self->{steps} ||= []}) { $self->$cb(@args) }
$self->emit(finish => @args) unless $self->{counter};
$self->{counter} = 0;
if (my $cb = shift @{$self->{steps} ||= []}) {
local $self->{step} = 1;
$self->$cb(@args);
}

return 0 if $self->{pending};
if ($self->{counter}) { $self->ioloop->timer(0 => $self->begin) }
else { $self->emit(finish => @args) unless $self->{counter} }
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions t/mojo/delay.t
Expand Up @@ -78,18 +78,18 @@ $delay->wait;
is $finished, 1, 'finish event has been emitted once';
is $result, 'success', 'right result';

# End chain after second step
# End chain after third step
my $remaining;
($finished, $result) = ();
$delay = Mojo::IOLoop::Delay->new;
$delay->on(finish => sub { $finished++ });
$delay->steps(
sub { Mojo::IOLoop->timer(0 => shift->begin) },
sub {
$result = 'success';
$result = 'fail';
$remaining = shift->begin->();
},
sub { $result = 'fail' },
sub { $result = 'success' },
sub { $result = 'fail' }
);
$delay->wait;
Expand Down

0 comments on commit 3afd358

Please sign in to comment.