Skip to content

Commit

Permalink
preserve order of arguments in Mojo::IOLoop::Steps
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 17, 2012
1 parent 4797471 commit 138cb00
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

3.32 2012-08-17
- Added Mojo::IOLoop::Steps.
- Added Mojo::IOLoop::Steps. (judofyr, marcus, sri)
- Added steps method to Mojo::IOLoop.
- Added tap method to Mojo::Base.
- Improved documentation.
Expand Down
17 changes: 9 additions & 8 deletions lib/Mojo/IOLoop/Steps.pm
Expand Up @@ -10,20 +10,20 @@ sub new {
# "My god, it's full of geezers."
sub next {
my $self = shift;
$self->{counter}++;
return sub { shift; $self->_step(@_) };
my $id = $self->{counter}++;
return sub { shift; $self->_step($id, @_) };
}

sub _step {
my $self = shift;
my ($self, $id) = (shift, shift);

my $args = $self->{args} ||= [];
push @$args, @_;
$args->[$id] = [@_];

return unless --$self->{counter} <= 0;
return unless my $cb = shift @{$self->{steps}};
$self->{args} = [];
$self->$cb(@$args);
$self->$cb(map {@$_} @$args);
}

1;
Expand Down Expand Up @@ -83,9 +83,10 @@ Construct a new L<Mojo::IOLoop::Steps> object.
my $cb = $steps->next;
Generate callback for next step, all generated callbacks need to be invoked
before the next step can be reached. All arguments passed to the callback,
except for the first one, are queued and passed through to the next step.
Generate callback to next step, which will only be reached after all generated
callbacks have been invoked. The order in which callbacks have been generated
is preserved, and all arguments, except for the first one, will be queued and
passed through to the next step.
=head1 SEE ALSO
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/steps.t
Expand Up @@ -42,7 +42,7 @@ $steps = Mojo::IOLoop::Steps->new(
$result = \@numbers;
}
);
is_deeply $result, [2, 3, 2, 1], 'right numbers';
is_deeply $result, [2, 1, 2, 3], 'right numbers';

# Event loop
$result = undef;
Expand Down

0 comments on commit 138cb00

Please sign in to comment.