X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=blobdiff_plain;f=lib%2FObject%2FRemote.pm;h=25f0349c017198bdb51ce94f5a06a0e83a63f507;hp=7bb33c673cd1aba3bebc5728c8b028ab57e442a1;hb=dc28afe80b27f0eb81eef66e6ba554a7c1fb6e41;hpb=ad4f54b29c5f84aa010b456303b24647c0e28ae7 diff --git a/lib/Object/Remote.pm b/lib/Object/Remote.pm index 7bb33c6..25f0349 100644 --- a/lib/Object/Remote.pm +++ b/lib/Object/Remote.pm @@ -2,10 +2,28 @@ package Object::Remote; use Object::Remote::MiniLoop; use Object::Remote::Proxy; -use Scalar::Util qw(weaken); +use Scalar::Util qw(weaken blessed); +use Object::Remote::Future; +use Module::Runtime qw(use_module); use Moo; -has connection => (is => 'ro', required => 1); +sub new::on { + my ($class, $on, @args) = @_; + __PACKAGE__->new( + connection => $on, + class => $class, + args => \@args + )->proxy; +} + +has connection => ( + is => 'ro', required => 1, + coerce => sub { + blessed($_[0]) + ? $_[0] + : use_module('Object::Remote::Connection')->new_from_spec($_[0]) + }, +); has id => (is => 'rwp'); @@ -21,10 +39,11 @@ sub BUILD { my ($self, $args) = @_; unless ($self->id) { die "No id supplied and no class either" unless $args->{class}; + ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args}; $self->_set_id( - $self->_await( + await_future( $self->connection->send( - class_call => $args->{class}, + class_call => $args->{class}, 0, $args->{constructor}||'new', @{$args->{args}||[]} ) )->{remote}->disarm_free->id @@ -39,7 +58,10 @@ sub current_loop { sub call { my ($self, $method, @args) = @_; - $self->_await($self->connection->send(call => $self->id, $method, @args)); + my $w = wantarray; + future { + $self->connection->send(call => $self->id, $w, $method, @args) + }; } sub call_discard { @@ -47,12 +69,10 @@ sub call_discard { $self->connection->send_discard(call => $self->id, $method, @args); } -sub _await { - my ($self, $future) = @_; - my $loop = $self->current_loop; - $future->on_ready(sub { $loop->stop }); - $loop->run; - ($future->get)[0]; +sub call_discard_free { + my ($self, $method, @args) = @_; + $self->disarm_free; + $self->connection->send_discard(call_free => $self->id, $method, @args); } sub DEMOLISH {