Advertisement
Guest User

Untitled

a guest
Apr 21st, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 4.66 KB | None | 0 0
  1. From 37a06dcdbe8c5f76e35407362355475cfd2d917b Mon Sep 17 00:00:00 2001
  2. From: Simon Bertrang <janus@errornet.de>
  3. Date: Fri, 22 Apr 2011 01:20:25 +0200
  4. Subject: [PATCH] rename on_tick to recurring, merge ticks into timers and implement periodic recurrence
  5.  
  6. ---
  7. lib/Mojo/IOLoop.pm  |   31 ++++++++++++++++++-------------
  8.  t/mojo/ioloop.t     |   17 +++++++++++++----
  9.  t/mojo/ioloop_tls.t |    2 +-
  10.  3 files changed, 32 insertions(+), 18 deletions(-)
  11.  
  12. diff --git lib/Mojo/IOLoop.pm lib/Mojo/IOLoop.pm
  13. index ea3c950..b771a97 100644
  14. --- lib/Mojo/IOLoop.pm
  15. +++ lib/Mojo/IOLoop.pm
  16. @@ -497,7 +497,16 @@ sub on_error { shift->_add_event('error', @_) }
  17.  sub on_hup   { shift->_add_event('hup',   @_) }
  18.  sub on_idle { shift->_add_loop_event('idle', @_) }
  19.  sub on_read { shift->_add_event('read', @_) }
  20. -sub on_tick { shift->_add_loop_event('tick', @_) }
  21. +
  22. +sub recurring {
  23. +  my $self = shift;
  24. +
  25. +  # Singleton
  26. +  $self = $self->singleton unless ref $self;
  27. +
  28. +  # Ticking timer
  29. +  $self->_add_loop_event(timer => pop, after => pop, ticked => time);
  30. +}
  31.  
  32.  sub one_tick {
  33.    my ($self, $timeout) = @_;
  34. @@ -592,11 +601,6 @@ sub one_tick {
  35.    # Timers
  36.    my $timers = $self->_timer;
  37.  
  38. -  # Tick
  39. -  for my $tick (keys %{$self->{_tick}}) {
  40. -    $self->_run_callback('tick', $self->{_tick}->{$tick}->{cb}, $tick);
  41. -  }
  42. -
  43.    # Idle
  44.    unless (@read || @write || @error || @hup || $timers) {
  45.      for my $idle (keys %{$self->{_idle}}) {
  46. @@ -1048,7 +1052,7 @@ sub _drop_immediately {
  47.    my ($self, $id) = @_;
  48.  
  49.    # Drop loop events
  50. -  for my $event (qw/idle tick timer/) {
  51. +  for my $event (qw/idle timer/) {
  52.      if ($self->{"_$event"}->{$id}) {
  53.  
  54.        # Drop
  55. @@ -1481,15 +1485,16 @@ sub _timer {
  56.  
  57.      # Timer
  58.      my $after = $t->{after} || 0;
  59. -    if ($after <= time - $t->{started}) {
  60. +    if ($after <= time - ($t->{started} || $t->{ticked})) {
  61. +      $t->{ticked} += $after if $after && $t->{ticked};
  62.  
  63.        # Drop
  64. -      $self->_drop_immediately($id);
  65. +      $self->_drop_immediately($id) if $t->{started};
  66.  
  67.        # Callback
  68.        if (my $cb = $t->{cb}) {
  69.          $self->_run_callback('timer', $cb);
  70. -        $count++;
  71. +        $count++ if $t->{started};
  72.        }
  73.      }
  74.    }
  75. @@ -2060,15 +2065,15 @@ Callback to be invoked if new data arrives on the connection.
  76.      # Process chunk
  77.    });
  78.  
  79. -=head2 C<on_tick>
  80. +=head2 C<recurring>
  81.  
  82. -  my $id = $loop->on_tick(sub {...});
  83. +  my $id = $loop->recurring(0 => sub {...});
  84.  
  85.  Callback to be invoked on every reactor tick, this for example allows you to
  86.  run multiple reactors next to each other.
  87.  
  88.    my $loop2 = Mojo::IOLoop->new(timeout => 0);
  89. -  Mojo::IOLoop->singleton->on_tick(sub { $loop2->one_tick });
  90. +  Mojo::IOLoop->singleton->recurring(0 => sub { $loop2->one_tick });
  91.  
  92.  Note that the loop timeout can be changed dynamically at any time to adjust
  93.  responsiveness.
  94. diff --git t/mojo/ioloop.t t/mojo/ioloop.t
  95. index c24d28c..9c1fd51 100644
  96. --- t/mojo/ioloop.t
  97. +++ t/mojo/ioloop.t
  98. @@ -6,7 +6,7 @@ use warnings;
  99.  # Disable IPv6, epoll and kqueue
  100.  BEGIN { $ENV{MOJO_NO_IPV6} = $ENV{MOJO_POLL} = 1 }
  101.  
  102. -use Test::More tests => 9;
  103. +use Test::More tests => 10;
  104.  
  105.  use_ok 'Mojo::IOLoop';
  106.  
  107. @@ -30,7 +30,7 @@ $loop->connect(
  108.  
  109.  # Ticks
  110.  my $ticks = 0;
  111. -my $id = $loop->on_tick(sub { $ticks++ });
  112. +my $id = $loop->recurring(0 => sub { $ticks++ });
  113.  
  114.  # Timer
  115.  my $flag = 0;
  116. @@ -69,7 +69,7 @@ $loop->on_idle(sub { $idle++ });
  117.  $loop->one_tick;
  118.  
  119.  # Ticks
  120. -ok $ticks > 2, 'more than two ticks';
  121. +ok $ticks > 2, 'more than two ticks: '.$ticks;
  122.  
  123.  # Idle callback
  124.  is $idle, 1, 'on_idle was called';
  125. @@ -77,13 +77,22 @@ is $idle, 1, 'on_idle was called';
  126.  # Run again without first tick event handler
  127.  my $before = $ticks;
  128.  my $after  = 0;
  129. -$loop->on_tick(sub { $after++ });
  130. +$loop->recurring(0 => sub { $after++ });
  131.  $loop->drop($id);
  132.  $loop->timer(1 => sub { shift->stop });
  133.  $loop->start;
  134.  ok $after > 2, 'more than two ticks';
  135.  is $ticks, $before, 'no additional ticks';
  136.  
  137. +
  138. +# Recurrence
  139. +my $count = 0;
  140. +$loop->recurring(0.5 => sub { $count++ });
  141. +$loop->timer(3 => sub { shift->stop });
  142. +$loop->start;
  143. +
  144. +is $count, 6, 'three recurring events';
  145. +
  146.  # Handle
  147.  my $port = Mojo::IOLoop->generate_port;
  148.  my $handle;
  149. diff --git t/mojo/ioloop_tls.t t/mojo/ioloop_tls.t
  150. index 9d1455d..84a5f20 100644
  151. --- t/mojo/ioloop_tls.t
  152. +++ t/mojo/ioloop_tls.t
  153. @@ -69,7 +69,7 @@ $loop   = Mojo::IOLoop->singleton;
  154.  $port   = Mojo::IOLoop->generate_port;
  155.  $server = $client = '';
  156.  my ($drop, $running);
  157. -Mojo::IOLoop->drop($loop->on_tick(sub { $drop++ }));
  158. +Mojo::IOLoop->drop($loop->recurring(0 => sub { $drop++ }));
  159.  my $error = '';
  160.  $loop->listen(
  161.    port      => $port,
  162. --
  163. 1.7.3.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement