File Coverage

blib/lib/Mojo/Reactor/EV.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Mojo::Reactor::EV;
2 6     6   39 use Mojo::Base 'Mojo::Reactor::Poll';
  6         16  
  6         41  
3              
4 6     6   52 use Carp 'croak';
  6         21  
  6         356  
5 6     6   1204 use EV 4.0;
  0            
  0            
6              
7             my $EV;
8              
9             sub DESTROY { undef $EV }
10              
11             sub again {
12             croak 'Timer not active' unless my $timer = shift->{timers}{shift()};
13             $timer->{watcher}->again;
14             }
15              
16             sub is_running { !!EV::depth }
17              
18             # We have to fall back to Mojo::Reactor::Poll, since EV is unique
19             sub new { $EV++ ? Mojo::Reactor::Poll->new : shift->SUPER::new }
20              
21             sub one_tick { EV::run(EV::RUN_ONCE) }
22              
23             sub recurring { shift->_timer(1, @_) }
24              
25             sub start {EV::run}
26              
27             sub stop { EV::break(EV::BREAK_ALL) }
28              
29             sub timer { shift->_timer(0, @_) }
30              
31             sub watch {
32             my ($self, $handle, $read, $write) = @_;
33              
34             my $fd = fileno $handle;
35             croak 'I/O watcher not active' unless my $io = $self->{io}{$fd};
36              
37             my $mode = 0;
38             $mode |= EV::READ if $read;
39             $mode |= EV::WRITE if $write;
40              
41             if ($mode == 0) { delete $io->{watcher} }
42             elsif (my $w = $io->{watcher}) { $w->events($mode) }
43             else {
44             my $cb = sub {
45             my ($w, $revents) = @_;
46             $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 0)
47             if EV::READ & $revents;
48             $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 1)
49             if EV::WRITE & $revents && $self->{io}{$fd};
50             };
51             $io->{watcher} = EV::io($fd, $mode, $cb);
52             }
53              
54             return $self;
55             }
56              
57             sub _timer {
58             my ($self, $recurring, $after, $cb) = @_;
59             $after ||= 0.0001 if $recurring;
60              
61             my $id = $self->_id;
62             my $wrapper = sub {
63             delete $self->{timers}{$id} unless $recurring;
64             $self->_try('Timer', $cb);
65             };
66             EV::now_update() if $after > 0;
67             $self->{timers}{$id}{watcher} = EV::timer($after, $after, $wrapper);
68              
69             return $id;
70             }
71              
72             1;
73              
74             =encoding utf8
75              
76             =head1 NAME
77              
78             Mojo::Reactor::EV - Low-level event reactor with libev support
79              
80             =head1 SYNOPSIS
81              
82             use Mojo::Reactor::EV;
83              
84             # Watch if handle becomes readable or writable
85             my $reactor = Mojo::Reactor::EV->new;
86             $reactor->io($first => sub {
87             my ($reactor, $writable) = @_;
88             say $writable ? 'First handle is writable' : 'First handle is readable';
89             });
90              
91             # Change to watching only if handle becomes writable
92             $reactor->watch($first, 0, 1);
93              
94             # Turn file descriptor into handle and watch if it becomes readable
95             my $second = IO::Handle->new_from_fd($fd, 'r');
96             $reactor->io($second => sub {
97             my ($reactor, $writable) = @_;
98             say $writable ? 'Second handle is writable' : 'Second handle is readable';
99             })->watch($second, 1, 0);
100              
101             # Add a timer
102             $reactor->timer(15 => sub {
103             my $reactor = shift;
104             $reactor->remove($first);
105             $reactor->remove($second);
106             say 'Timeout!';
107             });
108              
109             # Start reactor if necessary
110             $reactor->start unless $reactor->is_running;
111              
112             =head1 DESCRIPTION
113              
114             L is a low-level event reactor based on L (4.0+).
115              
116             =head1 EVENTS
117              
118             L inherits all events from L.
119              
120             =head1 METHODS
121              
122             L inherits all methods from L and
123             implements the following new ones.
124              
125             =head2 again
126              
127             $reactor->again($id);
128              
129             Restart timer. Note that this method requires an active timer.
130              
131             =head2 is_running
132              
133             my $bool = $reactor->is_running;
134              
135             Check if reactor is running.
136              
137             =head2 new
138              
139             my $reactor = Mojo::Reactor::EV->new;
140              
141             Construct a new L object.
142              
143             =head2 one_tick
144              
145             $reactor->one_tick;
146              
147             Run reactor until an event occurs or no events are being watched anymore.
148              
149             # Don't block longer than 0.5 seconds
150             my $id = $reactor->timer(0.5 => sub {});
151             $reactor->one_tick;
152             $reactor->remove($id);
153              
154             =head2 recurring
155              
156             my $id = $reactor->recurring(0.25 => sub {...});
157              
158             Create a new recurring timer, invoking the callback repeatedly after a given
159             amount of time in seconds.
160              
161             =head2 start
162              
163             $reactor->start;
164              
165             Start watching for I/O and timer events, this will block until L is
166             called or no events are being watched anymore.
167              
168             # Start reactor only if it is not running already
169             $reactor->start unless $reactor->is_running;
170              
171             =head2 stop
172              
173             $reactor->stop;
174              
175             Stop watching for I/O and timer events.
176              
177             =head2 timer
178              
179             my $id = $reactor->timer(0.5 => sub {...});
180              
181             Create a new timer, invoking the callback after a given amount of time in
182             seconds.
183              
184             =head2 watch
185              
186             $reactor = $reactor->watch($handle, $readable, $writable);
187              
188             Change I/O events to watch handle for with true and false values. Note that
189             this method requires an active I/O watcher.
190              
191             # Watch only for readable events
192             $reactor->watch($handle, 1, 0);
193              
194             # Watch only for writable events
195             $reactor->watch($handle, 0, 1);
196              
197             # Watch for readable and writable events
198             $reactor->watch($handle, 1, 1);
199              
200             # Pause watching for events
201             $reactor->watch($handle, 0, 0);
202              
203             =head1 SEE ALSO
204              
205             L, L, L.
206              
207             =cut