Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
next_tick belongs to Mojo::Reactor::Poll
  • Loading branch information
kraih committed Apr 27, 2015
1 parent 13bc2ca commit 8d28581
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -8,6 +8,7 @@
- Removed setuidgid method from Mojo::Server.
- Removed group and user settings from Hypnotoad.
- Removed -g/--group and -u/--user options from daemon and prefork commands.
- Added next_tick method to Mojo::Reactor::Poll.
- Improved next_tick callbacks to run in the same order in which they were
registered.

Expand Down
32 changes: 11 additions & 21 deletions lib/Mojo/Reactor.pm
Expand Up @@ -13,27 +13,15 @@ sub detect {

sub io { croak 'Method "io" not implemented by subclass' }
sub is_running { croak 'Method "is_running" not implemented by subclass' }

sub next_tick {
my ($self, $cb) = @_;
$self->timer(0 => \&_next) if push(@{$self->{next_tick}}, $cb) == 1;
return undef;
}

sub one_tick { croak 'Method "one_tick" not implemented by subclass' }
sub recurring { croak 'Method "recurring" not implemented by subclass' }
sub remove { croak 'Method "remove" not implemented by subclass' }
sub reset { croak 'Method "reset" not implemented by subclass' }
sub start { croak 'Method "start" not implemented by subclass' }
sub stop { croak 'Method "stop" not implemented by subclass' }
sub timer { croak 'Method "timer" not implemented by subclass' }
sub watch { croak 'Method "watch" not implemented by subclass' }

sub _next {
my $self = shift;
my $next = delete $self->{next_tick};
for my $cb (@$next) { $self->$cb }
}
sub next_tick { croak 'Method "next_tick" not implemented by subclass' }
sub one_tick { croak 'Method "one_tick" not implemented by subclass' }
sub recurring { croak 'Method "recurring" not implemented by subclass' }
sub remove { croak 'Method "remove" not implemented by subclass' }
sub reset { croak 'Method "reset" not implemented by subclass' }
sub start { croak 'Method "start" not implemented by subclass' }
sub stop { croak 'Method "stop" not implemented by subclass' }
sub timer { croak 'Method "timer" not implemented by subclass' }
sub watch { croak 'Method "watch" not implemented by subclass' }

1;

Expand All @@ -51,6 +39,7 @@ Mojo::Reactor - Low-level event reactor base class
sub again {...}
sub io {...}
sub is_running {...}
sub next_tick {...}
sub one_tick {...}
sub recurring {...}
sub remove {...}
Expand Down Expand Up @@ -134,6 +123,7 @@ Check if reactor is running. Meant to be overloaded in a subclass.
Invoke callback as soon as possible, but not before returning or other
callbacks that have been registered with this method, always returns C<undef>.
Meant to be overloaded in a subclass.
=head2 one_tick
Expand Down
22 changes: 21 additions & 1 deletion lib/Mojo/Reactor/Poll.pm
Expand Up @@ -20,6 +20,13 @@ sub io {

sub is_running { !!shift->{running} }

sub next_tick {
my ($self, $cb) = @_;
push @{$self->{next_tick}}, $cb;
$self->{next} //= $self->timer(0 => \&_tick);
return undef;
}

sub one_tick {
my $self = shift;

Expand Down Expand Up @@ -84,7 +91,7 @@ sub remove {
return !!delete $self->{io}{fileno $remove};
}

sub reset { delete @{shift()}{qw(io next_tick timers)} }
sub reset { delete @{shift()}{qw(io next next_tick timers)} }

sub start {
my $self = shift;
Expand Down Expand Up @@ -114,6 +121,12 @@ sub _id {
return $id;
}

sub _tick {
my $self = shift;
delete $self->{next};
while (my $cb = shift @{$self->{next_tick}}) { $self->$cb }
}

sub _timer {
my ($self, $recurring, $after, $cb) = @_;

Expand Down Expand Up @@ -208,6 +221,13 @@ readable or writable.
Check if reactor is running.
=head2 next_tick
my $undef = $reactor->next_tick(sub {...});
Invoke callback as soon as possible, but not before returning or other
callbacks that have been registered with this method, always returns C<undef>.
=head2 one_tick
$reactor->one_tick;
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/reactor_ev.t
Expand Up @@ -150,11 +150,11 @@ is ref $reactor2, 'Mojo::Reactor::Poll', 'right object';

# Ordered next_tick
my $result = [];
for my $i (1 .. 50) {
for my $i (1 .. 10) {
$reactor->next_tick(sub { push @$result, $i });
}
$reactor->start;
is_deeply $result, [1 .. 50], 'right result';
is_deeply $result, [1 .. 10], 'right result';

# Reset while watchers are active
$writable = undef;
Expand Down
6 changes: 4 additions & 2 deletions t/mojo/reactor_poll.t
Expand Up @@ -147,11 +147,11 @@ is ref $reactor2, 'Mojo::Reactor::Poll', 'right object';

# Ordered next_tick
my $result = [];
for my $i (1 .. 50) {
for my $i (1 .. 10) {
$reactor->next_tick(sub { push @$result, $i });
}
$reactor->start;
is_deeply $result, [1 .. 50], 'right result';
is_deeply $result, [1 .. 10], 'right result';

# Reset while watchers are active
$writable = undef;
Expand Down Expand Up @@ -296,6 +296,8 @@ eval { Mojo::Reactor->io };
like $@, qr/Method "io" not implemented by subclass/, 'right error';
eval { Mojo::Reactor->is_running };
like $@, qr/Method "is_running" not implemented by subclass/, 'right error';
eval { Mojo::Reactor->next_tick };
like $@, qr/Method "next_tick" not implemented by subclass/, 'right error';
eval { Mojo::Reactor->one_tick };
like $@, qr/Method "one_tick" not implemented by subclass/, 'right error';
eval { Mojo::Reactor->recurring };
Expand Down

0 comments on commit 8d28581

Please sign in to comment.