Skip to content

Commit

Permalink
added data method to Mojo::IOLoop::Delay
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 22, 2014
1 parent 87ae27b commit 8cf8077
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

4.84 2014-02-22
- Added remaining attribute to Mojo::IOLoop::Delay.
- Added data method to Mojo::IOLoop::Delay.

4.83 2014-02-19
- Improved Mojo::JSON to handle encoding errors more gracefully.
Expand Down
28 changes: 28 additions & 0 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -13,6 +13,22 @@ sub begin {
return sub { $ignore // 1 and shift; $self->_step($id, @_) };
}

sub data {
my $self = shift;

# Hash
my $data = $self->{data} ||= {};
return $data unless @_;

# Get
return $data->{$_[0]} unless @_ > 1 || ref $_[0];

# Set
%$data = (%$data, %{ref $_[0] ? $_[0] : {@_}});

return $self;
}

sub steps {
my $self = shift->remaining([@_]);
$self->ioloop->timer(0 => $self->begin);
Expand Down Expand Up @@ -170,6 +186,18 @@ the first argument will be ignored by default.
Mojo::IOLoop->client({port => 3000} => $delay->begin(0));
my ($loop, $err, $stream) = $delay->wait;
=head2 data
my $hash = $delay->data;
my $foo = $delay->data('foo');
$delay = $delay->data({foo => 'bar'});
$delay = $delay->data(foo => 'bar');
Data shared between all L</"steps">.
# Remove value
my $foo = delete $delay->data->{foo};
=head2 steps
$delay = $delay->steps(sub {...}, sub {...});
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/daemon.t
Expand Up @@ -50,9 +50,9 @@ is $app->config('foo'), 'bar', 'right value';
delete $app->config->{foo};
is $app->config('foo'), undef, 'no value';
$app->config(foo => 'bar', baz => 'yada');
is_deeply $app->config, {foo => 'bar', baz => 'yada'}, 'right value';
$app->config({test => 23});
is $app->config->{test}, 23, 'right value';
is $app->config({test => 23})->config->{test}, 23, 'right value';
is_deeply $app->config, {foo => 'bar', baz => 'yada', test => 23},
'right value';

# Transaction
isa_ok $app->build_tx, 'Mojo::Transaction::HTTP', 'right class';
Expand Down
16 changes: 14 additions & 2 deletions t/mojo/delay.t
Expand Up @@ -23,6 +23,17 @@ $end2->();
is_deeply [$delay->wait], [], 'no return values';
is_deeply \@results, [1, 1], 'right results';

# Data
is $delay->data('foo'), undef, 'no value';
is_deeply $delay->data(foo => 'bar')->data, {foo => 'bar'}, 'right value';
is $delay->data('foo'), 'bar', 'right value';
delete $delay->data->{foo};
is $delay->data('foo'), undef, 'no value';
$delay->data(foo => 'bar', baz => 'yada');
is $delay->data({test => 23})->data->{test}, 23, 'right value';
is_deeply $delay->data, {foo => 'bar', baz => 'yada', test => 23},
'right value';

# Arguments
$delay = Mojo::IOLoop::Delay->new;
my $result;
Expand Down Expand Up @@ -170,11 +181,11 @@ my $double = sub {
Mojo::IOLoop->timer(0 => sub { $end->($num * 2) });
};
$result = undef;
$delay = Mojo::IOLoop::Delay->new->steps(
$delay = Mojo::IOLoop::Delay->new->data(num => 9)->steps(
sub {
my $delay = shift;
my $end = $delay->begin(0);
Mojo::IOLoop->timer(0 => sub { $end->(9) });
Mojo::IOLoop->timer(0 => sub { $end->($delay->data('num')) });
unshift @{$delay->remaining}, $double;
},
sub {
Expand All @@ -185,6 +196,7 @@ $delay = Mojo::IOLoop::Delay->new->steps(
is scalar @{$delay->remaining}, 2, 'two steps remaining';
is_deeply [$delay->wait], [18], 'right return values';
is scalar @{$delay->remaining}, 0, 'no steps remaining';
is $delay->data('num'), 9, 'right value';
is $result, 18, 'right result';

# Exception in first step
Expand Down

0 comments on commit 8cf8077

Please sign in to comment.