Skip to content

Commit

Permalink
improved Mojo::IOLoop::Delay to allow argument splicing
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 25, 2014
1 parent 5e24045 commit 27cbd2e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

4.95 2014-04-25
4.95 2014-04-26
- Improved Mojo::IOLoop::Delay to allow argument splicing.
- Improved Mojo::IOLoop::Server to reuse cipher list from IO::Socket::SSL.

4.94 2014-04-20
Expand Down
4 changes: 1 addition & 3 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -82,12 +82,10 @@ sub client {
}

sub delay {
my $self = _instance(shift);

my $self = _instance(shift);
my $delay = Mojo::IOLoop::Delay->new;
weaken $delay->ioloop($self)->{ioloop};
@_ > 1 ? $delay->steps(@_) : $delay->once(finish => shift) if @_;

return $delay;
}

Expand Down
31 changes: 22 additions & 9 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -8,10 +8,10 @@ has ioloop => sub { Mojo::IOLoop->singleton };
has remaining => sub { [] };

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

sub data { shift->Mojo::_dict(data => @_) }
Expand All @@ -38,9 +38,10 @@ sub wait {
sub _die { $_[0]->has_subscribers('error') ? $_[0]->ioloop->stop : die $_[1] }

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

$self->{args}[$id] = [@_];
$self->{args}[$id]
= [defined $len ? splice(@_, $offset, $len) : splice(@_, $offset)];
return $self if $self->{fail} || --$self->{pending} || $self->{lock};
local $self->{lock} = 1;
my @args = map {@$_} @{delete $self->{args}};
Expand Down Expand Up @@ -164,19 +165,31 @@ implements the following new ones.
=head2 begin
my $without_first_arg = $delay->begin;
my $with_first_arg = $delay->begin(0);
my $cb = $delay->begin;
my $cb = $delay->begin($offset);
my $cb = $delay->begin($offset, $len);
Increment active event counter, the returned callback can be used to decrement
the active event counter again. Arguments passed to the callback are queued in
the right order for the next step or L</"finish"> event and L</"wait"> method,
the first argument will be ignored by default.
the active event counter again. Arguments passed to the callback are spliced
and queued in the right order for the next step or L</"finish"> event and
L</"wait"> method, the argument offset defaults to C<1> with no default
length.
# Capture all arguments except for the first one (invocant)
my $delay = Mojo::IOLoop->delay;
Mojo::IOLoop->client({port => 3000} => $delay->begin);
my ($err, $stream) = $delay->wait;
# Capture all arguments
my $delay = Mojo::IOLoop->delay;
Mojo::IOLoop->client({port => 3000} => $delay->begin(0));
my ($loop, $err, $stream) = $delay->wait;
# Capture only the second argument
my $delay = Mojo::IOLoop->delay;
Mojo::IOLoop->client({port => 3000} => $delay->begin(1, 1));
my $err = $delay->wait;
=head2 data
my $hash = $delay->data;
Expand Down
12 changes: 12 additions & 0 deletions t/mojo/delay.t
Expand Up @@ -23,6 +23,18 @@ $end2->();
is_deeply [$delay->wait], [], 'no return values';
is_deeply \@results, [1, 1], 'right results';

# Argument splicing
$delay = Mojo::IOLoop::Delay->new;
Mojo::IOLoop->next_tick($delay->begin);
$delay->begin(1)->(1, 2, 3);
$delay->begin(1, 1)->(4, 5, 6);
$delay->begin(0, 1)->(7, 8);
$delay->begin(2)->(9, 10, 11);
$delay->begin(0, 0)->(12, 13);
$delay->begin(0, 2)->(14, 15, 16);
$delay->begin(2, 5)->(17, 18, 19, 20);
is_deeply [$delay->wait], [2, 3, 5, 7, 11, 14, 15, 19, 20], 'right values';

# Data
is $delay->data('foo'), undef, 'no value';
is_deeply $delay->data(foo => 'bar')->data, {foo => 'bar'}, 'right value';
Expand Down

0 comments on commit 27cbd2e

Please sign in to comment.