source: perl/lib/BarnOwl/MainLoopCompatHook.pm @ c8d9f84

release-1.7release-1.8release-1.9
Last change on this file since c8d9f84 was c8d9f84, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
More aggressively stop Timers created in perl In case something leaks with references or whatever, at least we don't leak timers.
  • 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 DESTROY {
40    my $self = shift;
41    $self->clear();
42}
43
44sub _ensure_timer {
45  my $self = shift;
46  BarnOwl::debug("Enabling backwards-compatibility \"main loop\" hook");
47  my $weak = $self;
48  weaken($weak);
49  unless ($self->{timer}) {
50    $self->{timer} = BarnOwl::Timer->new( {
51        after => 0,
52        interval => 1,
53        cb => sub {
54          $weak->run if $weak;
55          BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
56        },
57      } );
58  }
59}
60
61sub _kill_timer {
62  my $self = shift;
63  if ($self->{timer}) {
64    $self->{timer}->stop;
65    undef $self->{timer};
66  }
67}
68
691;
Note: See TracBrowser for help on using the repository browser.