Ext/MsgPack/RPC/Session.pm

filter: Statementstmt 46.3% 51 / 110 Branchbran 37.5% 9 / 24 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 33.3% 8 / 24 Podpod 100% 6 / 6 totaltotal 44.8% 78 / 174 CC 15 SCAR 19.9
CC
15
Cov
45%
__ANON__
30.0
next_message
12.0
request
7.3
CRAP 7.3
« Ext/LuaFuncs.pm ↑ Summary Ext/Current.pm »
1 package Neovim::Ext::MsgPack::RPC::Session;
2 56 $Neovim::Ext::MsgPack::RPC::Session::VERSION = '0.01';
3 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 56 ▶use base qw/Class::Accessor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 56 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 56 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 56 __PACKAGE__->mk_accessors (qw/async_session is_running pending_messages request_cb notification_cb/);
9  
10  
11 sub new
12 {
13 43 ▶ my ($this, $async_session) = @_;
Subroutine new: called CC=2 SCAR=7.4
Pod documented
14  
15 43 ▶ my $class = ref ($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0430
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
16 43 my $self =
17 {
18 async_session => $async_session,
19 pending_messages => [],
20 };
21  
22 43 return bless $self, $class;
23 }
24  
25  
26  
27 sub next_message
28 {
29 0 ▶ my ($this) = @_;
Subroutine next_message: not called CC=3 SCAR=24.8
Pod documented
30  
31 0 ▶ croak "event loop is already running" if ($this->is_running);
Branch: 1 branch
BranchTrueFalseTotal
if $this->is_running 0 0 0
32  
33 0 my $session = $this;
34 0 weaken ($session);
35  
36 0 ▶ if (scalar (@{$session->pending_messages}))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$session->pending_messages;}) 0 0 0
37 {
38 0 return shift @{$session->pending_messages};
39 }
40  
41 0 ▶ $this->async_session->run (sub { $session->_enqueue_request_and_stop (@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 ▶ sub { $session->_enqueue_notification_and_stop (@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
43  
44 0 return shift @{$session->pending_messages};
45 }
46  
47  
48  
49 sub request
50 {
51 104 ▶ my ($this, $method, @args) = @_;
Subroutine request: called CC=7 SCAR=19.9
Pod documented
52  
53 104 my $async = 0;
54  
55 104 my @filtered;
56 104 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 279 104 383
57 {
58 279 my $value = shift @args;
59 279 ▶ if ($value && $value eq 'async_')
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value eq "async_") 43 236 279
Condition66%2/3
$value and $value eq "async_"
!ll&&!rl&&r
023643
MC/DC50%1/2
$value and $value eq "async_"
$value $value eq "async_"
Truth table66%2/3
A and B
A= $valueB= $value eq "async_"
ABresult
0X0
100
111
60 {
61 43 $async = !!shift @args;
62 43 next;
63 }
64  
65 236 push @filtered, $value;
66 }
67  
68 104 @args = @filtered;
69  
70 104 ▶ if ($async)
Branch: 1 branch
BranchTrueFalseTotal
if ($async) 43 61 104
71 {
72 43 $this->async_session->notify ($method, \@args);
73 return
74 43 }
75  
76  
77 61 my $is_running = !!$this->is_running;
78  
79 61 my $result = [];
80  
81 61 my $future = $this->async_session->create_future();
82 $this->async_session->request ($method, \@args, sub
83 {
84 18 ▶ my ($err, $rv) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
85  
86 18 push @{$result}, $err, $rv;
87 18 ▶ if ($is_running)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_running) { } 0 18 18
88 {
89 0 $future->done();
90 }
91 else
92 {
93 18 $this->async_session->stop();
94 }
95 }
96 61 );
97  
98 61 ▶ if ($is_running)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_running) { } 0 61 61
99 {
100 0 $this->async_session->await ($future);
101 }
102 else
103 {
104 61 my $session = $this;
105 61 weaken ($session);
106  
107 0 ▶ $this->async_session->run (sub { $session->_enqueue_request (@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 61 ▶ sub { $session->_enqueue_notification (@_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
109 }
110  
111 18 my $error = shift @$result;
112 18 ▶ if ($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 18 18
113 {
114 0 die $error->[1];
115 }
116  
117 18 return $result->[0];
118 }
119  
120  
121  
122 sub run
123 {
124 0 ▶ my ($this, $request_cb, $notification_cb, $setup_cb) = @_;
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
125  
126 0 $this->request_cb ($request_cb);
127 0 $this->notification_cb ($notification_cb);
128  
129 0 my $session = $this;
130 0 weaken ($session);
131  
132 my $init = sub
133 {
134 0 ▶ $setup_cb->() if ($setup_cb);
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
if $setup_cb 0 0 0
135  
136 0 ▶ while (scalar (@{$session->pending_messages}))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$session->pending_messages;}) 0 0 0
137 {
138 0 my $msg = shift @{$session->pending_messages};
139  
140 0 my $type = shift @$msg;
141 0 ▶ if ($type eq 'request')
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'request') { } 0 0 0
elsif ($type eq 'notification') { } 0 0 0
142 {
143 0 $session->_on_request (@$msg);
144 }
145 elsif ($type eq 'notification')
146 {
147 0 $session->_on_notification (@$msg);
148 }
149 }
150 0 };
151  
152 0 $this->is_running (1);
153  
154 0 ▶ $this->async_session->run (sub { $session->_on_request (@_) }, sub { $session->_on_notification (@_) }, $init);
Subroutine __ANON__: not called CC=1 SCAR=6.9
155  
156 0 $this->is_running (0);
157 }
158  
159  
160  
161 sub stop
162 {
163 0 ▶ my ($this) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
164 0 $this->async_session->stop();
165 }
166  
167  
168  
169 sub close
170 {
171 0 ▶ my ($this) = @_;
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
172 0 $this->async_session->close();
173 }
174  
175  
176  
177 sub _on_request
178 {
179 0 ▶ my ($this, $name, $args, $response) = @_;
Subroutine _on_request: not called CC=1 SCAR=6.9
180  
181 0 my $rv = $this->request_cb->($name, $args);
182 0 $response->send ($rv);
183 }
184  
185  
186  
187 sub _on_notification
188 {
189 0 ▶ my ($this, $name, $args) = @_;
Subroutine _on_notification: not called CC=1 SCAR=6.9
190 0 $this->notification_cb->($name, $args);
191 }
192  
193  
194  
195 sub _enqueue_request
196 {
197 0 ▶ my ($this, $name, $args, $response) = @_;
Subroutine _enqueue_request: not called CC=1 SCAR=6.9
198 0 push @{$this->pending_messages}, ['request', $name, $args, $response];
199 }
200  
201  
202  
203 sub _enqueue_request_and_stop
204 {
205 0 ▶ my $this = shift;
Subroutine _enqueue_request_and_stop: not called CC=1 SCAR=6.9
206 0 $this->_enqueue_request (@_);
207 0 $this->stop();
208 }
209  
210  
211  
212 sub _enqueue_notification
213 {
214 0 ▶ my ($this, $name, $args) = @_;
Subroutine _enqueue_notification: not called CC=1 SCAR=6.9
215 0 push @{$this->pending_messages}, ['notification', $name, $args];
216 }
217  
218  
219  
220 sub _enqueue_notification_and_stop
221 {
222 0 ▶ my $this = shift;
Subroutine _enqueue_notification_and_stop: not called CC=1 SCAR=6.9
223 0 $this->_enqueue_notification (@_);
224 0 $this->stop();
225 }
226  
227 =head1 NAME
228  
229 Neovim::Ext::MsgPack::RPC::Session - Neovim::Ext::MsgPack::RPC::Session class
230  
231 =head1 VERSION
232  
233 version 0.01
234  
235 =head1 SYNOPSIS
236  
237 use Neovim::Ext;
238  
239 =head1 METHODS
240  
241 =head2 run( $request_cb, $notification_cb, [$setup_cb] )
242  
243 Run the event loop to receive requests and notifications from Neovim.
244  
245 =head2 stop( )
246  
247 Stop the event loop.
248  
249 =head2 request( $method, @args )
250  
251 Send a msgpack-rpc request and block until a response is received. If C<async_>
252 is set in C<@args>, an asynchronous notification is sent instead, and this method
253 doesn't block or return the value or error result.
254  
255 =head2 next_message( )
256  
257 Block until a message (request or notification) is available. If messages were
258 previously stored, the first one in the list will be returned, otherwise, run
259 the event loop until a message becomes available.
260  
261 =head2 close( )
262  
263 Close the event loop.
264  
265 =cut
266  
267 56 1;
« Ext/LuaFuncs.pm ↑ Summary Ext/Current.pm »