fixup start, add then::
[scpubgit/Object-Remote.git] / lib / Object / Remote / Future.pm
index c5ac7ee..04bc251 100644 (file)
@@ -34,9 +34,6 @@ package start;
 sub AUTOLOAD {
   my $invocant = shift;
   my ($method) = our $AUTOLOAD =~ /^start::(.+)$/;
-  if (ref($invocant) eq 'ARRAY') {
-    return [ map $_->${\"start::${method}"}, @$invocant ];
-  }
   my $res;
   unless (eval { $res = $invocant->$method(@_); 1 }) {
     my $f = CPS::Future->new;
@@ -51,6 +48,25 @@ sub AUTOLOAD {
   return $res;
 }
 
+package then;
+
+sub AUTOLOAD {
+  my $invocant = shift;
+  my ($method) = our $AUTOLOAD =~ /^then::(.+)$/;
+  my @args = @_;
+  # Need two copies since if we're called on an already complete future
+  # $f will be freed immediately
+  my $ret = my $f = CPS::Future->new;
+  $invocant->on_fail(sub { $f->fail(@_); undef($f); });
+  $invocant->on_done(sub {
+    my ($obj) = @_;
+    my $next = $obj->${\"start::${method}"}(@args);
+    $next->on_done(sub { $f->done(@_); undef($f); });
+    $next->on_fail(sub { $f->fail(@_); undef($f); });
+  });
+  return $ret;
+}
+
 1;
 
 =head1 NAME