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 log_trace { my $ir = $f->is_ready; "await_future() invoked; is_ready: $ir" };
24 return $f if $f->is_ready;
25 require Object::Remote;
26 my $loop = Object::Remote->current_loop;
28 local @await = (@await, $f);
30 log_trace { my $l = @await; "future has become ready, length of \@await: '$l'" };
31 if ($f == $await[-1]) {
32 log_debug { "This future is not waiting on anything so calling stop on the run loop" };
36 log_debug { "Starting run loop for newly created future" };
39 if (@await and $await[-1]->is_ready) {
40 log_debug { "Last future in await list was ready, stopping run loop" };
43 log_trace { "await_future() returning" };
44 return wantarray ? $f->get : ($f->get)[0];
48 log_trace { my $l = @_; "await_all() invoked with '$l' futures to wait on" };
49 await_future(CPS::Future->wait_all(@_));
55 our $start = sub { my ($obj, $call) = (shift, shift); $obj->$call(@_); };
59 my ($method) = our $AUTOLOAD =~ /^start::(.+)$/;
61 unless (eval { $res = $invocant->$method(@_); 1 }) {
62 my $f = CPS::Future->new;
66 unless (Scalar::Util::blessed($res) and $res->isa('CPS::Future')) {
67 my $f = CPS::Future->new;
77 my ($obj, $call) = (shift, shift);
78 if ((caller(1)||'') eq 'start') {
79 $obj->$start::start($call => @_);
89 my ($method) = our $AUTOLOAD =~ /^maybe::start::(.+)$/;
90 $method = "start::${method}" if ((caller(1)||'') eq 'start');
91 $invocant->$method(@_);
98 my ($method) = our $AUTOLOAD =~ /^then::(.+)$/;
100 # Need two copies since if we're called on an already complete future
101 # $f will be freed immediately
102 my $ret = my $f = CPS::Future->new;
103 $invocant->on_fail(sub { $f->fail(@_); undef($f); });
104 $invocant->on_done(sub {
106 my $next = $obj->${\"start::${method}"}(@args);
107 $next->on_done(sub { $f->done(@_); undef($f); });
108 $next->on_fail(sub { $f->fail(@_); undef($f); });
117 Object::Remote::Future - Asynchronous calling for L<Object::Remote>
121 Shipping prioritised over writing this part up. Blame mst.