source: perl/lib/BarnOwl/MainLoopCompatHook.pm @ 9e88fa7

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 9e88fa7 was 3aa0522, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Deprecate the main loop hook, use on-demand perl timer Users without a main loop hook shouldn't need to pay 1s wakeups, and users with one probably want more control over the timeout anyway. Signed-off-by: David Benjamin <davidben@mit.edu>
  • Property mode set to 100644
File size: 1.2 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::MainLoopCompatHook;
5
6use BarnOwl;
7use BarnOwl::Hook;
8use BarnOwl::Timer;
9use Scalar::Util qw(weaken);
10
11sub new {
12    my $class = shift;
13    my $hook = BarnOwl::Hook->new;
14    return bless {hook => $hook}, $class;
15}
16
17sub check_owlconf {
18  my $self = shift;
19  $self->_ensure_timer() if (*BarnOwl::mainloop_hook{CODE});
20}
21
22sub run {
23    my $self = shift;
24    return $self->{hook}->run(@_);
25}
26
27sub add {
28    my $self = shift;
29    $self->_ensure_timer();
30    $self->{hook}->add(@_);
31}
32
33sub clear {
34    my $self = shift;
35    $self->_kill_timer() unless *BarnOwl::mainloop_hook{CODE};
36    $self->{hook}->clear();
37}
38
39sub _ensure_timer {
40  my $self = shift;
41  BarnOwl::debug("Enabling backwards-compatibility \"main loop\" hook");
42  my $weak = $self;
43  weaken($weak);
44  unless ($self->{timer}) {
45    $self->{timer} = BarnOwl::Timer->new( {
46        after => 0,
47        interval => 1,
48        cb => sub {
49          $weak->run if $weak;
50          BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
51        },
52      } );
53  }
54}
55
56sub _kill_timer {
57  my $self = shift;
58  if ($self->{timer}) {
59    $self->{timer}->stop;
60    undef $self->{timer};
61  }
62}
63
641;
Note: See TracBrowser for help on using the repository browser.