File Coverage

blib/lib/Chrome/DevToolsProtocol/Transport/AnyEvent.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package Chrome::DevToolsProtocol::Transport::AnyEvent;
2 1     1   462 use strict;
  1         2  
  1         32  
3 1     1   7 use Filter::signatures;
  1         2  
  1         8  
4 1     1   25 use Moo 2;
  1         16  
  1         6  
5 1     1   318 no warnings 'experimental::signatures';
  1         2  
  1         32  
6 1     1   5 use feature 'signatures';
  1         2  
  1         69  
7 1     1   7 use Scalar::Util 'weaken';
  1         2  
  1         58  
8              
9 1     1   7 use Carp qw(croak);
  1         2  
  1         49  
10              
11 1     1   319 use AnyEvent;
  0            
  0            
12             use AnyEvent::WebSocket::Client;
13             use AnyEvent::Future qw(as_future_cb);
14              
15             our $VERSION = '0.71';
16             our @CARP_NOT = ();
17              
18             =head1 NAME
19              
20             Chrome::DevToolsProtocol::Transport::AnyEvent - AnyEvent backend for Chrome communication
21              
22             =head1 SYNOPSIS
23              
24             my $got_endpoint = Future->done( "ws://..." );
25             Chrome::DevToolsProtocol::Transport::AnyEvent->connect( $handler, $got_endpoint, $logger)
26             ->then(sub {
27             my( $connection ) = @_;
28             print "We are connected\n";
29             });
30              
31             =cut
32              
33             has 'type' => (
34             is => 'ro',
35             default => 'websocket'
36             );
37              
38             has 'connection' => (
39             is => 'rw',
40             );
41              
42             has 'ws_client' => (
43             is => 'rw',
44             );
45              
46             sub connect( $self, $handler, $got_endpoint, $logger ) {
47             weaken $handler;
48             weaken(my $s = $self);
49              
50             local @CARP_NOT = (@CARP_NOT, 'Chrome::DevToolsProtocol::Transport');
51              
52             croak "Need an endpoint to connect to" unless $got_endpoint;
53             $self->close;
54              
55             $got_endpoint->then( sub( $endpoint ) {
56             die "Got an undefined endpoint" unless defined $endpoint;
57              
58             my $res = $s->future;
59             $logger->('debug',"Connecting to $endpoint");
60             $s->ws_client( AnyEvent::WebSocket::Client->new(
61             max_payload_size => 0, # allow unlimited size for messages
62             ));
63             $s->ws_client->connect( $endpoint )->cb( sub {
64             $res->done( @_ )
65             });
66             $res
67              
68             })->then( sub( $c ) {
69             $logger->( 'trace', sprintf "Connected" );
70             my $connection = $c->recv;
71              
72             $s->connection( $connection );
73             #undef $self;
74              
75             # Kick off the continous polling
76             $connection->on( each_message => sub( $connection,$message, @rest) {
77             # I haven't investigated what @rest contains...
78             $handler->on_response( $connection, $message->body )
79             });
80             $connection->on( parse_error => sub( $connection, $error) {
81             $logger->('error', $error);
82             });
83              
84             my $res = Future->done( $s );
85             undef $s;
86             $res
87             });
88             }
89              
90             sub send( $self, $message ) {
91             if( my $c = $self->connection ) {
92             $c->send( $message );
93             };
94             $self->future->done(1);
95             }
96              
97             sub close( $self ) {
98             my $c = delete $self->{connection};
99             $c->close
100             if $c;
101             delete $self->{ws_client};
102             }
103              
104             # Maybe we should keep track of the callstacks of our ->future()s
105             # and when they get lost, so we can more easily pinpoint the locations?!
106             sub future {
107             my $f = AnyEvent::Future->new;
108             #use Carp qw(cluck); cluck "Producing new future $f";
109             return $f;
110             }
111              
112             =head2 C<< $transport->sleep( $seconds ) >>
113              
114             $transport->sleep( 10 )->get; # wait for 10 seconds
115              
116             Returns a Future that will be resolved in the number of seconds given.
117              
118             =cut
119              
120             sub sleep( $self, $seconds ) {
121             AnyEvent::Future->new_delay( after => $seconds );
122             }
123              
124             1;
125              
126             =head1 REQUIRED ADDITIONAL MODULES
127              
128             This module needs additional modules that are not installed by the default
129             installation of WWW::Mechanize::Chrome:
130              
131             L<AnyEvent>
132              
133             L<AnyEvent::WebSocket::Client>
134              
135             L<AnyEvent::Future>
136              
137              
138             =head1 REPOSITORY
139              
140             The public repository of this module is
141             L<https://github.com/Corion/www-mechanize-chrome>.
142              
143             =head1 SUPPORT
144              
145             The public support forum of this module is L<https://perlmonks.org/>.
146              
147             =head1 BUG TRACKER
148              
149             Please report bugs in this module via the Github bug queue at
150             L<https://github.com/Corion/WWW-Mechanize-Chrome/issues>
151              
152             =head1 AUTHOR
153              
154             Max Maischein C<corion@cpan.org>
155              
156             =head1 COPYRIGHT (c)
157              
158             Copyright 2010-2023 by Max Maischein C<corion@cpan.org>.
159              
160             =head1 LICENSE
161              
162             This module is released under the same terms as Perl itself.
163              
164             =cut