Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
deprecate Mojo::IOLoop::Delay::remaining
  • Loading branch information
kraih committed Oct 29, 2017
1 parent 62ffba5 commit 6be6023
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

7.49 2017-10-28
- Deprecated Mojo::IOLoop::Delay::data.
- Deprecated Mojo::IOLoop::Delay::data and Mojo::IOLoop::Delay::remaining.
- Added Promises/A+ support. Note that Mojo::IOLoop::Delay previously
inherited a catch method from Mojo::EventEmitter that was passed the error
message as second argument instead of the first, so you might have to change
Expand Down
41 changes: 22 additions & 19 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -5,8 +5,7 @@ use Mojo::IOLoop;
use Mojo::Util 'deprecated';
use Scalar::Util qw(blessed weaken);

has ioloop => sub { Mojo::IOLoop->singleton };
has remaining => sub { [] };
has ioloop => sub { Mojo::IOLoop->singleton };

sub all {
my @promises = @_;
Expand Down Expand Up @@ -64,11 +63,22 @@ sub race {
return $race;
}

sub reject { shift->_settle('reject', @_) }
sub reject { shift->_settle('reject', @_) }

# DEPRECATED!
sub remaining {
deprecated 'Mojo::IOLoop::Delay::remaining is deprecated';
my $self = shift;
return $self->{steps} ||= [] unless @_;
$self->{steps} = shift;
return $self;
}

sub resolve { shift->_settle('resolve', @_) }

sub steps {
my $self = shift->remaining([@_]);
my ($self, @steps) = @_;
$self->{steps} = \@steps;
$self->ioloop->next_tick($self->begin);
return $self;
}
Expand Down Expand Up @@ -135,15 +145,15 @@ sub _step {
my @args = map {@$_} @{delete $self->{args}};

$self->{counter} = 0;
if (my $cb = shift @{$self->remaining}) {
if (my $cb = shift @{$self->{steps}}) {
unless (eval { $self->$cb(@args); 1 }) {
my $err = $@;
$self->{fail}++;
return $self->remaining([])->reject($err)->emit(error => $err);
@{$self}{qw(fail steps)} = (1, []);
return $self->reject($err)->emit(error => $err);
}
}

return $self->remaining([])->resolve(@args)->emit(finish => @args)
($self->{steps} = []) and return $self->resolve(@args)->emit(finish => @args)
unless $self->{counter};
$self->ioloop->next_tick($self->begin) unless $self->{pending};
return $self;
Expand Down Expand Up @@ -325,13 +335,6 @@ L<Mojo::IOLoop::Delay> implements the following attributes.
Event loop object to control, defaults to the global L<Mojo::IOLoop> singleton.
=head2 remaining
my $remaining = $delay->remaining;
$delay = $delay->remaining([sub {...}]);
Remaining L</"steps"> in chain.
=head1 METHODS
L<Mojo::IOLoop::Delay> inherits all methods from L<Mojo::EventEmitter> and
Expand Down Expand Up @@ -473,10 +476,10 @@ Resolve the promise with one or more fulfillment values.
Sequentialize multiple events, every time the event counter reaches zero a
callback will run, the first one automatically runs during the next reactor tick
unless it is delayed by incrementing the event counter. This chain will continue
until there are no L</"remaining"> callbacks, a callback does not increment the
event counter or an exception gets thrown in a callback. Finishing the chain
will also result in the promise being fulfilled, or if an exception got thrown
it will be rejected.
until there are no remaining callbacks, a callback does not increment the event
counter or an exception gets thrown in a callback. Finishing the chain will also
result in the promise being fulfilled, or if an exception got thrown it will be
rejected.
=head2 then
Expand Down
29 changes: 0 additions & 29 deletions t/mojo/delay.t
Expand Up @@ -292,7 +292,6 @@ $delay->steps(
sub { shift; push @results, [@_] },
sub { push @results, 'fail' }
)->wait;
is_deeply $delay->remaining, [], 'no remaining steps';
is_deeply \@results, [[23], [23]], 'right results';

# Finish steps with event
Expand Down Expand Up @@ -350,38 +349,13 @@ $delay = Mojo::IOLoop->delay(
is $finished, 1, 'finish event has been emitted once';
is_deeply $result, [1, 2, 3, 2, 3, 2, 1, 4, 5, 6, 23, 24], 'right results';

# Dynamic step
my $double = sub {
my ($delay, $num) = @_;
my $end = $delay->begin(0);
Mojo::IOLoop->next_tick(sub { $end->($num * 2) });
};
$result = undef;
$delay = Mojo::IOLoop::Delay->new->steps(
sub {
my $delay = shift;
my $end = $delay->begin(0);
Mojo::IOLoop->next_tick(sub { $end->(9) });
unshift @{$delay->remaining}, $double;
},
sub {
my ($delay, $num) = @_;
$result = $num;
}
);
is scalar @{$delay->remaining}, 2, 'two steps remaining';
$delay->wait;
is scalar @{$delay->remaining}, 0, 'no steps remaining';
is $result, 18, 'right result';

# Exception in first step
my $failed;
($finished, $result) = ();
$delay = Mojo::IOLoop::Delay->new;
$delay->on(error => sub { $failed = pop });
$delay->on(finish => sub { $finished++ });
$delay->steps(sub { die 'First step!' }, sub { $result = 'failed' })->wait;
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^First step!/, 'right error';
ok !$finished, 'finish event has not been emitted';
ok !$result, 'no result';
Expand All @@ -393,7 +367,6 @@ $delay->on(error => sub { $failed = pop });
$delay->on(finish => sub { $finished++ });
$delay->steps(sub { Mojo::IOLoop->next_tick(shift->begin) },
sub { die 'Last step!' })->wait;
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^Last step!/, 'right error';
ok !$finished, 'finish event has not been emitted';

Expand All @@ -416,7 +389,6 @@ $delay->steps(
);
$delay->on(error => sub { $failed = pop });
$delay->wait;
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^Second step!/, 'right error';
ok !$finished, 'finish event has not been emitted';
is $result, 'pass', 'right result';
Expand All @@ -436,7 +408,6 @@ $delay->steps(
sub { $result = 'failed' }
);
Mojo::IOLoop->start;
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^Second step!/, 'right error';
ok !$finished, 'finish event has not been emitted';
ok !$result, 'no result';
Expand Down
2 changes: 1 addition & 1 deletion t/pod_coverage.t
Expand Up @@ -8,4 +8,4 @@ plan skip_all => 'Test::Pod::Coverage 1.04+ required for this test!'
unless eval 'use Test::Pod::Coverage 1.04; 1';

# DEPRECATED!
all_pod_coverage_ok({also_private => ['data']});
all_pod_coverage_ok({also_private => ['data', 'remaining']});

0 comments on commit 6be6023

Please sign in to comment.