Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Revert "deprecate Mojo::IOLoop::delay in favor of Mojo::IOLoop::Delay…
…::delay"

This reverts commit bc9b000.
  • Loading branch information
kraih committed Oct 24, 2017
1 parent bc9b000 commit 82b0f09
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 75 deletions.
2 changes: 0 additions & 2 deletions Changes
@@ -1,7 +1,5 @@

7.49 2017-10-21
- Deprecated Mojo::IOLoop::delay in favor of Mojo::IOLoop::Delay::delay.
- Added delay function to Mojo::IOLoop::Delay.
- Updated jQuery to version 3.2.1.

7.48 2017-10-19
Expand Down
65 changes: 61 additions & 4 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -10,7 +10,7 @@ use Mojo::IOLoop::Server;
use Mojo::IOLoop::Stream;
use Mojo::IOLoop::Subprocess;
use Mojo::Reactor::Poll;
use Mojo::Util qw(deprecated md5_sum steady_time);
use Mojo::Util qw(md5_sum steady_time);
use Scalar::Util qw(blessed weaken);

use constant DEBUG => $ENV{MOJO_IOLOOP_DEBUG} || 0;
Expand Down Expand Up @@ -67,10 +67,7 @@ sub client {
return $id;
}

# DEPRECATED!
sub delay {
deprecated 'Mojo::IOLoop::delay is DEPRECATED'
. ' in favor of Mojo::IOLoop::Delay::delay';
my $delay = Mojo::IOLoop::Delay->new;
weaken $delay->ioloop(_instance(shift))->{ioloop};
return @_ ? $delay->steps(@_) : $delay;
Expand Down Expand Up @@ -402,6 +399,66 @@ takes the same arguments as L<Mojo::IOLoop::Client/"connect">.
...
});
=head2 delay
my $delay = Mojo::IOLoop->delay;
my $delay = $loop->delay;
my $delay = $loop->delay(sub {...});
my $delay = $loop->delay(sub {...}, sub {...});
Build L<Mojo::IOLoop::Delay> object to manage callbacks and control the flow of
events for this event loop, which can help you avoid deep nested closures that
often result from continuation-passing style. Callbacks will be passed along to
L<Mojo::IOLoop::Delay/"steps">.
# Synchronize multiple non-blocking operations
my $delay = Mojo::IOLoop->delay(sub { say 'BOOM!' });
for my $i (1 .. 10) {
my $end = $delay->begin;
Mojo::IOLoop->timer($i => sub {
say 10 - $i;
$end->();
});
}
$delay->wait;
# Sequentialize multiple non-blocking operations
Mojo::IOLoop->delay(
# First step (simple timer)
sub {
my $delay = shift;
Mojo::IOLoop->timer(2 => $delay->begin);
say 'Second step in 2 seconds.';
},
# Second step (concurrent timers)
sub {
my $delay = shift;
Mojo::IOLoop->timer(1 => $delay->begin);
Mojo::IOLoop->timer(3 => $delay->begin);
say 'Third step in 3 seconds.';
},
# Third step (the end)
sub { say 'And done after 5 seconds total.' }
)->wait;
# Handle exceptions in all steps
Mojo::IOLoop->delay(
sub {
my $delay = shift;
die 'Intentional error';
},
sub {
my ($delay, @args) = @_;
say 'Never actually reached.';
}
)->catch(sub {
my ($delay, $err) = @_;
say "Something went wrong: $err";
})->wait;
=head2 is_running
my $bool = Mojo::IOLoop->is_running;
Expand Down
42 changes: 11 additions & 31 deletions lib/Mojo/IOLoop/Delay.pm
@@ -1,15 +1,12 @@
package Mojo::IOLoop::Delay;
use Mojo::Base 'Mojo::EventEmitter';

use Exporter 'import';
use Mojo::IOLoop;
use Mojo::Util;

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

our @EXPORT_OK = ('delay');

