From 8d285811c15bea78dee53a8fe5e46cd485843cf6 Mon Sep 17 00:00:00 2001 From: Sebastian Riedel Date: Mon, 27 Apr 2015 02:52:07 +0200 Subject: [PATCH] next_tick belongs to Mojo::Reactor::Poll --- Changes | 1 + lib/Mojo/Reactor.pm | 32 +++++++++++--------------------- lib/Mojo/Reactor/Poll.pm | 22 +++++++++++++++++++++- t/mojo/reactor_ev.t | 4 ++-- t/mojo/reactor_poll.t | 6 ++++-- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/Changes b/Changes index 9d21a27ebc..896c14aeae 100644 --- a/Changes +++ b/Changes @@ -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. diff --git a/lib/Mojo/Reactor.pm b/lib/Mojo/Reactor.pm index 1817137166..6b0c34b404 100644 --- a/lib/Mojo/Reactor.pm +++ b/lib/Mojo/Reactor.pm @@ -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; @@ -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 {...} @@ -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. +Meant to be overloaded in a subclass. =head2 one_tick diff --git a/lib/Mojo/Reactor/Poll.pm b/lib/Mojo/Reactor/Poll.pm index c4c8ac012b..121fa5a016 100644 --- a/lib/Mojo/Reactor/Poll.pm +++ b/lib/Mojo/Reactor/Poll.pm @@ -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; @@ -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; @@ -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) = @_; @@ -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. + =head2 one_tick $reactor->one_tick; diff --git a/t/mojo/reactor_ev.t b/t/mojo/reactor_ev.t index bd372e1e56..651e109be4 100644 --- a/t/mojo/reactor_ev.t +++ b/t/mojo/reactor_ev.t @@ -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; diff --git a/t/mojo/reactor_poll.t b/t/mojo/reactor_poll.t index d1c6784d83..317f9b6f9a 100644 --- a/t/mojo/reactor_poll.t +++ b/t/mojo/reactor_poll.t @@ -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; @@ -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 };