| 1 | package Neovim::Ext::MsgPack::RPC::AsyncSession; | ||||||||||||||||||||
| 2 | 56 | $Neovim::Ext::MsgPack::RPC::AsyncSession::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 Neovim::Ext::MsgPack::RPC::Response; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 56 | __PACKAGE__->mk_accessors (qw/loop msgpack_stream next_request_id pending_requests | |||||||||||||||||||
| 9 | request_cb notification_cb handlers/); | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | sub new | ||||||||||||||||||||
| 13 | { | ||||||||||||||||||||
| 14 | 43 | ▶ | my ($this, $msgpack_stream) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 43 | ▶ | croak "msgpack_stream not provided" if (!$msgpack_stream); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 43 | ▶ | my $class = ref ($this) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 19 | 43 | my $self = | |||||||||||||||||||
| 20 | { | ||||||||||||||||||||
| 21 | msgpack_stream => $msgpack_stream, | ||||||||||||||||||||
| 22 | next_request_id => 1, | ||||||||||||||||||||
| 23 | pending_requests => {}, | ||||||||||||||||||||
| 24 | loop => $msgpack_stream->loop, | ||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 43 | return bless $self, $class; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | sub request | ||||||||||||||||||||
| 33 | { | ||||||||||||||||||||
| 34 | 61 | ▶ | my ($this, $method, $args, $response_cb) = @_; | ||||||||||||||||||
|
Subroutine
request: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 61 | my $request_id = $this->next_request_id; | |||||||||||||||||||
| 37 | 61 | $this->next_request_id ($request_id+1); | |||||||||||||||||||
| 38 | 61 | ▶ | $this->msgpack_stream->send ([0, $request_id, $method, $args // []]); | ||||||||||||||||||
|
Condition50%1/2
$args // []
MC/DC0%0/1
$args // []
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||
| 39 | 61 | $this->pending_requests->{$request_id} = $response_cb; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | sub create_future | ||||||||||||||||||||
| 45 | { | ||||||||||||||||||||
| 46 | 61 | ▶ | my $this = shift; | ||||||||||||||||||
|
Subroutine
create_future: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 47 | 61 | $this->msgpack_stream->create_future();; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | sub notify | ||||||||||||||||||||
| 53 | { | ||||||||||||||||||||
| 54 | 43 | ▶ | my ($this, $method, $args) = @_; | ||||||||||||||||||
|
Subroutine
notify: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 55 | 43 | ▶ | $this->msgpack_stream->send ([2, $method, $args // []]); | ||||||||||||||||||
|
Condition50%1/2
$args // []
MC/DC0%0/1
$args // []
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub run | ||||||||||||||||||||
| 61 | { | ||||||||||||||||||||
| 62 | 61 | ▶ | my ($this, $request_cb, $notification_cb, $setup_cb) = @_; | ||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 61 | $this->request_cb ($request_cb); | |||||||||||||||||||
| 65 | 61 | $this->notification_cb ($notification_cb); | |||||||||||||||||||
| 66 | $this->msgpack_stream->run (sub | ||||||||||||||||||||
| 67 | { | ||||||||||||||||||||
| 68 | 18 | ▶ | $this->_on_message (@_); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||
| 70 | 61 | $setup_cb | |||||||||||||||||||
| 71 | ); | ||||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | sub stop | ||||||||||||||||||||
| 77 | { | ||||||||||||||||||||
| 78 | 18 | ▶ | my ($this) = @_; | ||||||||||||||||||
|
Subroutine
stop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 79 | 18 | $this->msgpack_stream->stop(); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | sub close | ||||||||||||||||||||
| 85 | { | ||||||||||||||||||||
| 86 | 0 | ▶ | my ($this) = @_; | ||||||||||||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 87 | 0 | $this->msgpack_stream->close(); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | sub await | ||||||||||||||||||||
| 93 | { | ||||||||||||||||||||
| 94 | 0 | ▶ | my ($this, $future) = @_; | ||||||||||||||||||
|
Subroutine
await: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 95 | 0 | $this->msgpack_stream->await ($future); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | sub _on_message | ||||||||||||||||||||
| 101 | { | ||||||||||||||||||||
| 102 | 18 | ▶ | my ($this, $msg) = @_; | ||||||||||||||||||
|
Subroutine
_on_message: called CC=1 SCAR=0.2
| |||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | my %handlers = | ||||||||||||||||||||
| 105 | ( | ||||||||||||||||||||
| 106 | 0 => sub | ||||||||||||||||||||
| 107 | { | ||||||||||||||||||||
| 108 | 0 | ▶ | $this->_on_request (@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 109 | }, | ||||||||||||||||||||
| 110 | 1 => sub | ||||||||||||||||||||
| 111 | { | ||||||||||||||||||||
| 112 | 18 | ▶ | $this->_on_response (@_); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 113 | }, | ||||||||||||||||||||
| 114 | 2 => sub | ||||||||||||||||||||
| 115 | { | ||||||||||||||||||||
| 116 | 0 | ▶ | $this->_on_notification (@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 117 | }, | ||||||||||||||||||||
| 118 | 18 | ); | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 18 | &{$handlers{$msg->[0]}}($msg); | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | sub _on_request | ||||||||||||||||||||
| 126 | { | ||||||||||||||||||||
| 127 | 0 | ▶ | my ($this, $msg) = @_; | ||||||||||||||||||
|
Subroutine
_on_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 0 | my $response = Neovim::Ext::MsgPack::RPC::Response->new ($this->msgpack_stream, $msg->[1]); | |||||||||||||||||||
| 130 | 0 | $this->request_cb->($msg->[2], $msg->[3], $response); | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | sub _on_response | ||||||||||||||||||||
| 136 | { | ||||||||||||||||||||
| 137 | 18 | ▶ | my ($this, $msg) = @_; | ||||||||||||||||||
|
Subroutine
_on_response: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 18 | my $cb = delete $this->pending_requests->{$msg->[1]}; | |||||||||||||||||||
| 140 | 18 | $cb->($msg->[2], $msg->[3]); | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | sub _on_notification | ||||||||||||||||||||
| 146 | { | ||||||||||||||||||||
| 147 | 0 | ▶ | my ($this, $msg) = @_; | ||||||||||||||||||
|
Subroutine
_on_notification: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 148 | 0 | $this->notification_cb->($msg->[1], $msg->[2]); | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =head1 NAME | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | Neovim::Ext::MsgPack::RPC::AsyncSession - Neovim::Ext::MsgPack::RPC::AsyncSession class | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =head1 VERSION | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | version 0.01 | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =head1 SYNOPSIS | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | use Neovim::Ext; | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =head1 METHODS | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =head2 new( $msgpack_stream ) | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | Create a new C<Neovim::Ext::MsgPack::RPC::AsyncSession>. | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =head2 request( $method, $args, \&response_cb ) | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | Send a msgpack-rpc request to Nvim. C<$response_cb> is called when the response is | ||||||||||||||||||||
| 172 | available. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head2 notify( $method, $args ) | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | Send a msgpack-rpc notification to Nvim. This has the same effect as a request, but no | ||||||||||||||||||||
| 177 | response will be received. | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =head2 run( \&request_cb, \¬ification_cb ) | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | Run the event loop to receive requests and notifications from Nvim. While the event | ||||||||||||||||||||
| 182 | loop is running, C<\&request_cb> and C<\¬ification_cb> will be called whenever | ||||||||||||||||||||
| 183 | requests or notifications are received. | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | =head2 stop( ) | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | Stop the event loop. | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | =head2 close( ) | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | Close the event loop. | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | =head2 create_future( ) | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | Create a future. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =head2 await( $future ) | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | Wait for C<$future> to complete. | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 56 | 1; | |||||||||||||||||||