1 package Object::Remote::Future;
7 use Object::Remote::Logging qw( :log );
11 our @EXPORT = qw(future await_future await_all);
14 my $f = $_[0]->(CPS::Future->new);
15 return $f if ((caller(1+($_[1]||0))||'') eq 'start');
23 return $f if $f->is_ready;
24 require Object::Remote;
25 my $loop = Object::Remote->current_loop;
27 local @await = (@await, $f);
29 $loop->stop if $f == $await[-1]
33 if (@await and $await[-1]->is_ready) {
36 return wantarray ? $f->get : ($f->get)[0];
40 await_future(CPS::Future->wait_all(@_));
46 our $start = sub { my ($obj, $call) = (shift, shift); $obj->$call(@_); };
50 my ($method) = our $AUTOLOAD =~ /^start::(.+)$/;
52 unless (eval { $res = $invocant->$method(@_); 1 }) {
53 my $f = CPS::Future->new;
57 unless (Scalar::Util::blessed($res) and $res->isa('CPS::Future')) {
58 my $f = CPS::Future->new;
68 my ($obj, $call) = (shift, shift);
69 if ((caller(1)||'') eq 'start') {
70 $obj->$start::start($call => @_);
80 my ($method) = our $AUTOLOAD =~ /^maybe::start::(.+)$/;
81 $method = "start::${method}" if ((caller(1)||'') eq 'start');
82 $invocant->$method(@_);
89 my ($method) = our $AUTOLOAD =~ /^then::(.+)$/;
91 # Need two copies since if we're called on an already complete future
92 # $f will be freed immediately
93 my $ret = my $f = CPS::Future->new;
94 $invocant->on_fail(sub { $f->fail(@_); undef($f); });
95 $invocant->on_done(sub {
97 my $next = $obj->${\"start::${method}"}(@args);
98 $next->on_done(sub { $f->done(@_); undef($f); });
99 $next->on_fail(sub { $f->fail(@_); undef($f); });
108 Object::Remote::Future - Asynchronous calling for L<Object::Remote>
112 Shipping prioritised over writing this part up. Blame mst.