sub begin {
my ($self, $offset, $len) = @_;
$self->{pending}++;
Expand All @@ -19,8 +16,6 @@ sub begin {

sub data { Mojo::Util::_stash(data => @_) }

sub delay { @_ ? __PACKAGE__->new->steps(@_) : __PACKAGE__->new }

sub pass { $_[0]->begin->(@_) }

sub steps {
Expand Down Expand Up @@ -65,7 +60,7 @@ sub _step {
=head1 NAME
Mojo::IOLoop::Delay - Flow-control utilities
Mojo::IOLoop::Delay - Manage callbacks and control the flow of events
=head1 SYNOPSIS
Expand Down Expand Up @@ -108,9 +103,8 @@ Mojo::IOLoop::Delay - Flow-control utilities
}
)->wait;
# Use the alternative constructor (and handle exceptions in all steps)
use Mojo::IOLoop::Delay 'delay';
delay(
# Handle exceptions in all steps
Mojo::IOLoop::Delay->new->steps(
sub {
my $delay = shift;
die 'Intentional error';
Expand All @@ -126,8 +120,9 @@ Mojo::IOLoop::Delay - Flow-control utilities
=head1 DESCRIPTION
L<Mojo::IOLoop::Delay> is a set of flow-control utilities that can help you
avoid deep nested closures that often result from continuation-passing style.
L<Mojo::IOLoop::Delay> manages callbacks and controls the flow of events for
L<Mojo::IOLoop>, which can help you avoid deep nested closures that often
result from continuation-passing style.
use Mojo::IOLoop;
Expand Down Expand Up @@ -157,10 +152,9 @@ a code reference that we can pass to L<Mojo::IOLoop/"timer"> as a callback, and
that leads to the next closure in the series when executed.
use Mojo::IOLoop;
use Mojo::IOLoop::Delay 'delay';
# Instead of nested closures we now have a simple chain
my $delay = delay(
my $delay = Mojo::IOloop->delay(
sub {
my $delay = shift;
Mojo::IOLoop->timer(3 => $delay->begin);
Expand Down Expand Up @@ -211,20 +205,6 @@ fatal if unhandled.
Emitted once the event counter reaches zero and there are no more steps.
=head1 FUNCTIONS
L<Mojo::IOLoop::Delay> implements the following functions, which can be imported
individually.
=head2 delay
my $delay = delay;
my $delay = delay(sub {...});
my $delay = delay(sub {...}, sub {...});
Construct a new L<Mojo::IOLoop::Delay> object. Callbacks will be passed along to
L</"steps>.
=head1 ATTRIBUTES
L<Mojo::IOLoop::Delay> implements the following attributes.
Expand Down Expand Up @@ -261,7 +241,7 @@ references generated by this method have been executed and the event counter has
reached zero, L</"steps"> will continue.
# Capture all arguments except for the first one (invocant)
my $delay = delay(sub {
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, $err, $stream) = @_;
...
});
Expand All @@ -274,23 +254,23 @@ arguments are then combined in the same order L</"begin"> was called, and passed
together to the next step or L</"finish"> event.
# Capture all arguments
my $delay = delay(sub {
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, $loop, $err, $stream) = @_;
...
});
Mojo::IOLoop->client({port => 3000} => $delay->begin(0));
$delay->wait;
# Capture only the second argument
my $delay = delay(sub {
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, $err) = @_;
...
});
Mojo::IOLoop->client({port => 3000} => $delay->begin(1, 1));
$delay->wait;
# Capture and combine arguments
my $delay = delay(sub {
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, $three_err, $three_stream, $four_err, $four_stream) = @_;
...
});
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -404,8 +404,7 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
# Concurrent non-blocking requests (synchronized with a delay)
use Mojo::IOLoop::Delay 'delay';
delay(
Mojo::IOLoop->delay(
sub {
my $delay = shift;
$ua->get('mojolicious.org' => $delay->begin);
Expand Down
10 changes: 4 additions & 6 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1255,7 +1255,6 @@ batches.

use Mojo::UserAgent;
use Mojo::IOLoop;
use Mojo::IOLoop::Delay 'delay';

my @urls = (
'mojolicious.org/perldoc/Mojo/DOM', 'mojolicious.org/perldoc/Mojo',
Expand All @@ -1267,7 +1266,7 @@ batches.
$ua->transactor->name('MyParallelCrawler 1.0');

# Use a delay to keep the event loop running until we are done
my $delay = delay;
my $delay = Mojo::IOLoop->delay;
my $fetch;
$fetch = sub {

Expand Down Expand Up @@ -1296,16 +1295,15 @@ the same host, or the operators might be forced to block your access.

=head2 Concurrent blocking requests

You can emulate blocking behavior by using L<Mojo::IOLoop::Delay> to synchronize
multiple non-blocking requests.
You can emulate blocking behavior by using L<Mojo::IOLoop/"delay"> to
synchronize multiple non-blocking requests.

use Mojo::UserAgent;
use Mojo::IOLoop;
use Mojo::IOLoop::Delay 'delay';

# Synchronize non-blocking requests
my $ua = Mojo::UserAgent->new;
my $delay = delay(sub {
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, $mojo, $minion) = @_;
say $mojo->result->dom->at('title')->text;
say $minion->result->dom->at('title')->text;
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -5,7 +5,6 @@ use Mojo::ByteStream;
use Mojo::Collection;
use Mojo::Exception;
use Mojo::IOLoop;
use Mojo::IOLoop::Delay 'delay';
use Mojo::Util qw(dumper hmac_sha1_sum steady_time);
use Scalar::Util 'blessed';

Expand Down Expand Up @@ -81,7 +80,7 @@ sub _current_route {
sub _delay {
my $c = shift;
my $tx = $c->render_later->tx;
my $delay = delay(@_);
my $delay = Mojo::IOLoop->delay(@_);
$delay->catch(sub { $c->helpers->reply->exception(pop) and undef $tx })->wait;
}

Expand Down Expand Up @@ -304,7 +303,7 @@ Check or get name of current route.
$c->delay(sub {...}, sub {...});
Disable automatic rendering and use L<Mojo::IOLoop::Delay> to manage callbacks
Disable automatic rendering and use L<Mojo::IOLoop/"delay"> to manage callbacks
and control the flow of events, which can help you avoid deep nested closures
that often result from continuation-passing style. Also keeps a reference to
L<Mojolicious::Controller/"tx"> in case the underlying connection gets closed
Expand All @@ -314,7 +313,7 @@ of the steps, breaking the chain.
# Longer version
$c->render_later;
my $tx = $c->tx;
my $delay = delay(sub {...}, sub {...});
my $delay = Mojo::IOLoop->delay(sub {...}, sub {...});
$delay->catch(sub { $c->reply->exception(pop) and undef $tx })->wait;
# Non-blocking request
Expand Down
3 changes: 1 addition & 2 deletions t/mojo/daemon.t
Expand Up @@ -11,7 +11,6 @@ use IO::Socket::INET;
use Mojo;
use Mojo::File 'path';
use Mojo::IOLoop;
use Mojo::IOLoop::Delay 'delay';
use Mojo::Log;
use Mojo::Server::Daemon;
use Mojo::UserAgent;
Expand Down Expand Up @@ -203,7 +202,7 @@ is $tx->res->body, 'Whatever!', 'right content';

# Concurrent requests
my ($tx2, $tx3);
my $delay = delay(sub { (undef, $tx, $tx2, $tx3) = @_ });
my $delay = Mojo::IOLoop->delay(sub { (undef, $tx, $tx2, $tx3) = @_ });
$ua->get('/concurrent1/' => $delay->begin);
$ua->post(
'/concurrent2/' => {Expect => 'fun'} => 'bar baz foo' x 128 => $delay->begin);
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/delay.t
Expand Up @@ -4,7 +4,7 @@ BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;
use Mojo::IOLoop;
use Mojo::IOLoop::Delay 'delay';
use Mojo::IOLoop::Delay;

# Basic functionality
my $delay = Mojo::IOLoop::Delay->new;
Expand Down Expand Up @@ -157,11 +157,11 @@ is_deeply $result, [2, 3, 4], 'right results';

# Nested delays
($finished, $result) = ();
$delay = delay(
$delay = Mojo::IOLoop->delay(
sub {
my $first = shift;
$first->on(finish => sub { $finished++ });
my $second = delay($first->begin);
my $second = Mojo::IOLoop->delay($first->begin);
Mojo::IOLoop->next_tick($second->begin);
Mojo::IOLoop->next_tick($first->begin);
my $end = $second->begin(0);
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/ioloop.t
Expand Up @@ -5,7 +5,7 @@ BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::IOLoop;
use Mojo::IOLoop::Client;
use Mojo::IOLoop::Delay 'delay';
use Mojo::IOLoop::Delay;
use Mojo::IOLoop::Server;
use Mojo::IOLoop::Stream;

Expand Down Expand Up @@ -138,7 +138,7 @@ $id = Mojo::IOLoop->server(
}
);
$port = Mojo::IOLoop->acceptor($id)->port;
my $delay = delay;
my $delay = Mojo::IOLoop->delay;
my $end = $delay->begin;
$handle = undef;
Mojo::IOLoop->client(
Expand Down Expand Up @@ -189,7 +189,7 @@ ok !$loop->acceptor($id), 'acceptor has been removed';

# Removed connection (with delay)
my $removed;
$delay = delay(sub { $removed++ });
$delay = Mojo::IOLoop->delay(sub { $removed++ });
$end = $delay->begin;
$id = Mojo::IOLoop->server(
(address => '127.0.0.1') => sub {
Expand Down
3 changes: 1 addition & 2 deletions t/mojo/ioloop_ipv6.t
Expand Up @@ -9,10 +9,9 @@ plan skip_all => 'set TEST_IPV6 to enable this test (developer only!)'
unless $ENV{TEST_IPV6};

use Mojo::IOLoop;
use Mojo::IOLoop::Delay 'delay';

# IPv6 roundtrip
my $delay = delay;
my $delay = Mojo::IOLoop->delay;
my ($server, $client);
my $end = $delay->begin;
my $id = Mojo::IOLoop->server(
Expand Down

0 comments on commit 82b0f09

Please sign in to comment.