Skip to content

Commit

Permalink
improved Mojo::IOLoop::Delay to allow more method chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 3, 2014
1 parent 6546a5d commit ddcce0b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.86 2014-03-03
- Improved Mojo::IOLoop::Delay to allow more method chaining.

4.85 2014-02-26
- Added next_tick method to Mojo::IOLoop and Mojo::Reactor.
Expand Down
10 changes: 6 additions & 4 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -41,17 +41,19 @@ sub _step {
my ($self, $id) = (shift, shift);

$self->{args}[$id] = [@_];
return if $self->{fail} || --$self->{pending} || $self->{lock};
return $self if $self->{fail} || --$self->{pending} || $self->{lock};
local $self->{lock} = 1;
my @args = map {@$_} @{delete $self->{args}};

$self->{counter} = 0;
if (my $cb = shift @{$self->remaining}) {
eval { $self->$cb(@args); 1 } or return $self->emit(error => $@)->{fail}++;
eval { $self->$cb(@args); 1 }
or (++$self->{fail} and return $self->emit(error => $@));
}

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

1;
Expand Down Expand Up @@ -189,8 +191,8 @@ Data shared between all L</"steps">.
=head2 pass
$delay->pass;
$delay->pass(@args);
$delay = $delay->pass;
$delay = $delay->pass(@args);
Increment active event counter and decrement it again right away to pass
values to the next step.
Expand Down
12 changes: 6 additions & 6 deletions t/mojo/delay.t
Expand Up @@ -63,7 +63,7 @@ $delay->steps(
my $delay = shift;
my $end = $delay->begin;
$delay->begin->(3, 2, 1);
Mojo::IOLoop->next_tick(sub { $end->(1, 2, 3) });
Mojo::IOLoop->next_tick(sub { $end->(1, 2, 3)->pass(5) });
},
sub {
my ($delay, @numbers) = @_;
Expand All @@ -75,9 +75,9 @@ $delay->steps(
$result = \@numbers;
}
);
is_deeply [$delay->wait], [2, 3, 2, 1, 4], 'right return values';
is_deeply [$delay->wait], [2, 3, 2, 1, 4, 5], 'right return values';
is $finished, 1, 'finish event has been emitted once';
is_deeply $result, [2, 3, 2, 1, 4], 'right results';
is_deeply $result, [2, 3, 2, 1, 4, 5], 'right results';

# End chain after first step
($finished, $result) = ();
Expand Down Expand Up @@ -162,17 +162,17 @@ $delay = Mojo::IOLoop->delay(
my $end3 = $first->begin(0);
$end2->(4);
$end3->(5, 6);
$first->pass(23)->pass(24);
$end->(1, 2, 3);
$first->pass(23);
},
sub {
my ($first, @numbers) = @_;
push @$result, @numbers;
}
);
is_deeply [$delay->wait], [2, 3, 2, 1, 4, 5, 6, 23], 'right return values';
is_deeply [$delay->wait], [2, 3, 2, 1, 4, 5, 6, 23, 24], 'right return values';
is $finished, 1, 'finish event has been emitted once';
is_deeply $result, [1, 2, 3, 2, 3, 2, 1, 4, 5, 6, 23], 'right results';
is_deeply $result, [1, 2, 3, 2, 3, 2, 1, 4, 5, 6, 23, 24], 'right results';

# Dynamic step
my $double = sub {
Expand Down

0 comments on commit ddcce0b

Please sign in to comment.