Skip to content

Commit

Permalink
improved Mojo::IOLoop to use Mojo::IOLoop::Delay more consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 27, 2014
1 parent 0c01c29 commit 01c7093
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.96 2014-04-28
- Improved Mojo::IOLoop to use Mojo::IOLoop::Delay more consistently.

4.95 2014-04-27
- Improved Mojo::IOLoop::Delay with circular reference protection.
Expand Down
11 changes: 4 additions & 7 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -82,11 +82,9 @@ sub client {
}

sub delay {
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;
weaken $delay->ioloop(_instance(shift))->{ioloop};
return @_ ? $delay->steps(@_) : $delay;
}

sub generate_port { Mojo::IOLoop::Server->generate_port }
Expand Down Expand Up @@ -437,9 +435,8 @@ L<Mojo::IOLoop::Client/"connect">.
Build L<Mojo::IOLoop::Delay> object to manage callbacks and control the flow
of events, which can help you avoid deep nested closures that often result
from continuation-passing style. A single callback will be treated as a
subscriber to the event L<Mojo::IOLoop::Delay/"finish">, and multiple ones as
a chain for L<Mojo::IOLoop::Delay/"steps">.
from continuation-passing style. Callbacks will be passed along to
L<Mojo::IOLoop::Delay/"steps">.
# Synchronize multiple events
my $delay = Mojo::IOLoop->delay(sub { say 'BOOM!' });
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -80,7 +80,7 @@ Mojo::IOLoop::Delay - Manage callbacks and control the flow of events
# Synchronize multiple events
my $delay = Mojo::IOLoop::Delay->new;
$delay->on(finish => sub { say 'BOOM!' });
$delay->steps(sub { say 'BOOM!' });
for my $i (1 .. 10) {
my $end = $delay->begin;
Mojo::IOLoop->timer($i => sub {
Expand Down
14 changes: 14 additions & 0 deletions t/mojo/delay.t
Expand Up @@ -93,6 +93,20 @@ is $finished, 1, 'finish event has been emitted once';
is_deeply $result, [2, 3, 2, 1, 4, 5], 'right results';
is $delay->data('foo'), 'bar', 'right value';

# One step
$result = undef;
$delay = Mojo::IOLoop::Delay->new;
$delay->steps(sub { ++$result });
$delay->begin->();
is $result, undef, 'no result';
Mojo::IOLoop->next_tick($delay->begin);
is $result, undef, 'no result';
$end = $delay->begin;
Mojo::IOLoop->next_tick(sub { $end->() });
is $result, undef, 'no result';
$delay->wait;
is $result, 1, 'right result';

# End chain after first step
($finished, $result) = ();
$delay = Mojo::IOLoop::Delay->new;
Expand Down

0 comments on commit 01c7093

Please sign in to comment.