1 package Object::Remote::Future;
7 use Object::Remote::Logging qw( :log router );
9 BEGIN { router()->exclude_forwarding }
13 our @EXPORT = qw(future await_future await_all);
16 my $f = $_[0]->(CPS::Future->new);
17 return $f if ((caller(1+($_[1]||0))||'') eq 'start');
25 log_trace { my $ir = $f->is_ready; "await_future() invoked; is_ready: $ir" };
26 return $f if $f->is_ready;
27 require Object::Remote;
28 my $loop = Object::Remote->current_loop;
30 local @await = (@await, $f);
32 log_trace { my $l = @await; "future has become ready, length of \@await: '$l'" };
33 if ($f == $await[-1]) {
34 log_trace { "This future is not waiting on anything so calling stop on the run loop" };
38 log_trace { "Starting run loop for newly created future" };
41 if (@await and $await[-1]->is_ready) {
42 log_trace { "Last future in await list was ready, stopping run loop" };
45 log_trace { "await_future() returning" };
46 return wantarray ? $f->get : ($f->get)[0];
50 log_trace { my $l = @_; "await_all() invoked with '$l' futures to wait on" };
51 await_future(CPS::Future->wait_all(@_));
57 our $start = sub { my ($obj, $call) = (shift, shift); $obj->$call(@_); };
61 my ($method) = our $AUTOLOAD =~ /^start::(.+)$/;
63 unless (eval { $res = $invocant->$method(@_); 1 }) {
64 my $f = CPS::Future->new;
68 unless (Scalar::Util::blessed($res) and $res->isa('CPS::Future')) {
69 my $f = CPS::Future->new;
79 my ($obj, $call) = (shift, shift);
80 if ((caller(1)||'') eq 'start') {
81 $obj->$start::start($call => @_);
91 my ($method) = our $AUTOLOAD =~ /^maybe::start::(.+)$/;
92 $method = "start::${method}" if ((caller(1)||'') eq 'start');
93 $invocant->$method(@_);
100 my ($method) = our $AUTOLOAD =~ /^then::(.+)$/;
102 # Need two copies since if we're called on an already complete future
103 # $f will be freed immediately
104 my $ret = my $f = CPS::Future->new;
105 $invocant->on_fail(sub { $f->fail(@_); undef($f); });
106 $invocant->on_done(sub {
108 my $next = $obj->${\"start::${method}"}(@args);
109 $next->on_done(sub { $f->done(@_); undef($f); });
110 $next->on_fail(sub { $f->fail(@_); undef($f); });
119 Object::Remote::Future - Asynchronous calling for L<Object::Remote>
123 Shipping prioritised over writing this part up. Blame mst.