File Coverage

blib/lib/Tangence/ObjectProxy.pm
Criterion Covered Total %
statement 400 449 89.0
branch 94 152 61.8
condition 31 67 46.2
subroutine 50 55 90.9
pod 17 25 68.0
total 592 748 79.1


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