X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FObject%2FRemote%2FFuture.pm;h=893fb6d663579dcb8ddaeffc326494a295958af0;hb=f7ea4120db80a6feb33053e6bcb0f983b71f7394;hp=04bc251a9c3cd10a0f8c92f1c344e5709293ce8f;hpb=11e7c8a508b90595385fe78e9036f8f8313d9dbd;p=scpubgit%2FObject-Remote.git diff --git a/lib/Object/Remote/Future.pm b/lib/Object/Remote/Future.pm index 04bc251..893fb6d 100644 --- a/lib/Object/Remote/Future.pm +++ b/lib/Object/Remote/Future.pm @@ -8,29 +8,41 @@ use CPS::Future; our @EXPORT = qw(future await_future await_all); -sub future (&) { +sub future (&;$) { my $f = $_[0]->(CPS::Future->new); - return $f if ((caller(1)||'') eq 'start'); + return $f if ((caller(1+($_[1]||0))||'') eq 'start'); await_future($f); } +our @await; + sub await_future { my $f = shift; return $f if $f->is_ready; require Object::Remote; my $loop = Object::Remote->current_loop; - $f->on_ready(sub { $loop->stop }); - $loop->run; + { + local @await = (@await, $f); + $f->on_ready(sub { + $loop->stop if $f == $await[-1] + }); + $loop->run; + } + if (@await and $await[-1]->is_ready) { + $loop->stop; + } return wantarray ? $f->get : ($f->get)[0]; } sub await_all { - await_future(CPS::Future->needs_all(@_)); + await_future(CPS::Future->wait_all(@_)); map $_->get, @_; } package start; +our $start = sub { my ($obj, $call) = (shift, shift); $obj->$call(@_); }; + sub AUTOLOAD { my $invocant = shift; my ($method) = our $AUTOLOAD =~ /^start::(.+)$/; @@ -48,6 +60,26 @@ sub AUTOLOAD { return $res; } +package maybe; + +sub start { + my ($obj, $call) = (shift, shift); + if ((caller(1)||'') eq 'start') { + $obj->$start::start($call => @_); + } else { + $obj->$call(@_); + } +} + +package maybe::start; + +sub AUTOLOAD { + my $invocant = shift; + my ($method) = our $AUTOLOAD =~ /^maybe::start::(.+)$/; + $method = "start::${method}" if ((caller(1)||'') eq 'start'); + $invocant->$method(@_); +} + package then; sub AUTOLOAD {