Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
188 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package Mojo::IOLoop::Steps; | ||
use Mojo::Base -base; | ||
|
||
sub new { | ||
my $self = shift->SUPER::new(steps => [@_]); | ||
$self->next->(); | ||
return $self; | ||
} | ||
|
||
# "My god, it's full of geezers." | ||
sub next { | ||
my $self = shift; | ||
$self->{counter}++; | ||
return sub { shift; $self->_step(@_) }; | ||
} | ||
|
||
sub _step { | ||
my $self = shift; | ||
|
||
# Cache arguments | ||
my $args = $self->{args} ||= []; | ||
push @$args, @_; | ||
|
||
# Next step | ||
return unless --$self->{counter} <= 0; | ||
return unless my $cb = shift @{$self->{steps}}; | ||
$self->{args} = []; | ||
$self->$cb(@$args); | ||
} | ||
|
||
1; | ||
|
||
=head1 NAME | ||
Mojo::IOLoop::Steps - Control flow of events | ||
=head1 SYNOPSIS | ||
use Mojo::IOLoop::Steps; | ||
# Control the flow of multiple events | ||
my $steps = Mojo::IOLoop::Steps->new( | ||
sub { | ||
my $steps = shift; | ||
Mojo::IOLoop->timer(3 => $steps->next); | ||
Mojo::IOLoop->timer(1 => $steps->next); | ||
}, | ||
sub { | ||
my ($steps, @args) = @_; | ||
Mojo::IOLoop->timer(2 => $steps->next); | ||
}, | ||
sub { | ||
my ($steps, @args) = @_; | ||
say "Thank you for waiting 5 seconds."; | ||
} | ||
); | ||
# Start event loop if necessary | ||
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||
=head1 DESCRIPTION | ||
L<Mojo::IOLoop::Delay> controls the flow of events for L<Mojo::IOLoop>. | ||
=head1 METHODS | ||
L<Mojo::IOLoop::Steps> inherits all methods from L<Mojo::Base> and implements | ||
the following new ones. | ||
=head2 C<new> | ||
my $steps = Mojo::IOLoop::Steps->new(sub {...}, sub {...}); | ||
Construct a new L<Mojo::IOLoop::Steps> object. | ||
=head2 C<next> | ||
my $cb = $steps->next; | ||
Generate callback for getting to the next step. If more than one is generated, | ||
they all have to be invoked before the next step can be reached. Note that the | ||
first argument passed to the callback will be ignored. | ||
=head1 SEE ALSO | ||
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use Mojo::Base -strict; | ||
|
||
# Disable IPv6 and libev | ||
BEGIN { | ||
$ENV{MOJO_NO_IPV6} = 1; | ||
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll'; | ||
} | ||
|
||
use Test::More tests => 3; | ||
|
||
# "It's not just safe, it's 40% safe!" | ||
use Mojo::IOLoop; | ||
use Mojo::IOLoop::Steps; | ||
|
||
# Basic functionality | ||
my $result; | ||
my $steps = Mojo::IOLoop::Steps->new( | ||
sub { | ||
my $steps = shift; | ||
$steps->next->(1, 2, 3); | ||
}, | ||
sub { | ||
my ($steps, @numbers) = @_; | ||
$result = \@numbers; | ||
} | ||
); | ||
is_deeply $result, [2, 3], 'right numbers'; | ||
|
||
# Multiple steps | ||
$result = undef; | ||
$steps = Mojo::IOLoop::Steps->new( | ||
sub { | ||
my $steps = shift; | ||
my $cb = $steps->next; | ||
$steps->next->(1, 2, 3); | ||
$cb->(3, 2, 1); | ||
}, | ||
sub { shift->next->(@_) }, | ||
sub { | ||
my ($steps, @numbers) = @_; | ||
$result = \@numbers; | ||
} | ||
); | ||
is_deeply $result, [2, 3, 2, 1], 'right numbers'; | ||
|
||
# Event loop | ||
$result = undef; | ||
$steps = Mojo::IOLoop->steps( | ||
sub { | ||
my $steps = shift; | ||
my $delay = Mojo::IOLoop->delay; | ||
$delay->on(finish => $steps->next); | ||
Mojo::IOLoop->timer(0 => $delay->begin); | ||
Mojo::IOLoop->timer(0 => $steps->next); | ||
$delay->begin; | ||
Mojo::IOLoop->timer(0 => sub { $delay->end(1, 2, 3) }); | ||
}, | ||
sub { | ||
my ($steps, @numbers) = @_; | ||
$result = \@numbers; | ||
} | ||
); | ||
Mojo::IOLoop->start; | ||
is_deeply $result, [1, 2, 3], 'right numbers'; |