Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 37a06dcdbe8c5f76e35407362355475cfd2d917b Mon Sep 17 00:00:00 2001
- From: Simon Bertrang <janus@errornet.de>
- Date: Fri, 22 Apr 2011 01:20:25 +0200
- Subject: [PATCH] rename on_tick to recurring, merge ticks into timers and implement periodic recurrence
- ---
- lib/Mojo/IOLoop.pm | 31 ++++++++++++++++++-------------
- t/mojo/ioloop.t | 17 +++++++++++++----
- t/mojo/ioloop_tls.t | 2 +-
- 3 files changed, 32 insertions(+), 18 deletions(-)
- diff --git lib/Mojo/IOLoop.pm lib/Mojo/IOLoop.pm
- index ea3c950..b771a97 100644
- --- lib/Mojo/IOLoop.pm
- +++ lib/Mojo/IOLoop.pm
- @@ -497,7 +497,16 @@ sub on_error { shift->_add_event('error', @_) }
- sub on_hup { shift->_add_event('hup', @_) }
- sub on_idle { shift->_add_loop_event('idle', @_) }
- sub on_read { shift->_add_event('read', @_) }
- -sub on_tick { shift->_add_loop_event('tick', @_) }
- +
- +sub recurring {
- + my $self = shift;
- +
- + # Singleton
- + $self = $self->singleton unless ref $self;
- +
- + # Ticking timer
- + $self->_add_loop_event(timer => pop, after => pop, ticked => time);
- +}
- sub one_tick {
- my ($self, $timeout) = @_;
- @@ -592,11 +601,6 @@ sub one_tick {
- # Timers
- my $timers = $self->_timer;
- - # Tick
- - for my $tick (keys %{$self->{_tick}}) {
- - $self->_run_callback('tick', $self->{_tick}->{$tick}->{cb}, $tick);
- - }
- -
- # Idle
- unless (@read || @write || @error || @hup || $timers) {
- for my $idle (keys %{$self->{_idle}}) {
- @@ -1048,7 +1052,7 @@ sub _drop_immediately {
- my ($self, $id) = @_;
- # Drop loop events
- - for my $event (qw/idle tick timer/) {
- + for my $event (qw/idle timer/) {
- if ($self->{"_$event"}->{$id}) {
- # Drop
- @@ -1481,15 +1485,16 @@ sub _timer {
- # Timer
- my $after = $t->{after} || 0;
- - if ($after <= time - $t->{started}) {
- + if ($after <= time - ($t->{started} || $t->{ticked})) {
- + $t->{ticked} += $after if $after && $t->{ticked};
- # Drop
- - $self->_drop_immediately($id);
- + $self->_drop_immediately($id) if $t->{started};
- # Callback
- if (my $cb = $t->{cb}) {
- $self->_run_callback('timer', $cb);
- - $count++;
- + $count++ if $t->{started};
- }
- }
- }
- @@ -2060,15 +2065,15 @@ Callback to be invoked if new data arrives on the connection.
- # Process chunk
- });
- -=head2 C<on_tick>
- +=head2 C<recurring>
- - my $id = $loop->on_tick(sub {...});
- + my $id = $loop->recurring(0 => sub {...});
- Callback to be invoked on every reactor tick, this for example allows you to
- run multiple reactors next to each other.
- my $loop2 = Mojo::IOLoop->new(timeout => 0);
- - Mojo::IOLoop->singleton->on_tick(sub { $loop2->one_tick });
- + Mojo::IOLoop->singleton->recurring(0 => sub { $loop2->one_tick });
- Note that the loop timeout can be changed dynamically at any time to adjust
- responsiveness.
- diff --git t/mojo/ioloop.t t/mojo/ioloop.t
- index c24d28c..9c1fd51 100644
- --- t/mojo/ioloop.t
- +++ t/mojo/ioloop.t
- @@ -6,7 +6,7 @@ use warnings;
- # Disable IPv6, epoll and kqueue
- BEGIN { $ENV{MOJO_NO_IPV6} = $ENV{MOJO_POLL} = 1 }
- -use Test::More tests => 9;
- +use Test::More tests => 10;
- use_ok 'Mojo::IOLoop';
- @@ -30,7 +30,7 @@ $loop->connect(
- # Ticks
- my $ticks = 0;
- -my $id = $loop->on_tick(sub { $ticks++ });
- +my $id = $loop->recurring(0 => sub { $ticks++ });
- # Timer
- my $flag = 0;
- @@ -69,7 +69,7 @@ $loop->on_idle(sub { $idle++ });
- $loop->one_tick;
- # Ticks
- -ok $ticks > 2, 'more than two ticks';
- +ok $ticks > 2, 'more than two ticks: '.$ticks;
- # Idle callback
- is $idle, 1, 'on_idle was called';
- @@ -77,13 +77,22 @@ is $idle, 1, 'on_idle was called';
- # Run again without first tick event handler
- my $before = $ticks;
- my $after = 0;
- -$loop->on_tick(sub { $after++ });
- +$loop->recurring(0 => sub { $after++ });
- $loop->drop($id);
- $loop->timer(1 => sub { shift->stop });
- $loop->start;
- ok $after > 2, 'more than two ticks';
- is $ticks, $before, 'no additional ticks';
- +
- +# Recurrence
- +my $count = 0;
- +$loop->recurring(0.5 => sub { $count++ });
- +$loop->timer(3 => sub { shift->stop });
- +$loop->start;
- +
- +is $count, 6, 'three recurring events';
- +
- # Handle
- my $port = Mojo::IOLoop->generate_port;
- my $handle;
- diff --git t/mojo/ioloop_tls.t t/mojo/ioloop_tls.t
- index 9d1455d..84a5f20 100644
- --- t/mojo/ioloop_tls.t
- +++ t/mojo/ioloop_tls.t
- @@ -69,7 +69,7 @@ $loop = Mojo::IOLoop->singleton;
- $port = Mojo::IOLoop->generate_port;
- $server = $client = '';
- my ($drop, $running);
- -Mojo::IOLoop->drop($loop->on_tick(sub { $drop++ }));
- +Mojo::IOLoop->drop($loop->recurring(0 => sub { $drop++ }));
- my $error = '';
- $loop->listen(
- port => $port,
- --
- 1.7.3.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement