Chrome/DevToolsProtocol/Transport/AnyEvent.pm

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