Tangence/ObjectProxy.pm

filter: Statementstmt 90.0% 426 / 473 Branchbran 64.1% 118 / 184 Conditioncond 38.1% 8 / 21 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 91.0% 51 / 56 Podpod 68.0% 17 / 25 totaltotal 80.4% 621 / 772 CC 123 SCAR 22.2
CC
123
Cov
81%
_watch_property
31.6
_update_property_array
17.4
_update_property_objset
10.3
CRAP 9.2
↑ Summary Tangence/Type.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk
5  
6 9 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Tangence::ObjectProxy 0.33;
11 class Tangence::ObjectProxy;
12  
13 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶use Syntax::Keyword::Match 0.06;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Future::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 ▶use Tangence::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 9 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 NAME
27  
28 C<Tangence::ObjectProxy> - proxy for a C<Tangence> object in a
29 C<Tangence::Client>
30  
31 =head1 DESCRIPTION
32  
33 Instances in this class act as a proxy for an object in the
34 L<Tangence::Server>, allowing methods to be called, events to be subscribed
35 to, and properties to be watched.
36  
37 These objects are not directly constructed by calling the C<new> class method;
38 instead they are returned by methods on L<Tangence::Client>, or by methods on
39 other C<Tangence::ObjectProxy> instances. Ultimately every object proxy that a
40 client uses will come from either the proxy to the registry, or the root
41 object.
42  
43 =cut
44  
45 19 ▶field $_client :param :weak :reader;
Subroutine client: called CC=1 SCAR=0.0
Pod undocumented
46 56 ▶field $_id :param :reader;
Subroutine id: called CC=1 SCAR=0.0
Pod documented
47 0 ▶field $_class :param :reader;
Subroutine class: not called CC=1 SCAR=2.6
Pod documented
48  
49 9 field $_destroyed;
50  
51 9 field %_subscriptions;
52 9 field %_props;
53  
54 method destroy
55 2 ▶{
Subroutine destroy: called CC=2 SCAR=6.9
Pod undocumented
56 2 $_destroyed = 1;
57  
58 2 foreach my $cb ( @{ $_subscriptions{destroy} } ) {
59 2 $cb->();
60 }
61 9 }
62  
63 =head1 METHODS
64  
65 The following methods documented in an C<await> expression return L<Future>
66 instances.
67  
68 =cut
69  
70 9 ▶use overload '""' => \&STRING;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 method STRING
73 57 ▶{
Subroutine STRING: called CC=1 SCAR=0.0
Pod undocumented
74 57 return "Tangence::ObjectProxy[id=$_id]";
75 9 }
76  
77 =head2 id
78  
79 $id = $proxy->id;
80  
81 Returns the object ID for the C<Tangence> object being proxied for.
82  
83 =cut
84  
85 # generated accessor
86  
87 =head2 classname
88  
89 $classname = $proxy->classname;
90  
91 Returns the name of the class of the C<Tangence> object being proxied for.
92  
93 =cut
94  
95 method classname
96 6 ▶{
Subroutine classname: called CC=1 SCAR=0.0
Pod documented
97 6 return $_class->name;
98 9 }
99  
100 =head2 class
101  
102 $class = $proxyobj->class;
103  
104 Returns the L<Tangence::Meta::Class> object representing the class of this
105 object.
106  
107 =cut
108  
109 # generated accessor
110  
111 =head2 can_method
112  
113 $method = $proxy->can_method( $name );
114  
115 Returns the L<Tangence::Meta::Method> object representing the named method, or
116 C<undef> if no such method exists.
117  
118 =cut
119  
120 method can_method
121 8 ▶{
Subroutine can_method: called CC=1 SCAR=0.0
Pod documented
122 8 return $_class->method( @_ );
123 9 }
124  
125 =head2 can_event
126  
127 $event = $proxy->can_event( $name );
128  
129 Returns the L<Tangence::Meta::Event> object representing the named event, or
130 C<undef> if no such event exists.
131  
132 =cut
133  
134 method can_event
135 13 ▶{
Subroutine can_event: called CC=1 SCAR=0.0
Pod documented
136 13 return $_class->event( @_ );
137 9 }
138  
139 =head2 can_property
140  
141 $property = $proxy->can_property( $name );
142  
143 Returns the L<Tangence::Meta::Property> object representing the named
144 property, or C<undef> if no such property exists.
145  
146 =cut
147  
148 method can_property
149 118 ▶{
Subroutine can_property: called CC=1 SCAR=0.0
Pod documented
150 118 return $_class->property( @_ );
151 9 }
152  
153 # Don't want to call it "isa"
154 method proxy_isa
155 0 ▶{
Subroutine proxy_isa: not called CC=2 SCAR=17.9
Pod undocumented
156 0 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
157 0 my ( $class ) = @_;
158 0 return !! grep { $_->name eq $class } $_class, $_class->superclasses;
159 }
160 else {
161 0 return $_class, $_class->superclasses
162 }
163 9 }
164  
165 10 ▶method grab ( $smashdata )
Subroutine grab: called CC=4 SCAR=14.4
Pod undocumented
166 {
167 10 foreach my $property ( keys %{ $smashdata } ) {
168 20 my $value = $smashdata->{$property};
169 20 my $dim = $self->can_property( $property )->dimension;
170  
171 20 ▶ if( $dim == DIM_OBJSET ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim == 5) 0 20 20
172 # Comes across in a LIST. We need to map id => obj
173 0 $value = { map { $_->id => $_ } @$value };
174 }
175  
176 20 ▶ my $prop = $_props{$property} ||= {};
Condition50%1/2
$_props{$property} ||= {}
l!l
020
MC/DC0%0/1
$_props{$property} ||= {}
$_props{$property}
Truth table50%1/2
A
A= $_props{$property}
Aresult
11
00
177 20 $prop->{cache} = $value;
178 }
179 9 }
180  
181 =head2 call_method
182  
183 $result = await $proxy->call_method( $mname, @args );
184  
185 Calls the given method on the server object, passing in the given arguments.
186 Returns a L<Future> that will yield the method's result.
187  
188 =cut
189  
190 6 ▶async method call_method ( $method, @args )
Subroutine call_method: called CC=6 SCAR=18.0
Pod documented
191 6 {
192 # Detect void-context legacy uses
193 defined wantarray or
194 6 ▶ croak "->call_method in void context no longer useful - it now returns a Future";
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or croak("->call_method in void context no longer useful - it now returns a Future") 6 0 6
195  
196 6 ▶ my $mdef = $self->can_method( $method )
Branch: 1 branch
BranchTrueFalseTotal
my $mdef = $self->can_method($method) or croak('Class ' . $self->classname . " does not have a method $method") 4 2 6
197 or croak "Class ".$self->classname." does not have a method $method";
198  
199 4 my $request = Tangence::Message->new( $_client, MSG_CALL )
200 ->pack_int( $self->id )
201 ->pack_str( $method );
202  
203 4 my @argtypes = $mdef->argtypes;
204 4 $argtypes[$_]->pack_value( $request, $args[$_] ) for 0..$#argtypes;
205  
206 4 my $message = await $_client->request( request => $request );
207  
208 3 my $code = $message->code;
209  
210 3 ▶ if( $code == MSG_RESULT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 130) { } 3 0 3
211 3 ▶ my $result = $mdef->ret ? $mdef->ret->unpack_value( $message )
Branch: 1 branch
BranchTrueFalseTotal
$mdef->ret ? : 2 1 3
212 : undef;
213 3 return $result;
214 }
215 else {
216 0 Future::Exception->throw( "Unexpected response code $code", tangence => );
217 }
218 9 }
219  
220 =head2 subscribe_event
221  
222 await $proxy->subscribe_event( $event, %callbacks );
223  
224 Subscribes to the given event on the server object, installing a callback
225 function which will be invoked whenever the event is fired.
226  
227 Takes the following named callbacks:
228  
229 =over 8
230  
231 =item on_fire => CODE
232  
233 Callback function to invoke whenever the event is fired
234  
235 $on_fire->( @args );
236  
237 The returned C<Future> it is guaranteed to be completed before any invocation
238 of the C<on_fire> event handler.
239  
240 =back
241  
242 =cut
243  
244 7 ▶async method subscribe_event ( $event, %args )
Subroutine subscribe_event: called CC=7 SCAR=20.2
Pod documented
245 7 {
246 # Detect void-context legacy uses
247 defined wantarray or
248 7 ▶ croak "->subscribe_event in void context no longer useful - it now returns a Future";
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or croak("->subscribe_event in void context no longer useful - it now returns a Future") 7 0 7
249  
250 7 ▶ ref( my $callback = delete $args{on_fire} ) eq "CODE"
Branch: 1 branch
BranchTrueFalseTotal
ref(my $callback = delete $args{"on_fire"}) eq "CODE" or croak("Expected 'on_fire' as a CODE ref") 7 0 7
251 or croak "Expected 'on_fire' as a CODE ref";
252  
253 7 ▶ $self->can_event( $event )
Branch: 1 branch
BranchTrueFalseTotal
$self->can_event($event) or croak('Class ' . $self->classname . " does not have an event $event") 5 2 7
254 or croak "Class ".$self->classname." does not have an event $event";
255  
256 5 ▶ if( my $cbs = $_subscriptions{$event} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cbs = $_subscriptions{$event}) 0 5 5
257 0 push @$cbs, $callback;
258 0 return;
259 }
260  
261 5 my @cbs = ( $callback );
262 5 $_subscriptions{$event} = \@cbs;
263  
264 5 ▶ return if $event eq "destroy"; # This is automatically handled
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "destroy" 2 3 5
265  
266 3 my $message = await $_client->request(
267 request => Tangence::Message->new( $_client, MSG_SUBSCRIBE )
268 ->pack_int( $self->id )
269 ->pack_str( $event )
270 );
271  
272 2 my $code = $message->code;
273  
274 2 ▶ if( $code == MSG_SUBSCRIBED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 131) { } 2 0 2
275 2 return;
276 }
277 else {
278 0 Future::Exception->throw( "Unexpected response code $code", tangence => );
279 }
280 9 }
281  
282 2 ▶method handle_request_EVENT ( $message )
Subroutine handle_request_EVENT: called CC=4 SCAR=14.0
Pod undocumented
283 {
284 2 my $event = $message->unpack_str();
285 2 ▶ my $edef = $self->can_event( $event ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $edef = $self->can_event($event) or return 2 0 2
286  
287 2 my @args = map { $_->unpack_value( $message ) } $edef->argtypes;
288  
289 2 ▶ if( my $cbs = $_subscriptions{$event} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cbs = $_subscriptions{$event}) 2 0 2
290 2 foreach my $cb ( @$cbs ) { $cb->( @args ) }
291 }
292 9 }
293  
294 =head2 unsubscribe_event
295  
296 $proxy->unsubscribe_event( $event );
297  
298 Removes an event subscription on the given event on the server object that was
299 previously installed using C<subscribe_event>.
300  
301 =cut
302  
303 2 ▶method unsubscribe_event ( $event )
Subroutine unsubscribe_event: called CC=3 SCAR=11.2
Pod documented
304 {
305 2 ▶ $self->can_event( $event )
Branch: 1 branch
BranchTrueFalseTotal
$self->can_event($event) or croak('Class ' . $self->classname . " does not have an event $event") 2 0 2
306 or croak "Class ".$self->classname." does not have an event $event";
307  
308 2 ▶ return if $event eq "destroy"; # This is automatically handled
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "destroy" 0 2 2
309  
310 $_client->request(
311 request => Tangence::Message->new( $_client, MSG_UNSUBSCRIBE )
312 ->pack_int( $self->id )
313 ->pack_str( $event ),
314  
315 2 ▶ on_response => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
316 2 );
317 9 }
318  
319 =head2 get_property
320  
321 await $value = $proxy->get_property( $prop );
322  
323 Requests the current value of the property from the server object.
324  
325 =cut
326  
327 8 ▶async method get_property ( $property )
Subroutine get_property: called CC=4 SCAR=14.0
Pod documented
328 8 {
329 # Detect void-context legacy uses
330 defined wantarray or
331 8 ▶ croak "->get_property in void context no longer useful - it now returns a Future";
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or croak("->get_property in void context no longer useful - it now returns a Future") 8 0 8
332  
333 8 ▶ my $pdef = $self->can_property( $property )
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($property) or croak('Class ' . $self->classname . " does not have a property $property") 6 2 8
334 or croak "Class ".$self->classname." does not have a property $property";
335  
336 6 my $message = await $_client->request(
337 request => Tangence::Message->new( $_client, MSG_GETPROP )
338 ->pack_int( $self->id )
339 ->pack_str( $property ),
340 );
341  
342 5 my $code = $message->code;
343  
344 5 ▶ if( $code == MSG_RESULT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 130) { } 5 0 5
345 5 return $pdef->overall_type->unpack_value( $message );
346 }
347 else {
348 0 Future::Exception->throw( "Unexpected response code $code", tangence => );
349 }
350 9 }
351  
352 =head2 get_property_element
353  
354 await $value = $proxy->get_property_element( $property, $index_or_key );
355  
356 Requests the current value of an element of the property from the server
357 object.
358  
359 =cut
360  
361 4 ▶async method get_property_element ( $property, $index_or_key )
Subroutine get_property_element: called CC=7 SCAR=20.6
Pod documented
362 4 {
363 # Detect void-context legacy uses
364 defined wantarray or
365 4 ▶ croak "->get_property_element in void context no longer useful - it now returns a Future";
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or croak("->get_property_element in void context no longer useful - it now returns a Future") 4 0 4
366  
367 4 ▶ my $pdef = $self->can_property( $property )
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($property) or croak('Class ' . $self->classname . " does not have a property $property") 4 0 4
368 or croak "Class ".$self->classname." does not have a property $property";
369  
370 4 my $request = Tangence::Message->new( $_client, MSG_GETPROPELEM )
371 ->pack_int( $self->id )
372 ->pack_str( $property );
373  
374 match( $pdef->dimension : == ) {
375 case( DIM_HASH ) {
376 2 ▶ $request->pack_str( $index_or_key );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 4 || $(Syntax::Keyword::Match/topic) == 3 ? : 2 0 2
Condition33%1/3
$(Syntax::Keyword::Match/topic) == 4 or $(Syntax::Keyword::Match/topic) == 3
l!l&&r!l&&!r
200
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) == 4 or $(Syntax::Keyword::Match/topic) == 3
$(Syntax::Keyword::Match/topic) == 4 $(Syntax::Keyword::Match/topic) == 3
Truth table33%1/3
A or B
A= $(Syntax::Keyword::Match/topic) == 4B= $(Syntax::Keyword::Match/topic) == 3
ABresult
1X1
011
000
377 }
378 case( DIM_ARRAY ), case( DIM_QUEUE ) {
379 2 $request->pack_int( $index_or_key );
380 }
381 4 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 2 ? : 2 2 4
382 0 croak "Cannot get_property_element of a non hash, array or queue";
383 }
384 }
385  
386 4 my $message = await $_client->request(
387 request => $request,
388 );
389  
390 4 my $code = $message->code;
391  
392 4 ▶ if( $code == MSG_RESULT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 130) { } 4 0 4
393 4 return $pdef->type->unpack_value( $message );
394 }
395 else {
396 0 Future::Exception->throw( "Unexpected response code $code", tangence => );
397 }
398 9 }
399  
400 =head2 prop
401  
402 $value = $proxy->prop( $property );
403  
404 Returns the locally-cached value of a smashed property. If the named property
405 is not a smashed property, an exception is thrown.
406  
407 =cut
408  
409 17 ▶method prop ( $property )
Subroutine prop: called CC=2 SCAR=7.1
Pod documented
410 {
411 17 ▶ if( exists $_props{$property}->{cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_props{$property}{"cache"}) 17 0 17
412 17 return $_props{$property}->{cache};
413 }
414  
415 0 croak "$self does not have a cached property '$property'";
416 9 }
417  
418 =head2 set_property
419  
420 await $proxy->set_property( $prop, $value );
421  
422 Sets the value of the property in the server object.
423  
424 =cut
425  
426 9 ▶async method set_property ( $property, $value )
Subroutine set_property: called CC=4 SCAR=14.1
Pod documented
427 9 {
428 # Detect void-context legacy uses
429 defined wantarray or
430 9 ▶ croak "->set_property in void context no longer useful - it now returns a Future";
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or croak("->set_property in void context no longer useful - it now returns a Future") 9 0 9
431  
432 9 ▶ my $pdef = $self->can_property( $property )
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($property) or croak('Class ' . $self->classname . " does not have a property $property") 9 0 9
433 or croak "Class ".$self->classname." does not have a property $property";
434  
435 9 my $request = Tangence::Message->new( $_client, MSG_SETPROP )
436 ->pack_int( $self->id )
437 ->pack_str( $property );
438 9 $pdef->overall_type->pack_value( $request, $value );
439  
440 9 my $message = await $_client->request(
441 request => $request,
442 );
443  
444 7 my $code = $message->code;
445  
446 7 ▶ if( $code == MSG_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 128) { } 7 0 7
447 7 return;
448 }
449 else {
450 0 Future::Exception->throw( "Unexpected response code $code", tangence => );
451 }
452 9 }
453  
454 =head2 watch_property
455  
456 await $proxy->watch_property( $property, %callbacks );
457  
458 =head2 watch_property_with_initial
459  
460 await $proxy->watch_property_with_initial( $property, %callbacks );
461  
462 Watches the given property on the server object, installing callback functions
463 which will be invoked whenever the property value changes. The latter form
464 additionally ensures that the server will send the current value of the
465 property as an initial update to the C<on_set> event, atomically when it
466 installs the update watches.
467  
468 Takes the following named arguments:
469  
470 =over 8
471  
472 =item on_updated => CODE
473  
474 Optional. Callback function to invoke whenever the property value changes.
475  
476 $on_updated->( $new_value );
477  
478 If not provided, then individual handlers for individual change types must be
479 provided.
480  
481 =back
482  
483 The set of callback functions that are required depends on the type of the
484 property. These are documented in the C<watch_property> method of
485 L<Tangence::Object>.
486  
487 =cut
488  
489 24 ▶sub _watchcbs_from_args ( $pdef, %args )
Subroutine _watchcbs_from_args: called CC=7 SCAR=19.7
490 {
491 24 my $callbacks = {};
492 24 my $on_updated = delete $args{on_updated};
493 24 ▶ if( $on_updated ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_updated) 5 19 24
494 5 ▶ ref $on_updated eq "CODE" or croak "Expected 'on_updated' to be a CODE ref";
Branch: 1 branch
BranchTrueFalseTotal
ref $on_updated eq "CODE" or croak("Expected 'on_updated' to be a CODE ref") 5 0 5
495 5 $callbacks->{on_updated} = $on_updated;
496 }
497  
498 24 foreach my $name ( @{ CHANGETYPES->{$pdef->dimension} } ) {
499 # All of these become optional if 'on_updated' is supplied
500 52 ▶ next if $on_updated and not exists $args{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $on_updated and not exists $args{$name} 15 37 52
Condition66%2/3
$on_updated and not exists $args{$name}
!ll&&!rl&&r
37015
MC/DC50%1/2
$on_updated and not exists $args{$name}
$on_updated not exists $args{$name}
Truth table66%2/3
A and not(B)
A= $on_updatedB= not exists $args{$name}
ABresult
0X0
100
111
501  
502 37 ▶ ref( $callbacks->{$name} = delete $args{$name} ) eq "CODE"
Branch: 1 branch
BranchTrueFalseTotal
ref($callbacks->{$name} = delete $args{$name}) eq "CODE" or croak("Expected '${name}' as a CODE ref") 37 0 37
503 or croak "Expected '$name' as a CODE ref";
504 }
505  
506 24 return $callbacks;
507 }
508  
509 5 ▶method watch_property { $self->_watch_property( shift, 0, @_ ) }
Subroutine watch_property: called CC=1 SCAR=0.0
Pod documented
510 13 ▶method watch_property_with_initial { $self->_watch_property( shift, 1, @_ ) }
Subroutine watch_property_with_initial: called CC=1 SCAR=0.0
Pod documented
511  
512 18 ▶async method _watch_property ( $property, $want_initial, %args )
Subroutine _watch_property: called CC=17 SCAR=34.5
513 18 {
514 # Detect void-context legacy uses
515 defined wantarray or
516 18 ▶ croak "->watch_property in void context no longer useful - it now returns a Future";
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or croak("->watch_property in void context no longer useful - it now returns a Future") 18 0 18
517  
518 18 ▶ my $pdef = $self->can_property( $property )
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($property) or croak('Class ' . $self->classname . " does not have a property $property") 18 0 18
519 or croak "Class ".$self->classname." does not have a property $property";
520  
521 18 my $callbacks = _watchcbs_from_args( $pdef, %args );
522  
523 # Smashed properties behave differently
524 18 my $smash = $pdef->smashed;
525  
526 18 ▶ if( my $cbs = $_props{$property}->{cbs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cbs = $_props{$property}{"cbs"}) 3 15 18
527 3 ▶ if( $want_initial and !$smash ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($want_initial and not $smash) { } 3 0 3
elsif ($want_initial and $smash) { } 0 0 0
Condition33%1/3
$want_initial and not $smash
!ll&&!rl&&r
003
MC/DC0%0/2
$want_initial and not $smash
$want_initial not $smash
Truth table33%1/3
A and not(B)
A= $want_initialB= not $smash
ABresult
0X0
100
111
528 3 my $value = await $self->get_property( $property );
529  
530 3 ▶ $callbacks->{on_set} and $callbacks->{on_set}->( $value );
Branch: 1 branch
BranchTrueFalseTotal
$callbacks->{"on_set"} and $callbacks->{"on_set"}->($value) 2 1 3
531 3 ▶ $callbacks->{on_updated} and $callbacks->{on_updated}->( $value );
Branch: 1 branch
BranchTrueFalseTotal
$callbacks->{"on_updated"} and $callbacks->{"on_updated"}->($value) 1 2 3
532 3 push @$cbs, $callbacks;
533 3 return;
534 }
535 ▶ elsif( $want_initial and $smash ) {
Condition0%0/3
$want_initial and $smash
!ll&&!rl&&r
000
MC/DC0%0/2
$want_initial and $smash
$want_initial $smash
Truth table0%0/3
A and B
A= $want_initialB= $smash
ABresult
0X0
100
111
536 0 my $cache = $_props{$property}->{cache};
537 0 ▶ $callbacks->{on_set} and $callbacks->{on_set}->( $cache );
Branch: 1 branch
BranchTrueFalseTotal
$callbacks->{"on_set"} and $callbacks->{"on_set"}->($cache) 0 0 0
538 0 ▶ $callbacks->{on_updated} and $callbacks->{on_updated}->( $cache );
Branch: 1 branch
BranchTrueFalseTotal
$callbacks->{"on_updated"} and $callbacks->{"on_updated"}->($cache) 0 0 0
539 0 push @$cbs, $callbacks;
540 0 return;
541 }
542 else {
543 0 push @$cbs, $callbacks;
544 0 return;
545 }
546  
547 0 die "UNREACHED";
548 }
549  
550 15 $_props{$property}->{cbs} = [ $callbacks ];
551  
552 15 ▶ if( $smash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smash) 2 13 15
553 2 ▶ if( $want_initial ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_initial) 2 0 2
554 2 my $cache = $_props{$property}->{cache};
555 2 ▶ $callbacks->{on_set} and $callbacks->{on_set}->( $cache );
Branch: 1 branch
BranchTrueFalseTotal
$callbacks->{"on_set"} and $callbacks->{"on_set"}->($cache) 2 0 2
556 2 ▶ $callbacks->{on_updated} and $callbacks->{on_updated}->( $cache );
Branch: 1 branch
BranchTrueFalseTotal
$callbacks->{"on_updated"} and $callbacks->{"on_updated"}->($cache) 0 2 2
557 }
558  
559 2 return;
560 }
561  
562 13 my $request = Tangence::Message->new( $_client, MSG_WATCH )
563 ->pack_int( $self->id )
564 ->pack_str( $property )
565 ->pack_bool( $want_initial );
566  
567 13 my $message = await $_client->request( request => $request );
568  
569 12 my $code = $message->code;
570  
571 12 ▶ if( $code == MSG_WATCHING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 132) { } 12 0 12
572 12 return;
573 }
574 else {
575 0 Future::Exception->throw( "Unexpected response code $code", tangence => );
576 }
577 9 }
578  
579 =head2 watch_property_with_cursor
580  
581 ( $cursor, $first_idx, $last_idx ) =
582 await $proxy->watch_property_with_cursor( $property, $from, %callbacks );
583  
584 A variant of C<watch_property> that installs a watch on the given property of
585 the server object, and additionally returns an cursor object that can be used
586 to lazily fetch the values stored in it.
587  
588 The C<$from> value indicates which end of the queue the cursor should start
589 from; C<CUSR_FIRST> to start at index 0, or C<CUSR_LAST> to start at the
590 highest-numbered index. The cursor is created atomically with installing the
591 watch.
592  
593 =cut
594  
595 method watch_property_with_iter
596 0 ▶{
Subroutine watch_property_with_iter: not called CC=2 SCAR=17.9
Pod undocumented
597 # Detect void-context legacy uses
598 defined wantarray or
599 0 ▶ croak "->watch_property_with_iter in void context no longer useful - it now returns a Future";
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or croak("->watch_property_with_iter in void context no longer useful - it now returns a Future") 0 0 0
600  
601 0 return $self->watch_property_with_cursor( @_ );
602 9 }
603  
604 6 ▶async method watch_property_with_cursor ( $property, $from, %args )
Subroutine watch_property_with_cursor: called CC=8 SCAR=21.8
Pod documented
605 6 {
606 match( $from : eq ) {
607 5 ▶ case( "first" ) { $from = CUSR_FIRST }
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'last' ? : 1 0 1
608 1 case( "last" ) { $from = CUSR_LAST }
609 0 ▶ default { croak "Unrecognised 'from' value $from" }
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'first' ? : 5 1 6
610 }
611  
612 6 ▶ my $pdef = $self->can_property( $property )
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($property) or croak('Class ' . $self->classname . " does not have a property $property") 6 0 6
613 or croak "Class ".$self->classname." does not have a property $property";
614  
615 6 my $callbacks = _watchcbs_from_args( $pdef, %args );
616  
617 # Smashed properties behave differently
618 6 my $smashed = $pdef->smashed;
619  
620 6 ▶ if( my $cbs = $_props{$property}->{cbs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cbs = $_props{$property}{"cbs"}) 0 6 6
621 0 die "TODO: need to synthesize a second cursor for $self";
622 }
623  
624 6 $_props{$property}->{cbs} = [ $callbacks ];
625  
626 6 ▶ if( $smashed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smashed) 0 6 6
627 0 die "TODO: need to synthesize an cursor";
628 }
629  
630 6 ▶ $pdef->dimension == DIM_QUEUE or croak "Can only iterate on queue-dimension properties";
Branch: 1 branch
BranchTrueFalseTotal
$pdef->dimension == 3 or croak("Can only iterate on queue-dimension properties") 6 0 6
631  
632 6 my $message = await $_client->request(
633 request => Tangence::Message->new( $_client, MSG_WATCH_CUSR )
634 ->pack_int( $self->id )
635 ->pack_str( $property )
636 ->pack_int( $from ),
637 );
638  
639 6 my $code = $message->code;
640  
641 6 ▶ if( $code == MSG_WATCHING_CUSR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 133) { } 6 0 6
642 6 my $cursor_id = $message->unpack_int();
643 6 my $first_idx = $message->unpack_int();
644 6 my $last_idx = $message->unpack_int();
645  
646 6 my $cursor = Tangence::ObjectProxy::_Cursor->new( $self, $cursor_id, $pdef->type );
647 6 return ( $cursor, $first_idx, $last_idx );
648 }
649 else {
650 0 Future::Exception->throw( "Unexpected response code $code", tangence => );
651 }
652 9 }
653  
654 47 ▶method handle_request_UPDATE ( $message )
Subroutine handle_request_UPDATE: called CC=6 SCAR=18.2
Pod undocumented
655 {
656 47 my $prop = $message->unpack_str();
657 47 my $how = TYPE_U8->unpack_value( $message );
658  
659 47 ▶ my $pdef = $self->can_property( $prop ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($prop) or return 47 0 47
660 47 my $type = $pdef->type;
661 47 my $dim = $pdef->dimension;
662  
663 47 ▶ my $p = $_props{$prop} ||= {};
Condition50%1/2
$_props{$prop} ||= {}
l!l
470
MC/DC0%0/1
$_props{$prop} ||= {}
$_props{$prop}
Truth table50%1/2
A
A= $_props{$prop}
Aresult
11
00
664  
665 47 my $dimname = DIMNAMES->[$dim];
666 47 ▶ if( my $code = $self->can( "_update_property_$dimname" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->can("_update_property_$dimname")) { } 47 0 47
667 47 $code->( $self, $p, $type, $how, $message );
668 }
669 else {
670 0 croak "Unrecognised property dimension $dim for $prop";
671 }
672  
673 47 ▶ $_->{on_updated} and $_->{on_updated}->( $p->{cache} ) for @{ $p->{cbs} };
Branch: 1 branch
BranchTrueFalseTotal
$_->{"on_updated"} and $_->{"on_updated"}->($p->{"cache"}) 12 37 49
674 9 }
675  
676 15 ▶method _update_property_scalar ( $p, $type, $how, $message )
Subroutine _update_property_scalar: called CC=4 SCAR=14.1
677 {
678 match( $how : == ) {
679 case( CHANGE_SET ) {
680 15 my $value = $type->unpack_value( $message );
681 15 $p->{cache} = $value;
682 15 ▶ $_->{on_set} and $_->{on_set}->( $p->{cache} ) for @{ $p->{cbs} };
Branch: 1 branch
BranchTrueFalseTotal
$_->{"on_set"} and $_->{"on_set"}->($p->{"cache"}) 17 0 17
683 }
684 15 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 1 ? : 15 0 15
685 0 croak "Change type $how is not valid for a scalar property";
686 }
687 }
688 9 }
689  
690 8 ▶method _update_property_hash ( $p, $type, $how, $message )
Subroutine _update_property_hash: called CC=10 SCAR=23.0
691 {
692 match( $how : == ) {
693 case( CHANGE_SET ) {
694 2 my $value = Tangence::Type->make( dict => $type )->unpack_value( $message );
695 2 $p->{cache} = $value;
696 2 ▶ $_->{on_set} and $_->{on_set}->( $p->{cache} ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_set"} and $_->{"on_set"}->($p->{"cache"}) 1 1 2
$(Syntax::Keyword::Match/topic) == 2 ? : 4 2 6
697 }
698 case( CHANGE_ADD ) {
699 4 my $key = $message->unpack_str();
700 4 my $value = $type->unpack_value( $message );
701 4 $p->{cache}->{$key} = $value;
702 4 ▶ $_->{on_add} and $_->{on_add}->( $key, $value ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_add"} and $_->{"on_add"}->($key, $value) 1 3 4
$(Syntax::Keyword::Match/topic) == 3 ? : 2 0 2
703 }
704 case( CHANGE_DEL ) {
705 2 my $key = $message->unpack_str();
706 2 delete $p->{cache}->{$key};
707 2 ▶ $_->{on_del} and $_->{on_del}->( $key ) for @{ $p->{cbs} };
Branch: 1 branch
BranchTrueFalseTotal
$_->{"on_del"} and $_->{"on_del"}->($key) 1 1 2
708 }
709 8 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 1 ? : 2 6 8
710 0 croak "Change type $how is not valid for a hash property";
711 }
712 }
713 9 }
714  
715 7 ▶method _update_property_queue ( $p, $type, $how, $message )
Subroutine _update_property_queue: called CC=10 SCAR=23.3
716 {
717 match( $how : == ) {
718 case( CHANGE_SET ) {
719 1 my $value = Tangence::Type->make( list => $type )->unpack_value( $message );
720 1 $p->{cache} = $value;
721 1 ▶ $_->{on_set} and $_->{on_set}->( $p->{cache} ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_set"} and $_->{"on_set"}->($p->{"cache"}) 1 0 1
$(Syntax::Keyword::Match/topic) == 4 ? : 3 3 6
722 }
723 case( CHANGE_PUSH ) {
724 3 my @value = $message->unpack_all_sametype( $type );
725 3 push @{ $p->{cache} }, @value;
726 3 ▶ $_->{on_push} and $_->{on_push}->( @value ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_push"} and $_->{"on_push"}->(@value) 3 0 3
$(Syntax::Keyword::Match/topic) == 5 ? : 3 0 3
727 }
728 case( CHANGE_SHIFT ) {
729 3 my $count = $message->unpack_int();
730 3 splice @{ $p->{cache} }, 0, $count, ();
731 3 ▶ $_->{on_shift} and $_->{on_shift}->( $count ) for @{ $p->{cbs} };
Branch: 1 branch
BranchTrueFalseTotal
$_->{"on_shift"} and $_->{"on_shift"}->($count) 3 0 3
732 }
733 7 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 1 ? : 1 6 7
734 0 croak "Change type $how is not valid for a queue property";
735 }
736 }
737 9 }
738  
739 13 ▶method _update_property_array ( $p, $type, $how, $message )
Subroutine _update_property_array: called CC=17 SCAR=28.6
740 {
741 match( $how : == ) {
742 case( CHANGE_SET ) {
743 4 my $value = Tangence::Type->make( list => $type )->unpack_value( $message );
744 4 $p->{cache} = $value;
745 4 ▶ $_->{on_set} and $_->{on_set}->( $p->{cache} ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_set"} and $_->{"on_set"}->($p->{"cache"}) 2 2 4
$(Syntax::Keyword::Match/topic) == 4 ? : 3 6 9
746 }
747 case( CHANGE_PUSH ) {
748 3 my @value = $message->unpack_all_sametype( $type );
749 3 push @{ $p->{cache} }, @value;
750 3 ▶ $_->{on_push} and $_->{on_push}->( @value ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_push"} and $_->{"on_push"}->(@value) 1 2 3
$(Syntax::Keyword::Match/topic) == 5 ? : 1 5 6
751 }
752 case( CHANGE_SHIFT ) {
753 1 my $count = $message->unpack_int();
754 1 splice @{ $p->{cache} }, 0, $count, ();
755 1 ▶ $_->{on_shift} and $_->{on_shift}->( $count ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_shift"} and $_->{"on_shift"}->($count) 1 0 1
$(Syntax::Keyword::Match/topic) == 6 ? : 2 3 5
756 }
757 case( CHANGE_SPLICE ) {
758 2 my $start = $message->unpack_int();
759 2 my $count = $message->unpack_int();
760 2 my @value = $message->unpack_all_sametype( $type );
761 2 splice @{ $p->{cache} }, $start, $count, @value;
762 2 ▶ $_->{on_splice} and $_->{on_splice}->( $start, $count, @value ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_splice"} and $_->{"on_splice"}->($start, $count, @value) 1 1 2
$(Syntax::Keyword::Match/topic) == 7 ? : 3 0 3
763 }
764 case( CHANGE_MOVE ) {
765 3 my $index = $message->unpack_int();
766 3 my $delta = $message->unpack_int();
767 # it turns out that exchanging neighbours is quicker by list assignment,
768 # but other times it's generally best to use splice() to extract then
769 # insert
770 3 ▶ if( abs($delta) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $delta == 1) { } 0 3 3
771 0 @{$p->{cache}}[$index,$index+$delta] = @{$p->{cache}}[$index+$delta,$index];
772 }
773 else {
774 3 my $elem = splice @{ $p->{cache} }, $index, 1, ();
775 3 splice @{ $p->{cache} }, $index + $delta, 0, ( $elem );
776 }
777 3 ▶ $_->{on_move} and $_->{on_move}->( $index, $delta ) for @{ $p->{cbs} };
Branch: 1 branch
BranchTrueFalseTotal
$_->{"on_move"} and $_->{"on_move"}->($index, $delta) 1 2 3
778 }
779 13 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 1 ? : 4 9 13
780 0 croak "Change type $how is not valid for an array property";
781 }
782 }
783 9 }
784  
785 4 ▶method _update_property_objset ( $p, $type, $how, $message )
Subroutine _update_property_objset: called CC=10 SCAR=23.3
786 {
787 match( $how : == ) {
788 case( CHANGE_SET ) {
789 # Comes across in a LIST. We need to map id => obj
790 2 my $objects = Tangence::Type->make( list => $type )->unpack_value( $message );
791 2 $p->{cache} = { map { $_->id => $_ } @$objects };
792 2 ▶ $_->{on_set} and $_->{on_set}->( $p->{cache} ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_set"} and $_->{"on_set"}->($p->{"cache"}) 2 0 2
$(Syntax::Keyword::Match/topic) == 2 ? : 1 1 2
793 }
794 case( CHANGE_ADD ) {
795 # Comes as object only
796 1 my $obj = $type->unpack_value( $message );
797 1 $p->{cache}->{$obj->id} = $obj;
798 1 ▶ $_->{on_add} and $_->{on_add}->( $obj ) for @{ $p->{cbs} };
Branch: 2 branches
BranchTrueFalseTotal
$_->{"on_add"} and $_->{"on_add"}->($obj) 1 0 1
$(Syntax::Keyword::Match/topic) == 3 ? : 1 0 1
799 }
800 case( CHANGE_DEL ) {
801 # Comes as ID number only
802 1 my $id = $message->unpack_int();
803 1 delete $p->{cache}->{$id};
804 1 ▶ $_->{on_del} and $_->{on_del}->( $id ) for @{ $p->{cbs} };
Branch: 1 branch
BranchTrueFalseTotal
$_->{"on_del"} and $_->{"on_del"}->($id) 1 0 1
805 }
806 4 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 1 ? : 2 2 4
807 0 croak "Change type $how is not valid for an objset property";
808 }
809 }
810 9 }
811  
812 =head2 unwatch_property
813  
814 $proxy->unwatch_property( $property );
815  
816 Removes a property watches on the given property on the server object that was
817 previously installed using C<watch_property>.
818  
819 =cut
820  
821 4 ▶method unwatch_property ( $property )
Subroutine unwatch_property: called CC=2 SCAR=7.0
Pod documented
822 {
823 4 ▶ $self->can_property( $property )
Branch: 1 branch
BranchTrueFalseTotal
$self->can_property($property) or croak('Class ' . $self->classname . " does not have a property $property") 4 0 4
824 or croak "Class ".$self->classname." does not have a property $property";
825  
826 # TODO: mark cursors as destroyed and invalid
827 4 delete $_props{$property};
828  
829 $_client->request(
830 request => Tangence::Message->new( $_client, MSG_UNWATCH )
831 ->pack_int( $self->id )
832 ->pack_str( $property ),
833  
834 4 ▶ on_response => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
835 4 );
836 9 }
837  
838 class Tangence::ObjectProxy::_Cursor
839 {
840 9 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
841 9 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
842  
843 =head1 CURSOR METHODS
844  
845 The following methods are availilable on the property cursor objects returned
846 by the C<watch_property_with_cursor> method.
847  
848 =cut
849  
850 0 ▶ field $obj :param :reader;
Subroutine obj: not called CC=1 SCAR=2.6
851 0 ▶ field $id :param :reader;
Subroutine id: not called CC=1 SCAR=2.6
852 9 field $element_type :param;
853  
854 6 ▶ sub BUILDARGS ( $class, $obj, $id, $element_type )
Subroutine BUILDARGS: called CC=1 SCAR=0.0
855 {
856 6 return ( obj => $obj, id => $id, element_type => $element_type );
857 }
858  
859 18 ▶ method client { $obj->client }
Subroutine client: called CC=1 SCAR=0.0
860  
861 # TODO: Object::Pad probably should do this bit
862 method DESTROY
863 6 ▶ {
Subroutine DESTROY: called CC=3 SCAR=13.1
864 6 ▶ return unless $obj and my $client = $self->client;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj and my $client = $self->client 0 6 6
Condition33%1/3
$obj and my $client = $self->client
!ll&&!rl&&r
006
MC/DC0%0/2
$obj and my $client = $self->client
$obj my $client = $self->client
Truth table33%1/3
A and B
A= $objB= my $client = $self->client
ABresult
0X0
100
111
865  
866 $client->request(
867 request => Tangence::Message->new( $client, MSG_CUSR_DESTROY )
868 ->pack_int( $id ),
869  
870 6 ▶ on_response => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
871 6 );
872 9 }
873  
874 =head2 next_forward
875  
876 ( $index, @more ) = await $cursor->next_forward( $count );
877  
878 =head2 next_backward
879  
880 ( $index, @more ) = await $cursor->next_backward( $count );
881  
882 Requests the next items from the cursor. C<next_forward> moves forwards
883 towards higher-numbered indices, and C<next_backward> moves backwards towards
884 lower-numbered indices. If C<$count> is unspecified, a default of 1 will
885 apply.
886  
887 The returned future wil yield the index of the first element returned, and the
888 new elements. Note that there may be fewer elements returned than were
889 requested, if the end of the queue was reached. Specifically, there will be no
890 new elements if the cursor is already at the end.
891  
892 =cut
893  
894 method next_forward
895 7 ▶ {
Subroutine next_forward: called CC=1 SCAR=0.0
896 7 $self->_next( CUSR_FWD, @_ );
897 9 }
898  
899 method next_backward
900 5 ▶ {
Subroutine next_backward: called CC=1 SCAR=0.0
901 5 $self->_next( CUSR_BACK, @_ );
902 9 }
903  
904 12 ▶ async method _next ( $direction, $count = 1 )
Subroutine _next: called CC=5 SCAR=16.6
905 12 {
906 # Detect void-context legacy uses
907 defined wantarray or
908 12 ▶ croak "->next_forward/backward in void context no longer useful - it now returns a Future";
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or croak("->next_forward/backward in void context no longer useful - it now returns a Future") 12 0 12
909  
910 12 my $client = $self->client;
911  
912 12 ▶ my $message = await $client->request(
Condition50%1/2
$count || 1
l!l
120
MC/DC0%0/1
$count || 1
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
913 request => Tangence::Message->new( $client, MSG_CUSR_NEXT )
914 ->pack_int( $id )
915 ->pack_int( $direction )
916 ->pack_int( $count || 1 ),
917 );
918  
919 12 my $code = $message->code;
920  
921 12 ▶ if( $code == MSG_CUSR_RESULT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 134) { } 12 0 12
922 return (
923 12 $message->unpack_int(),
924 $message->unpack_all_sametype( $element_type ),
925 );
926 }
927 else {
928 0 Future::Exception->throw( "Unexpected response code $code", tangence => );
929 }
930 9 }
931 }
932  
933 9 =head1 AUTHOR
934  
935 Paul Evans <leonerd@leonerd.org.uk>
936  
937 =cut
938  
939 9 0x55AA;
↑ Summary Tangence/Type.pm »