File Coverage

blib/lib/Object/Remote/Loop.pm
Criterion Covered Total %
statement 51 74 68.9
branch 4 8 50.0
condition 1 3 33.3
subroutine 17 26 65.3
pod 0 13 0.0
total 73 124 58.8


line stmt bran cond sub pod time code
1             package Object::Remote::Loop;
2              
3 13     13   77 use Scalar::Util qw( refaddr weaken );
  13         17  
  13         870  
4 13     13   4755 use Module::Runtime qw( use_module );
  13         18972  
  13         92  
5 13     13   644 use Object::Remote::MiniLoop;
  13         18  
  13         297  
6 13     13   42 use Object::Remote::Logging qw( :log :dlog router );
  13         20  
  13         82  
7 13     13   79 use Moo;
  13         22  
  13         92  
8              
9             BEGIN {
10 13     13   7330 $SIG{PIPE} = sub { log_debug { "Got a PIPE signal" } };
  0         0  
  0         0  
11              
12 13         47 router()->exclude_forwarding
13             }
14              
15             has loop => (
16             is => 'ro',
17             default => sub {
18             if ($ENV{OBJECT_REMOTE_LOOP}) {
19             return use_module($ENV{OBJECT_REMOTE_LOOP})->new;
20             }
21             Object::Remote::MiniLoop->new
22             });
23              
24             has _timers => (is => 'ro', default => sub { {} });
25              
26             for my $fn (qw( watch_io unwatch_io
27             loop_once run stop await await_all
28             )) {
29             my $code = "sub ${fn} { my \$self = shift; \$self->loop->${fn}(\@_); }";
30 173     173 0 534 eval $code;
  173     2 0 1079  
  2     0 0 5  
  2     175 0 30  
  0     232 0 0  
  0     21 0 0  
  175     40 0 383  
  175         1029  
  232         553  
  232         4951  
  21         56  
  21         244  
  40         572  
  40         395  
31             }
32              
33             sub want_run {
34 0     0 0 0 my ($self) = @_;
35 0     0   0 Dlog_debug { "Run loop: Incremeting want_running, is now $_" }
36 0         0 ++$self->{want_running};
37             }
38              
39             sub run_while_wanted {
40 0     0 0 0 my ($self) = @_;
41 0     0   0 log_debug { my $wr = $self->{want_running}; "Run loop: run_while_wanted() invoked; want_running: $wr" };
  0         0  
  0         0  
42 0         0 $self->loop_once while $self->{want_running};
43 0     0   0 log_debug { "Run loop: run_while_wanted() completed" };
  0         0  
44 0         0 return;
45             }
46              
47             sub want_stop {
48 0     0 0 0 my ($self) = @_;
49 0 0       0 if (! $self->{want_running}) {
50 0     0   0 log_debug { "Run loop: want_stop() was called but want_running was not true" };
  0         0  
51 0         0 return;
52             }
53 0     0   0 Dlog_debug { "Run loop: decrimenting want_running, is now $_" }
54 0         0 --$self->{want_running};
55             }
56              
57             sub new_future {
58 313     313 0 1593 return $_[0]->loop->new_future;
59             }
60              
61             sub watch_time {
62 23     23 0 136 my ($self, %watch) = @_;
63 23         78 my ($code, $id);
64 23         0 my $our_id;
65 23 50 33     287 if (not ($watch{every} or $watch{after} or $watch{at})) {
    100          
66 0         0 die "watch_time requires every, after or at";
67             }
68             elsif ($watch{every}) {
69             $code = sub {
70 3     3   34 $id = $self->loop->watch_time( %watch, code => $code );
71 3         18 $watch{code}->(@_);
72 1         7 };
73 1         4 $watch{after} = delete $watch{every};
74             }
75             else {
76             $code = sub {
77 3     3   33 delete $self->_timers->{"$our_id"};
78 3         25 $watch{code}->(@_);
79             }
80 22         169 }
81 23         67 $our_id = refaddr $code;
82 23         164 $self->_timers->{$our_id} = \$id;
83 23         218 $id = $self->loop->watch_time( %watch, code => $code );
84 23         61 weaken($code);
85 23         108 return $our_id;
86             }
87              
88             sub unwatch_time {
89 1     1 0 8 my ($self, $id) = @_;
90 1         9 my $timer = delete $self->_timers->{$id};
91 1 50       6 if ($timer) {
92 1         7 $self->loop->unwatch_time( ${$timer} )
  1         7  
93             }
94             }
95              
96             1;