| 1 | package Object::Remote::Future; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base qw(Exporter); | ||
| 6 | |||
| 7 | use Object::Remote::Logging qw( :log router ); | ||
| 8 | |||
| 9 | BEGIN { router()->exclude_forwarding } | ||
| 10 | |||
| 11 | use Future; | ||
| 12 | |||
| 13 | our @EXPORT = qw(future await_future await_all); | ||
| 14 | |||
| 15 | sub future (&;$) { | ||
| 16 | my $f = $_[0]->(Object::Remote->current_loop->new_future); | ||
| 17 | return $f if ((caller(1+($_[1]||0))||'') eq 'start'); | ||
| 18 | await_future($f); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub await_future { | ||
| 22 | my $f = shift; | ||
| 23 | Object::Remote->current_loop->await($f); | ||
| 24 | return wantarray ? $f->get : ($f->get)[0]; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub await_all { | ||
| 28 | log_trace { my $l = @_; "await_all() invoked with '$l' futures to wait on" }; | ||
| 29 | Object::Remote->current_loop->await_all(@_); | ||
| 30 | map $_->get, @_; | ||
| 31 | } | ||
| 32 | |||
| 33 | package # hide from PAUSE | ||
| 34 | start; | ||
| 35 | |||
| 36 | our $start = sub { my ($obj, $call) = (shift, shift); $obj->$call(@_); }; | ||
| 37 | |||
| 38 | sub AUTOLOAD { | ||
| 39 | my $invocant = shift; | ||
| 40 | my ($method) = our $AUTOLOAD =~ /^start::(.+)$/; | ||
| 41 | my $res; | ||
| 42 | unless (eval { $res = $invocant->$method(@_); 1 }) { | ||
| 43 | my $f = Object::Remote->current_loop->new_future; | ||
| 44 | $f->fail($@); | ||
| 45 | return $f; | ||
| 46 | } | ||
| 47 | unless (Scalar::Util::blessed($res) and $res->isa('Future')) { | ||
| 48 | my $f = Object::Remote->current_loop->new_future; | ||
| 49 | $f->done($res); | ||
| 50 | return $f; | ||
| 51 | } | ||
| 52 | return $res; | ||
| 53 | } | ||
| 54 | |||
| 55 | package # hide from PAUSE | ||
| 56 | maybe; | ||
| 57 | |||
| 58 | sub start { | ||
| 59 | my ($obj, $call) = (shift, shift); | ||
| 60 | if ((caller(1)||'') eq 'start') { | ||
| 61 | $obj->$start::start($call => @_); | ||
| 62 | } else { | ||
| 63 | $obj->$call(@_); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | package # hide from PAUSE | ||
| 68 | maybe::start; | ||
| 69 | |||
| 70 | sub AUTOLOAD { | ||
| 71 | my $invocant = shift; | ||
| 72 | my ($method) = our $AUTOLOAD =~ /^maybe::start::(.+)$/; | ||
| 73 | $method = "start::${method}" if ((caller(1)||'') eq 'start'); | ||
| 74 | $invocant->$method(@_); | ||
| 75 | } | ||
| 76 | |||
| 77 | package # hide from PAUSE | ||
| 78 | then; | ||
| 79 | |||
| 80 | sub AUTOLOAD { | ||
| 81 | my $invocant = shift; | ||
| 82 | my ($method) = our $AUTOLOAD =~ /^then::(.+)$/; | ||
| 83 | my @args = @_; | ||
| 84 | return $invocant->then(sub { | ||
| 85 | my ($obj) = @_; | ||
| 86 | return $obj->${\"start::${method}"}(@args); | ||
| 87 | }); | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | =head1 NAME | ||
| 93 | |||
| 94 | Object::Remote::Future - Asynchronous calling for L<Object::Remote> | ||
| 95 | |||
| 96 | =head1 LAME | ||
| 97 | |||
| 98 | Shipping prioritised over writing this part up. Blame mst. | ||
| 99 | |||
| 100 | =cut |