Skip to content

Commit

Permalink
simplify Mojo::IOLoop::Steps
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 16, 2012
1 parent eab886e commit d92891e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 63 deletions.
22 changes: 13 additions & 9 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -552,19 +552,23 @@ Get L<Mojo::IOLoop::Steps> object to control the flow of events.
# Control the flow of multiple events
Mojo::IOLoop->steps(
# First step
sub {
my $steps = shift;
Mojo::IOLoop->timer(3 => $steps->next);
Mojo::IOLoop->timer(1 => $steps->next);
my $next = shift;
say 'Waiting 2 seconds.';
Mojo::IOLoop->timer(2 => $next);
},
# Second step
sub {
my ($steps, @args) = @_;
Mojo::IOLoop->timer(2 => $steps->next);
my $next = shift;
say 'Waiting 3 seconds.';
Mojo::IOLoop->timer(3 => $next);
},
sub {
my ($steps, @args) = @_;
say "Thank you for waiting 5 seconds.";
}
# Third step
sub { say 'And done after 5 seconds.' }
);
=head2 C<stop>
Expand Down
48 changes: 20 additions & 28 deletions lib/Mojo/IOLoop/Steps.pm
Expand Up @@ -3,29 +3,22 @@ use Mojo::Base -base;

sub new {
my $self = shift->SUPER::new(steps => [@_]);
$self->next->();
$self->_step();
return $self;
}

# "My god, it's full of geezers."
sub next {
my $self = shift;
$self->{counter}++;
return sub { shift; $self->_step(@_) };
}

sub _step {
my $self = shift;

# Cache arguments
# Arguments
my $args = $self->{args} ||= [];
push @$args, @_;
$self->{args} = [];

# Next step
return unless --$self->{counter} <= 0;
return unless my $cb = shift @{$self->{steps}};
$self->{args} = [];
$self->$cb(@$args);
$cb->(sub { shift; $self->_step(@_) }, @$args);
}

1;
Expand All @@ -39,19 +32,26 @@ Mojo::IOLoop::Steps - Control flow of events
use Mojo::IOLoop::Steps;
# Control the flow of multiple events
my $steps = Mojo::IOLoop::Steps->new(
Mojo::IOLoop::Steps->new(
# First step
sub {
my $steps = shift;
Mojo::IOLoop->timer(3 => $steps->next);
Mojo::IOLoop->timer(1 => $steps->next);
my $next = shift;
say 'Waiting 2 seconds.';
Mojo::IOLoop->timer(2 => $next);
},
# Second step
sub {
my ($steps, @args) = @_;
Mojo::IOLoop->timer(2 => $steps->next);
my ($next, @args) = @_;
say 'Waiting 3 seconds.';
Mojo::IOLoop->timer(3 => $next);
},
# Third step
sub {
my ($steps, @args) = @_;
say "Thank you for waiting 5 seconds.";
my ($next, @args) = @_;
say 'And done after 5 seconds.';
}
);
Expand All @@ -60,7 +60,7 @@ Mojo::IOLoop::Steps - Control flow of events
=head1 DESCRIPTION
L<Mojo::IOLoop::Delay> controls the flow of events for L<Mojo::IOLoop>.
L<Mojo::IOLoop::Steps> controls the flow of events for L<Mojo::IOLoop>.
=head1 METHODS
Expand All @@ -73,14 +73,6 @@ the following new ones.
Construct a new L<Mojo::IOLoop::Steps> object.
=head2 C<next>
my $cb = $steps->next;
Generate callback for getting to the next step. If more than one is generated,
they all have to be invoked before the next step can be reached. Note that the
first argument passed to the callback will be ignored.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
35 changes: 9 additions & 26 deletions t/mojo/steps.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 3;
use Test::More tests => 2;

# "It's not just safe, it's 40% safe!"
use Mojo::IOLoop;
Expand All @@ -16,47 +16,30 @@ use Mojo::IOLoop::Steps;
my $result;
my $steps = Mojo::IOLoop::Steps->new(
sub {
my $steps = shift;
$steps->next->(1, 2, 3);
my $next = shift;
$next->(1, 2, 3);
},
sub {
my ($steps, @numbers) = @_;
my ($next, @numbers) = @_;
$result = \@numbers;
$next->();
}
);
is_deeply $result, [2, 3], 'right numbers';

# Multiple steps
$result = undef;
$steps = Mojo::IOLoop::Steps->new(
sub {
my $steps = shift;
my $cb = $steps->next;
$steps->next->(1, 2, 3);
$cb->(3, 2, 1);
},
sub { shift->next->(@_) },
sub {
my ($steps, @numbers) = @_;
$result = \@numbers;
}
);
is_deeply $result, [2, 3, 2, 1], 'right numbers';

# Event loop
$result = undef;
$steps = Mojo::IOLoop->steps(
sub {
my $steps = shift;
my $delay = Mojo::IOLoop->delay;
$delay->on(finish => $steps->next);
my $next = shift;
my $delay = Mojo::IOLoop->delay($next);
Mojo::IOLoop->timer(0 => $delay->begin);
Mojo::IOLoop->timer(0 => $steps->next);
$delay->begin;
Mojo::IOLoop->timer(0 => sub { $delay->end(1, 2, 3) });
},
sub { shift->(@_) },
sub {
my ($steps, @numbers) = @_;
my ($next, @numbers) = @_;
$result = \@numbers;
}
);
Expand Down

0 comments on commit d92891e

Please sign in to comment.