blib/lib/Discord/Client.pm

filter: Statementstmt 95.8% 185 / 193 Branchbran 70.0% 56 / 80 Conditioncond 55.4% 41 / 74 MC/DCmcdc 27.5% 11 / 40 Subroutinesub 90.2% 37 / 41 Podpod 100% 18 / 18 totaltotal 78.0% 348 / 446 CC 73 SCAR 20.6
CC
73
Cov
81%
new
19.1
_emit
15.4
__ANON__
11.3
CRAP 7.8
« blib/lib/Discord/Client/HTTPInteractions.pm ↑ Summary blib/lib/Discord/Client/Gateway/Shards.pm »
1 package Discord::Client;
2  
3 22 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 22 our $VERSION = '0.003';
12  
13 30 ▶sub new {
Subroutine new: called CC=14 SCAR=29.5
Pod documented
14 30 my ( $class, %args ) = @_;
15  
16 # Validate callback limits before constructing owned resources.
17 30 ▶ croak 'on_error must be a code reference' if exists( $args{on_error} ) && ref( $args{on_error} ) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_error"} and ref $args{"on_error"} ne "CODE" 0 30 30
Condition66%2/3
exists $args{"on_error"} and ref $args{"on_error"} ne "CODE"
!ll&&!rl&&r
11190
MC/DC0%0/2
exists $args{"on_error"} and ref $args{"on_error"} ne "CODE"
exists $args{"on_error"} ref $args{"on_error"} ne "CODE"
Truth table66%2/3
A and B
A= exists $args{"on_error"}B= ref $args{"on_error"} ne "CODE"
ABresult
0X0
100
111
18 croak 'max_callbacks must be a positive integer'
19 if exists( $args{max_callbacks} )
20 && ( !defined( $args{max_callbacks} )
21 || ref( $args{max_callbacks} )
22 30 ▶ || $args{max_callbacks} !~ /\A[1-9]\d*\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"max_callbacks"} and !defined($args{"max_callbacks"}) || ref $args{"max_callbacks"} || !($args{"max_callbacks"} =~ /\A[1-9]\d*\z/u) 0 30 30
Condition44%4/9
exists $args{"max_callbacks"} and !defined($args{"max_callbacks"}) || ref $args{"max_callbacks"} || !($args{"max_callbacks"} =~ /\A[1-9]\d*\z/u)
!ll&&!rl&&r
2910
not defined $args{"max_callbacks"} or ref $args{"max_callbacks"} or not $args{"max_callbacks"} =~ /\A[1-9]\d*\z/u
l!l&&r!l&&!r
001
not defined $args{"max_callbacks"} or ref $args{"max_callbacks"}
l!l&&r!l&&!r
001
MC/DC0%0/4
exists $args{"max_callbacks"} and !defined($args{"max_callbacks"}) || ref $args{"max_callbacks"} || !($args{"max_callbacks"} =~ /\A[1-9]\d*\z/u)
exists $args{"max_callbacks"} not defined $args{"max_callbacks"} ref $args{"max_callbacks"} not $args{"max_callbacks"} =~ /\A[1-9]\d*\z/u
Truth table40%2/5
A and not(B) or C or not(D)
A= exists $args{"max_callbacks"}B= not defined $args{"max_callbacks"}C= ref $args{"max_callbacks"}D= not $args{"max_callbacks"} =~ /\A[1-9]\d*\z/u
ABCDresult
0XXX0
10000
11XX1
101X1
10011
23  
24 # Load the independently usable components and share one event loop.
25 30 require Discord::Client::REST;
26 30 require Discord::Client::Gateway;
27 30 require Discord::Client::Commands;
28 30 require Discord::Client::Interactions;
29 30 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition33%1/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
3000
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table33%1/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
30 my $self = bless {
31 loop => $loop,
32 handlers => {},
33 callbacks => {},
34 on_error => $args{on_error},
35 30 ▶ max_callbacks => $args{max_callbacks} // 1024,
Condition100%2/2
$args{"max_callbacks"} // 1024
l!l
129
MC/DC100%1/1
$args{"max_callbacks"} // 1024
$args{"max_callbacks"}
Truth table100%2/2
A
A= $args{"max_callbacks"}
Aresult
11
00
36 }, $class;
37  
38 # Child callbacks must not keep the client alive through an ownership cycle.
39 30 my $weak = $self;
40 30 weaken($weak);
41  
42 # Share REST across commands and interactions; only close a client we create.
43 30 $self->{cache} = _create_cache( $args{cache} );
44 $self->{rest} = $args{rest} // Discord::Client::REST->new(
45 token => $args{token},
46 loop => $loop,
47 0 ▶ %{ $args{rest_options} // {} }
Condition20%1/5
$args{"rest"} // "Discord::Client::REST"->new("token", $args{"token"}, "loop", $loop, %{$args{"rest_options"} // {};})
l!l&&r!l&&!r
3000
$args{"rest_options"} // {}
l!l
00
MC/DC0%0/2
$args{"rest"} // "Discord::Client::REST"->new("token", $args{"token"}, "loop", $loop, %{$args{"rest_options"} // {};})
$args{"rest"} "Discord::Client::REST"->new("token", $args{"token"}, "loop", $loop, %{$args{"rest_options"} // {};})
MC/DC0%0/1
$args{"rest_options"} // {}
$args{"rest_options"}
Truth table33%1/3
A // B
A= $args{"rest"}B= "Discord::Client::REST"->new("token", $args{"token"}, "loop", $loop, %{$args{"rest_options"} // {};})
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{"rest_options"}
Aresult
11
00
48 );
49 30 $self->{own_rest} = !$args{rest};
50 $self->{commands} = Discord::Client::Commands->new(
51 rest => $self->{rest},
52 loop => $loop,
53 application_id => $args{application_id}
54 30 );
55 $self->{interactions} = Discord::Client::Interactions->new(
56 commands => $self->{commands},
57 rest => $self->{rest},
58 loop => $loop,
59 30 ▶ %{ $args{interaction_options} // {} },
Condition100%2/2
$args{"interaction_options"} // {}
l!l
1515
MC/DC100%1/1
$args{"interaction_options"} // {}
$args{"interaction_options"}
Truth table100%2/2
A
A= $args{"interaction_options"}
Aresult
11
00
60 2 ▶ on_error => sub { $weak->_report( $_[0], $_[1] ) if $weak }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $weak 2 0 2
61 30 );
62  
63 # Keep connection options separate from interaction admission limits.
64 my %gateway_options = (
65 30 ▶ %{ $args{gateway_options} // {} },
Condition100%4/4
$args{"gateway_options"} // {}
l!l
228
$args{"intents"} // ["GUILDS"]
l!l
921
MC/DC100%1/1
$args{"gateway_options"} // {}
$args{"gateway_options"}
MC/DC100%1/1
$args{"intents"} // ["GUILDS"]
$args{"intents"}
Truth table100%2/2
A
A= $args{"gateway_options"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"intents"}
Aresult
11
00
66 token => $args{token},
67 rest => $self->{rest},
68 loop => $loop,
69 intents => $args{intents} // ['GUILDS'],
70 0 ▶ on_error => sub { $weak->_report( $_[0] ) if $weak }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
71 30 );
72 30 $self->{gateway} = _create_gateway( \%args, \%gateway_options );
73  
74 # Apply SDK state before admitting application callbacks to the bounded queue.
75 $self->{gateway}->_on_dispatch(
76 70 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
77 70 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 70 70
78 70 $weak->_receive_gateway_event(@_);
79 }
80 30 );
81  
82 # Forward application notifications through the client callback lifecycle.
83 $self->{gateway}->on(
84 59 ▶ '*' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
85 59 my ( $event, $data, $gateway, $packet ) = @_;
86 59 ▶ $weak->_emit( $event, $data, $packet ) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 59 0 59
87 59 return;
88 }
89 30 );
90 30 return $self;
91 }
92  
93 30 ▶sub _create_cache {
Subroutine _create_cache: called CC=7 SCAR=21.8
94 30 my ($cache) = @_;
95  
96 # Reuse a supplied cache or construct an explicitly enabled one.
97 30 ▶ return undef unless $cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache 27 3 30
98 3 require Discord::Client::Cache;
99  
100 3 ▶ return $cache if blessed($cache) && $cache->isa('Discord::Client::Cache');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($cache) and $cache->isa("Discord::Client::Cache") 0 3 3
Condition33%1/3
builtin::blessed($cache) and $cache->isa("Discord::Client::Cache")
!ll&&!rl&&r
300
MC/DC0%0/2
builtin::blessed($cache) and $cache->isa("Discord::Client::Cache")
builtin::blessed($cache) $cache->isa("Discord::Client::Cache")
Truth table33%1/3
A and B
A= builtin::blessed($cache)B= $cache->isa("Discord::Client::Cache")
ABresult
0X0
100
111
101 3 ▶ croak 'cache must be a boolean, options hash or Cache object'
Branch: 1 branch
BranchTrueFalseTotal
if ref $cache and ref $cache ne "HASH" 0 3 3
Condition33%1/3
ref $cache and ref $cache ne "HASH"
!ll&&!rl&&r
030
MC/DC0%0/2
ref $cache and ref $cache ne "HASH"
ref $cache ref $cache ne "HASH"
Truth table33%1/3
A and B
A= ref $cacheB= ref $cache ne "HASH"
ABresult
0X0
100
111
102 if ref($cache) && ref($cache) ne 'HASH';
103 3 ▶ my %options = ref($cache) eq 'HASH' ? %$cache : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $cache eq 'HASH' ? : 3 0 3
104 3 return Discord::Client::Cache->new(%options);
105 }
106  
107 30 ▶sub _create_gateway {
Subroutine _create_gateway: called CC=3 SCAR=11.0
108 30 my ( $args, $options ) = @_;
109  
110 # Choose one connection owner without starting network I/O.
111 30 ▶ return $args->{gateway} if $args->{gateway};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"gateway"} 28 2 30
112 2 ▶ if ( exists $args->{shards} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"shards"}) 1 1 2
113 1 require Discord::Client::Gateway::Shards;
114 1 return Discord::Client::Gateway::Shards->new( %$options, shards => $args->{shards} );
115 }
116 1 return Discord::Client::Gateway->new(%$options);
117 }
118  
119 70 ▶sub _receive_gateway_event {
Subroutine _receive_gateway_event: called CC=6 SCAR=17.9
120 70 my ( $self, $event, $data, $gateway, $packet, $received_at ) = @_;
121  
122 # Publish READY state before startup observers or application handlers run.
123 70 ▶ $self->{cache}->apply( $event, $data ) if $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 11 59 70
124 70 ▶ if ( $event eq 'READY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "READY") 28 42 70
125 28 $self->{user} = $data->{user};
126 $self->{commands}->application_id( $data->{application}{id} )
127 28 ▶ if ref( $data->{application} ) eq 'HASH' && defined( $data->{application}{id} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $data->{"application"} eq "HASH" and defined $data->{"application"}{"id"} 4 24 28
Condition66%2/3
ref $data->{"application"} eq "HASH" and defined $data->{"application"}{"id"}
!ll&&!rl&&r
2404
MC/DC50%1/2
ref $data->{"application"} eq "HASH" and defined $data->{"application"}{"id"}
ref $data->{"application"} eq "HASH" defined $data->{"application"}{"id"}
Truth table66%2/3
A and B
A= ref $data->{"application"} eq "HASH"B= defined $data->{"application"}{"id"}
ABresult
0X0
100
111
128 }
129  
130 # Interaction admission has its own limit, independent of event callbacks.
131 70 ▶ $self->_dispatch_interaction( $data, $received_at ) if $event eq 'INTERACTION_CREATE';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "INTERACTION_CREATE" 25 45 70
132 70 return;
133 }
134  
135 25 ▶sub _dispatch_interaction {
Subroutine _dispatch_interaction: called CC=2 SCAR=7.9
136 25 my ( $self, $data, $received_at ) = @_;
137  
138 # Pass receipt time through so queue delays count toward acknowledgement expiry.
139 25 my %options = ( acknowledgement_only => 1 );
140 25 ▶ $options{received_at} = $received_at if defined $received_at;
Branch: 1 branch
BranchTrueFalseTotal
if defined $received_at 25 0 25
141 25 my $acknowledgement = $self->{interactions}->handle( $data, %options );
142 25 my $weak = $self;
143 25 weaken($weak);
144  
145 # Report admission errors here; the dispatcher reports admitted handler errors.
146 $acknowledgement->on_fail(
147 1 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=24.3
148 1 my ( undef, undef, $error ) = @_;
149 1 ▶ return unless $weak && !$weak->{closed} && blessed($error);
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and not $weak->{"closed"} and builtin::blessed($error) 0 1 1
Condition33%2/6
$weak and not $weak->{"closed"} and builtin::blessed($error)
!ll&&!rl&&r
001
$weak and not $weak->{"closed"}
!ll&&!rl&&r
001
MC/DC0%0/3
$weak and not $weak->{"closed"} and builtin::blessed($error)
$weak not $weak->{"closed"} builtin::blessed($error)
Truth table25%1/4
A and not(B) and C
A= $weakB= not $weak->{"closed"}C= builtin::blessed($error)
ABCresult
0XX0
10X0
1100
1111
150 1 ▶ if ( $error->category eq 'protocol' || $error->category eq 'queue_full' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error->category eq "protocol" or $error->category eq "queue_full") 0 1 1
Condition33%1/3
$error->category eq "protocol" or $error->category eq "queue_full"
l!l&&r!l&&!r
001
MC/DC0%0/2
$error->category eq "protocol" or $error->category eq "queue_full"
$error->category eq "protocol" $error->category eq "queue_full"
Truth table33%1/3
A or B
A= $error->category eq "protocol"B= $error->category eq "queue_full"
ABresult
1X1
011
000
151 0 $weak->_report($error);
152 }
153 }
154 25 );
155  
156 25 return $acknowledgement;
157 }
158  
159 # These accessors expose shared components without fetching or copying resources.
160 3 ▶sub loop { $_[0]->{loop} }
Subroutine loop: called CC=1 SCAR=0.0
Pod documented
161 0 ▶sub rest { $_[0]->{rest} }
Subroutine rest: not called CC=1 SCAR=6.9
Pod documented
162 1 ▶sub gateway { $_[0]->{gateway} }
Subroutine gateway: called CC=1 SCAR=0.0
Pod documented
163 4 ▶sub commands { $_[0]->{commands} }
Subroutine commands: called CC=1 SCAR=0.0
Pod documented
164 28 ▶sub interactions { $_[0]->{interactions} }
Subroutine interactions: called CC=1 SCAR=0.0
Pod documented
165 2 ▶sub user { $_[0]->{user} }
Subroutine user: called CC=1 SCAR=0.0
Pod documented
166 5 ▶sub cache { $_[0]->{cache} }
Subroutine cache: called CC=1 SCAR=0.0
Pod documented
167  
168 9 ▶sub model {
Subroutine model: called CC=1 SCAR=0.0
Pod documented
169 9 my ( $self, $type, $data ) = @_;
170 9 require Discord::Client::Model;
171 9 return Discord::Client::Model->wrap( $type, $data, rest => $self->{rest} );
172 }
173  
174 4 ▶sub stats {
Subroutine stats: called CC=2 SCAR=6.9
Pod documented
175 4 my ($self) = @_;
176  
177 # Expose counters without including credentials or event payloads.
178 return {
179 rest => $self->{rest}->stats,
180 gateway => $self->{gateway}->stats,
181 4 ▶ callbacks => scalar( keys %{ $self->{callbacks} } ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cache'} ? : 1 3 4
182 4 ( $self->{cache} ? ( cache => $self->{cache}->stats ) : () )
183 };
184 }
185  
186 20 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
Pod documented
187 20 my ( $self, @args ) = @_;
188 20 $self->{commands}->define(@args);
189 20 return $self;
190 }
191  
192 4 ▶sub component {
Subroutine component: called CC=1 SCAR=0.0
Pod documented
193 4 my ( $self, @args ) = @_;
194 4 $self->{commands}->component(@args);
195 4 return $self;
196 }
197  
198 15 ▶sub on {
Subroutine on: called CC=4 SCAR=15.9
Pod documented
199 15 my ( $self, $event, $callback ) = @_;
200  
201 # Normalize event names once so registration and delivery use the same keys.
202 15 ▶ croak 'Event and callback are required' unless defined($event) && !ref($event) && ref($callback) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $event and not ref $event and ref $callback eq "CODE" 0 15 15
Condition33%2/6
defined $event and not ref $event and ref $callback eq "CODE"
!ll&&!rl&&r
0015
defined $event and not ref $event
!ll&&!rl&&r
0015
MC/DC0%0/3
defined $event and not ref $event and ref $callback eq "CODE"
defined $event not ref $event ref $callback eq "CODE"
Truth table25%1/4
A and not(B) and C
A= defined $eventB= not ref $eventC= ref $callback eq "CODE"
ABCresult
0XX0
10X0
1100
1111
203 15 push @{ $self->{handlers}{ uc($event) } }, $callback;
204 15 return $self;
205 }
206  
207 5 ▶sub on_message {
Subroutine on_message: called CC=2 SCAR=7.0
Pod documented
208 5 my ( $self, $callback ) = @_;
209 5 ▶ croak 'Message callback must be a code reference' unless ref($callback) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 5 5
210  
211 # Reuse event lifetime/error handling while presenting a REST-backed snapshot.
212 return $self->on(
213 7 ▶ MESSAGE_CREATE => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 7 my ( $data, $packet, $bot ) = @_;
215 7 my $message = $bot->model( message => $data );
216 7 return $callback->( $message, $bot );
217 }
218 5 );
219 }
220  
221 59 ▶sub _emit {
Subroutine _emit: called CC=15 SCAR=27.3
222 59 my ( $self, $event, @data ) = @_;
223 59 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 59 59
224  
225 # Bound pending work before invoking another application callback.
226 59 ▶ for my $callback ( @{ $self->{handlers}{ uc($event) } // [] } ) {
Condition100%2/2
$self->{"handlers"}{uc $event} // []
l!l
1346
MC/DC100%1/1
$self->{"handlers"}{uc $event} // []
$self->{"handlers"}{uc $event}
Truth table100%2/2
A
A= $self->{"handlers"}{uc $event}
Aresult
11
00
227 15 ▶ last if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 15 15
228 15 ▶ if ( keys( %{ $self->{callbacks} } ) >= $self->{max_callbacks} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{"callbacks"};} >= $self->{"max_callbacks"}) 1 14 15
229 1 $self->_report_error( 'Client event callback capacity exceeded', 'queue_full' );
230 1 last;
231 }
232  
233 # Isolate synchronous exceptions so later listeners can still run.
234 14 my $future = eval { $callback->( @data, $self ) };
235 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 13 14
236 1 $self->_report_error( 'Client event handler failed', 'handler' );
237 1 next;
238 }
239 13 ▶ if ( $self->{closed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"closed"}) 3 10 13
240  
241 # A callback may return close itself; let that shutdown finish.
242 $future->cancel
243 if blessed($future)
244 && $future->isa('Future')
245 && !$future->is_ready
246 3 ▶ && ( !$self->{close_future} || $future != $self->{close_future} );
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($future) and $future->isa("Future") and not $future->is_ready and !$self->{"close_future"} || $future != $self->{"close_future"} 1 2 3
Condition66%8/12
builtin::blessed($future) and $future->isa("Future") and not $future->is_ready and !$self->{"close_future"} || $future != $self->{"close_future"}
!ll&&!rl&&r
111
builtin::blessed($future) and $future->isa("Future") and not $future->is_ready
!ll&&!rl&&r
012
builtin::blessed($future) and $future->isa("Future")
!ll&&!rl&&r
003
not $self->{"close_future"} or $future != $self->{"close_future"}
l!l&&r!l&&!r
011
MC/DC40%2/5
builtin::blessed($future) and $future->isa("Future") and not $future->is_ready and !$self->{"close_future"} || $future != $self->{"close_future"}
builtin::blessed($future) $future->isa("Future") not $future->is_ready not $self->{"close_future"} $future != $self->{"close_future"}
Truth table50%3/6
A and B and not(C) and not(D) or E
A= builtin::blessed($future)B= $future->isa("Future")C= not $future->is_readyD= not $self->{"close_future"}E= $future != $self->{"close_future"}
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
247 3 last;
248 }
249  
250 # Retain asynchronous work until completion and report only sanitized failures.
251 10 ▶ if ( blessed($future) && $future->isa('Future') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($future) and $future->isa("Future")) 5 5 10
Condition66%2/3
builtin::blessed($future) and $future->isa("Future")
!ll&&!rl&&r
505
MC/DC50%1/2
builtin::blessed($future) and $future->isa("Future")
builtin::blessed($future) $future->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($future)B= $future->isa("Future")
ABresult
0X0
100
111
252 5 my $key = ++$self->{callback_id};
253 5 $self->{callbacks}{$key} = $future;
254 $future->on_ready(
255 5 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
256 5 delete $self->{callbacks}{$key};
257 $self->_report_error( 'Client event handler failed', 'handler' )
258 5 ▶ if !$future->is_done && !$self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if not $future->is_done || $self->{"closed"} 1 4 5
Condition100%3/3
$future->is_done || $self->{'closed'}
l!l&&r!l&&!r
221
MC/DC100%2/2
$future->is_done || $self->{'closed'}
$future->is_done $self->{'closed'}
Truth table100%3/3
A || B
A= $future->is_doneB= $self->{'closed'}
ABresult
1X1
011
000
259 }
260 5 );
261 }
262 }
263 }
264  
265 3 ▶sub _report_error {
Subroutine _report_error: called CC=1 SCAR=0.0
266 3 my ( $self, $message, $category ) = @_;
267 3 require Discord::Client::Error;
268 3 $self->_report( Discord::Client::Error->new( message => $message, category => $category ) );
269 }
270  
271 5 ▶sub _report {
Subroutine _report: called CC=7 SCAR=21.5
272 5 my ( $self, $error, $context ) = @_;
273 5 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 5 5
274  
275 # Use the configured hooks, falling back to a sanitized warning.
276 5 ▶ my @handlers = ( $self->{on_error} ? $self->{on_error} : (), @{ $self->{handlers}{ERROR} // [] } );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'on_error'} ? : 5 0 5
Condition50%1/2
$self->{"handlers"}{"ERROR"} // []
l!l
05
MC/DC0%0/1
$self->{"handlers"}{"ERROR"} // []
$self->{"handlers"}{"ERROR"}
Truth table50%1/2
A
A= $self->{"handlers"}{"ERROR"}
Aresult
11
00
277 5 ▶ if ( !@handlers ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@handlers) 0 5 5
278 0 warn $error->message . "\n";
279 0 return;
280 }
281  
282 # An error hook must not propagate an exception into the event loop.
283 5 for my $handler (@handlers) {
284 5 my $ok = eval {
285 5 $handler->( $error, $context, $self );
286 5 1;
287 };
288 5 ▶ warn "Client error handler failed\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 5 5
289 }
290 }
291  
292 28 ▶sub start {
Subroutine start: called CC=1 SCAR=0.1
Pod documented
293 28 my ($self) = @_;
294  
295 # Tie startup cancellation to the lifetime of the whole client.
296 28 my $future = $self->{gateway}->connect->then_done($self);
297 0 ▶ $future->on_cancel( sub { $self->close } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
298 28 return $future;
299 }
300 5 ▶sub completion { $_[0]->{gateway}->completion }
Subroutine completion: called CC=1 SCAR=0.0
Pod documented
301  
302 3 ▶sub run {
Subroutine run: called CC=6 SCAR=17.9
Pod documented
303 3 my ($self) = @_;
304  
305 # Signals are optional on some platforms; remember only installed handlers.
306 3 my @signals;
307 3 for my $signal (qw(INT TERM)) {
308 6 my $id = eval {
309 0 ▶ $self->{loop}->attach_signal( $signal => sub { $self->close } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
310 };
311 6 ▶ push @signals, [ $signal, $id ] if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 6 0 6
312 }
313  
314 # Drive startup and lifetime using the same loop-backed Futures.
315 3 my $ran = eval {
316 3 $self->start->get;
317 2 $self->completion->get;
318 2 1;
319 };
320 3 my $run_error = $@;
321  
322 # Always attempt cleanup, preserving the original failure over cleanup errors.
323 3 my $closed = eval {
324 3 $self->close->get;
325 1 1;
326 };
327 3 my $close_error = $@;
328 3 $self->{loop}->detach_signal(@$_) for @signals;
329 3 ▶ die $run_error unless $ran;
Branch: 1 branch
BranchTrueFalseTotal
unless $ran 1 2 3
330 2 ▶ die $close_error unless $closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $closed 1 1 2
331 1 return;
332 }
333  
334 33 ▶sub close {
Subroutine close: called CC=5 SCAR=16.1
Pod documented
335 33 my ($self) = @_;
336 33 ▶ return $self->{close_future} if $self->{close_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"close_future"} 3 30 33
337  
338 # Stop new callbacks before cancelling work that can invoke completion hooks.
339 30 $self->{closed} = 1;
340 30 $self->{interactions}->close;
341 30 $self->{commands}->close;
342  
343 # Detach the registry before cancellation callbacks can re-enter it.
344 30 my @callbacks = values %{ $self->{callbacks} };
345 30 $self->{callbacks} = {};
346 30 for my $future (@callbacks) {
347 2 ▶ $future->cancel unless $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $future->is_ready 2 0 2
348 }
349  
350 # Wait for transport shutdown and release only the REST client we own.
351 30 my @closing = ( $self->{gateway}->close );
352 30 ▶ push @closing, $self->{rest}->close if $self->{own_rest};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"own_rest"} 0 30 30
353 30 $self->{handlers} = {};
354 30 return $self->{close_future} = Future->needs_all(@closing);
355 }
356  
357 22 1;
358  
359 __END__
« blib/lib/Discord/Client/HTTPInteractions.pm ↑ Summary blib/lib/Discord/Client/Gateway/Shards.pm »