File Coverage

blib/lib/ONE.pm
Criterion Covered Total %
statement 63 72 87.5
branch 11 22 50.0
condition 4 8 50.0
subroutine 17 18 94.4
pod 4 6 66.6
total 99 126 78.5


line stmt bran cond sub pod time code
1             # ABSTRACT: A Node.js style AnyEvent class, using MooseX::Event
2             package ONE;
3             {
4             $ONE::VERSION = 'v0.2.0';
5             }
6 2     2   54967 use AnyEvent;
  2         11992  
  2         67  
7 2     2   1125 use ONE::Collect;
  2         7  
  2         56  
8 2     2   1854 use MooseX::Event;
  2         2358  
  2         11  
9              
10             with 'MooseX::Event::Role::ClassMethods';
11              
12              
13             sub collect (&) {
14 1     1 1 939 my $collect = ONE::Collect->new();
15             my $wrapper = MooseX::Event->add_listener_wrapper( sub {
16 2     2   59 my( $todo ) = @_;
17 2         9 $collect->listener( $todo );
18 1         77 } );
19 1         11 $_[0]->();
20 1         4 MooseX::Event->remove_listener_wrapper( $wrapper );
21 1         12 $collect->complete;
22             }
23              
24             has '_loop_cv' => (is=>'rw', init_arg=>undef);
25             has '_idle_cv' => (is=>'rw', init_arg=>undef );
26             has '_signal' => (is=>'rw', default=>sub{{}}, init_arg=>undef);
27              
28              
29              
30             has_events qw(
31             idle
32             SIGHUP SIGINT SIGQUIT SIGILL SIGTRAP SIGABRT SIGBUS SIGFPE SIGKILL
33             SIGUSR1 SIGSEGV SIGUSR2 SIGPIPE SIGALRM SIGTERM SIGSTKFLT SIGCHLD SIGCONT
34             SIGSTOP SIGTSTP SIGTTIN SIGTTOU SIGURG SIGXCPU SIGXFSZ SIGVTALRM SIGPROF
35             SIGWINCH SIGIO SIGPWR SIGSYS );
36              
37              
38              
39              
40             # We would just use MooseX::Singleton, but it's nice to maintain compatibility with Mouse
41             BEGIN {
42 2     2   22093 my $instance;
43             sub instance {
44 5     5 1 2005 my $class = shift;
45 5   66     42 return $instance ||= $class->new(@_);
46             }
47             }
48              
49              
50              
51             sub activate_event {
52 2     2 0 98 my $self = shift;
53 2         3 my( $event ) = @_;
54 2 100       14 if ( $event eq 'idle' ) {
    50          
55 1     2035   34 $self->_idle_cv( AE::idle( sub { $self->emit('idle'); } ) );
  2035         91845  
56             }
57             elsif ( $event =~ /^SIG([\w\d]+)$/ ) {
58 1         4 my $sig = $1;
59 1     1   26 $self->_signal->{$sig} = AE::signal $sig, sub { $self->emit("SIG$sig") };
  1         1000099  
60             }
61             }
62              
63              
64             sub deactivate_event {
65 1     1 0 57 my $self = shift;
66 1         3 my( $event ) = @_;
67 1 50       4 if ( $event eq 'idle' ) {
    0          
68 1         7 $self->_idle_cv( undef );
69             }
70             elsif ( $event =~ /^SIG([\w\d]+)$/ ) {
71 0         0 delete $self->_signal->{$1};
72             }
73             }
74              
75              
76             sub loop {
77 1     1 1 8 my $cors = shift;
78 1 50       4 my $self = ref $cors ? $cors : $cors->instance;
79 1 50       6 if ( defined $self->_loop_cv ) {
80 0         0 $self->_loop_cv->send();
81             }
82 1         30 my $cv = AE::cv;
83 1         147 $self->_loop_cv( $cv );
84 1         8 $cv->recv();
85             }
86              
87              
88             sub stop {
89 1     1 1 40 my $cors = shift;
90 1 50       10 my $self = ref $cors ? $cors : $cors->instance;
91 1 50       9 return unless defined $self->_loop_cv;
92 1         28 $self->_loop_cv->send();
93 1         18 delete $self->{'_loop_cv'};
94             }
95              
96             sub import {
97 2     2   14 my $class = shift;
98 2         16 my $caller = caller;
99            
100 2         9 for (@_) {
101 2         10 my($module,$args) = split /=/;
102 2   50     13 my @args = split /[:]/, $args || "";
103              
104 2         3 local $@;
105 2         125 eval "require ONE::$module;";
106 2 50       15 if ( $@ ) {
107 0         0 require Carp;
108 0         0 Carp::croak( $@ );
109             }
110 2 50 33     181 eval "package $caller; ONE::$module->import(\@args);" if @args or !/=/;
111 2 50       18 if ( $@ ) {
112 0         0 require Carp;
113 0         0 Carp::croak( $@ );
114             }
115             }
116            
117 2     2   27 no strict 'refs';
  2         10  
  2         222  
118 2         55 *{$caller.'::collect'} = $class->can('collect');
  2         11390  
119             }
120              
121              
122             sub unimport {
123 0     0     my $caller = caller;
124 2     2   10 no strict 'refs';
  2         3  
  2         136  
125 0           delete ${$caller.'::'}{'collect'};
  0            
126             }
127              
128             __PACKAGE__->meta->make_immutable();
129 2     2   10 no MooseX::Event;
  2         4  
  2         13  
130              
131             1;
132              
133              
134             __END__