blib/lib/Tickit/Async.pm

filter: Statementstmt 50.0% 14 / 28 Branchbran n/a Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 55.5% 5 / 9 Podpod n/a totaltotal 45.2% 19 / 42 CC 2 SCAR 8.3
CC
2
Cov
45%
get_loop
6.0
signum2name
2.0
new
2.0
CRAP 2.3
↑ Summary lib/Tickit/Async.c »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2011-2021 -- leonerd@leonerd.org.uk
5  
6 package Tickit::Async 0.25;
7  
8 7 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use base qw( Tickit IO::Async::Notifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 Tickit->VERSION( '0.72' ); # ->_new_with_evloop with signal/process
12 IO::Async::Notifier->VERSION( '0.43' ); # Need support for being a nonprinciple mixin
13  
14 7 ▶use IO::Async::Loop 0.47; # ->run and ->stop methods
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use IO::Async::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # TODO: It'd be lovely if IO::Async::OS provided this
18 {
19 require Config;
20 my @signames = split ' ', $Config::Config{sig_name};
21 my @signums = split ' ', $Config::Config{sig_num};
22  
23 my %signum2name; @signum2name{@signums} = @signames;
24 0 ▶ sub signum2name { return $signum2name{ +shift } };
Subroutine signum2name: not called CC=1 SCAR=6.9
25 }
26  
27 =head1 NAME
28  
29 C<Tickit::Async> - use C<Tickit> with C<IO::Async>
30  
31 =head1 SYNOPSIS
32  
33 use IO::Async;
34 use Tickit::Async;
35  
36 my $tickit = Tickit::Async->new;
37  
38 # Create some widgets
39 # ...
40  
41 $tickit->set_root_widget( $rootwidget );
42  
43 my $loop = IO::Async::Loop->new;
44 $loop->add( $tickit );
45  
46 $tickit->run;
47  
48 =head1 DESCRIPTION
49  
50 This class allows a L<Tickit> user interface to run alongside other
51 L<IO::Async>-driven code, using C<IO::Async> as a source of IO events.
52  
53 As a shortcut convenience, a containing L<IO::Async::Loop> will be constructed
54 using the default magic constructor the first time it is needed, if the object
55 is not already a member of a loop. This will allow a C<Tickit::Async> object
56 to be used without being aware it is not a simple C<Tickit> object.
57  
58 To avoid accidentally creating multiple loops, callers should be careful to
59 C<add> the C<Tickit::Async> object to the main application's loop if one
60 already exists as soon as possible after construction.
61  
62 =cut
63  
64 sub new
65 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
66 0 my $class = shift;
67 0 my $self = $class->Tickit::new( @_ );
68  
69 0 return $self;
70 }
71  
72 sub get_loop
73 0 ▶{
Subroutine get_loop: not called CC=2 SCAR=17.9
74 0 my $self = shift;
75 0 ▶ return $self->SUPER::get_loop || do {
Condition0%0/3
$self->SUPER::get_loop || do { my $newloop = 'IO::Async::Loop'->new; $newloop->add($self); $newloop }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->SUPER::get_loop || do { my $newloop = 'IO::Async::Loop'->new; $newloop->add($self); $newloop }
$self->SUPER::get_loop do { my $newloop = 'IO::Async::Loop'->new; $newloop->add($self); $newloop }
Truth table0%0/3
A || B
A= $self->SUPER::get_loopB= do { my $newloop = 'IO::Async::Loop'->new; $newloop->add($self); $newloop }
ABresult
1X1
011
000
76 0 my $newloop = IO::Async::Loop->new;
77 0 $newloop->add( $self );
78 0 $newloop;
79 };
80 }
81  
82 sub _make_writer
83 0 ▶{
Subroutine _make_writer: not called CC=1 SCAR=6.9
84 0 my $self = shift;
85 0 my ( $out ) = @_;
86  
87 0 my $writer = IO::Async::Stream->new(
88 write_handle => $out,
89 autoflush => 1,
90 );
91  
92 0 $self->add_child( $writer );
93  
94 0 return $writer;
95 }
96  
97 sub _make_tickit
98 {
99 my $self = shift;
100 my ( $term ) = @_;
101  
102 my $loop = $self->get_loop;
103  
104 my $signalid;
105  
106 return Tickit::_Tickit->_new_with_evloop( $term,
107 init => sub {
108 $signalid = $loop->attach_signal( WINCH => $_[0] );
109 },
110 destroy => sub {
111 warn "TODO: destroy\n";
112 },
113  
114 run => sub { $loop->run },
115 stop => sub { $loop->stop },
116  
117 io => sub {
118 my ( $fh, $cond, $iowatch ) = @_;
119 $loop->watch_io(
120 handle => $fh,
121 ( $cond & Tickit::IO_IN ) ? ( on_read_ready => sub { $iowatch->( Tickit::IO_IN ) } ) : (),
122 ( $cond & Tickit::IO_OUT ) ? ( on_write_ready => sub { $iowatch->( Tickit::IO_OUT ) } ) : (),
123 ( $cond & Tickit::IO_HUP ) ? ( on_hangup => sub { $iowatch->( Tickit::IO_HUP ) } ) : (),
124 );
125 },
126 cancel_io => sub { $loop->unwatch_io( handle => $_[0], on_read_ready => 1 ) },
127  
128 timer => sub {
129 my ( $time, $watch ) = @_;
130 return $loop->watch_time( at => $time, code => $watch );
131 },
132 cancel_timer => sub { $loop->unwatch_time( $_[0] ) },
133  
134 later => sub {
135 my ( $watch ) = @_;
136 $loop->watch_idle( when => "later", code => $watch );
137 },
138 cancel_later => sub { warn "TODO: cancel idle" },
139  
140 signal => sub {
141 my ( $signum, $watch ) = @_;
142 my $signame = signum2name( $signum );
143 return [ $signame => $loop->attach_signal( $signame => $watch ) ];
144 },
145 cancel_signal => sub {
146 my ( $signame, $id ) = @{ +shift };
147 $loop->detach_signal( $signame => $id );
148 },
149  
150 process => sub {
151 my ( $pid, $watch ) = @_;
152 $loop->watch_process( $pid => sub {
153 my ( $pid, $wstatus ) = @_;
154 $watch->( $wstatus );
155 });
156 return $pid;
157 },
158 cancel_process => sub {
159 my ( $pid ) = @_;
160 $loop->unwatch_process( $pid );
161 },
162 );
163 }
164  
165 =head1 AUTHOR
166  
167 Paul Evans <leonerd@leonerd.org.uk>
168  
169 =cut
170  
171 0x55AA;
↑ Summary lib/Tickit/Async.c »