File Coverage

blib/lib/Chrome/DevToolsProtocol/Transport/Pipe.pm
Criterion Covered Total %
statement 12 36 33.3
branch 0 6 0.0
condition 0 3 0.0
subroutine 4 6 66.6
pod 1 2 50.0
total 17 53 32.0


line stmt bran cond sub pod time code
1             package Chrome::DevToolsProtocol::Transport::Pipe;
2 1     1   513 use strict;
  1         3  
  1         30  
3 1     1   5 use Filter::signatures;
  1         2  
  1         6  
4 1     1   24 no warnings 'experimental::signatures';
  1         3  
  1         33  
5 1     1   6 use feature 'signatures';
  1         2  
  1         471  
6              
7             our $VERSION = '0.69';
8              
9             =head1 NAME
10              
11             Chrome::DevToolsProtocol::Transport::Pipe - choose the best pipe transport backend
12              
13             =cut
14              
15             our @loops;
16             push @loops, (
17             ['Mojo/IOLoop.pm' => 'Chrome::DevToolsProtocol::Transport::Pipe::Mojo' ],
18             ['IO/Async.pm' => 'Chrome::DevToolsProtocol::Transport::Pipe::NetAsync'],
19             ['IO/Async/Loop.pm' => 'Chrome::DevToolsProtocol::Transport::Pipe::NetAsync'],
20             ['AnyEvent.pm' => 'Chrome::DevToolsProtocol::Transport::Pipe::AnyEvent'],
21             ['AE.pm' => 'Chrome::DevToolsProtocol::Transport::Pipe::AnyEvent'],
22             # native POE support would be nice
23             );
24             our $implementation;
25             our $default = 'Chrome::DevToolsProtocol::Transport::Pipe::NetAsync';
26              
27             =head1 METHODS
28              
29             =head2 C<< Chrome::DevToolsProtocol::Transport::Pipe->new(@args) >>
30              
31             my $ua = Chrome::DevToolsProtocol::Transport::Pipe->new();
32              
33             Creates a new instance of the transport using the "best" event loop
34             for implementation. The default event loop is currently L<AnyEvent>.
35              
36             All parameters are passed on to the implementation class.
37              
38             =cut
39              
40 0     0 1   sub new($factoryclass, @args) {
  0            
  0            
  0            
41 0   0       $implementation ||= $factoryclass->best_implementation();
42              
43             # Just in case a user has set this from the outside
44 0           eval "require $implementation; 1";
45              
46             # return a new instance
47 0           $implementation->new(@args);
48             }
49              
50 0     0 0   sub best_implementation( $class, @candidates ) {
  0            
  0            
  0            
51              
52 0 0         if(! @candidates) {
53 0           @candidates = @loops;
54             };
55              
56             # Find the currently running/loaded event loop(s)
57             #use Data::Dumper;
58             #$Data::Dumper::Sortkeys = 1;
59             #warn Dumper \%INC;
60             #warn Dumper \@candidates;
61             my @applicable_implementations = map {
62 0           $_->[1]
63             } grep {
64 0           $INC{$_->[0]}
  0            
65             } @candidates;
66              
67 0 0         if( ! @applicable_implementations ) {
68 0           @applicable_implementations = ($default, map {$_->[1]} @candidates);
  0            
69             }
70              
71             # Check which one we can load:
72 0           for my $impl (@applicable_implementations) {
73 0 0         if( eval "require $impl; 1" ) {
74 0           return $impl;
75             }
76             # else { warn $@ };
77             };
78              
79             # This will crash and burn, but that's how it is
80 0           eval "require $default; 1";
81 0           return $default;
82             };
83              
84             1;
85              
86             =head1 SUPPORTED BACKENDS
87              
88             The module will try to guess the best backend to use. The currently supported
89             backends are
90              
91             =over 4
92              
93             =item *
94              
95             L<IO::Async>
96              
97             =item *
98              
99             L<AnyEvent> (planned)
100              
101             =item *
102              
103             L<Mojolicious> (planned)
104              
105             =back
106              
107             If you want to substitute another backend, pass its class name instead
108             of this module which only acts as a factory.
109              
110             =head1 REPOSITORY
111              
112             The public repository of this module is
113             L<https://github.com/Corion/www-mechanize-chrome>.
114              
115             =head1 SUPPORT
116              
117             The public support forum of this module is L<https://perlmonks.org/>.
118              
119             =head1 BUG TRACKER
120              
121             Please report bugs in this module via the Github bug queue at
122             L<https://github.com/Corion/WWW-Mechanize-Chrome/issues>
123              
124             =head1 AUTHOR
125              
126             Max Maischein C<corion@cpan.org>
127              
128             =head1 COPYRIGHT (c)
129              
130             Copyright 2010-2023 by Max Maischein C<corion@cpan.org>.
131              
132             =head1 LICENSE
133              
134             This module is released under the same terms as Perl itself.
135              
136             =cut