1 package Object::Remote::Future;
9 our @EXPORT = qw(future await_future await_all);
12 my $f = $_[0]->(CPS::Future->new);
13 return $f if ((caller(1+($_[1]||0))||'') eq 'start');
21 return $f if $f->is_ready;
22 require Object::Remote;
23 my $loop = Object::Remote->current_loop;
25 local @await = (@await, $f);
27 $loop->stop if $f == $await[-1]
29 warn "running loop for ${f} [@{await}]";
32 if (@await and $await[-1]->is_ready) {
35 return wantarray ? $f->get : ($f->get)[0];
41 warn "awaiting ${sub}";
42 await_future(CPS::Future->wait_all($sub));
49 our $start = sub { my ($obj, $call) = (shift, shift); $obj->$call(@_); };
53 my ($method) = our $AUTOLOAD =~ /^start::(.+)$/;
55 unless (eval { $res = $invocant->$method(@_); 1 }) {
56 my $f = CPS::Future->new;
60 unless (Scalar::Util::blessed($res) and $res->isa('CPS::Future')) {
61 my $f = CPS::Future->new;
71 my ($obj, $call) = (shift, shift);
72 if ((caller(1)||'') eq 'start') {
73 $obj->$start::start($call => @_);
83 my ($method) = our $AUTOLOAD =~ /^maybe::start::(.+)$/;
84 $method = "start::${method}" if ((caller(1)||'') eq 'start');
85 $invocant->$method(@_);
92 my ($method) = our $AUTOLOAD =~ /^then::(.+)$/;
94 # Need two copies since if we're called on an already complete future
95 # $f will be freed immediately
96 my $ret = my $f = CPS::Future->new;
97 $invocant->on_fail(sub { $f->fail(@_); undef($f); });
98 $invocant->on_done(sub {
100 my $next = $obj->${\"start::${method}"}(@args);
101 $next->on_done(sub { $f->done(@_); undef($f); });
102 $next->on_fail(sub { $f->fail(@_); undef($f); });
111 Object::Remote::Future - Asynchronous calling for L<Object::Remote>
115 Shipping prioritised over writing this part up. Blame mst.