From: Matt S Trout Date: Mon, 4 Jun 2012 16:37:26 +0000 (+0000) Subject: future for on_close, clean up pending futures X-Git-Tag: v0.001001~33 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=commitdiff_plain;h=a980b0b8d4033a154d0e76caa63dab7899515639 future for on_close, clean up pending futures --- diff --git a/lib/Object/Remote/Connection.pm b/lib/Object/Remote/Connection.pm index ea1614d..291b554 100644 --- a/lib/Object/Remote/Connection.pm +++ b/lib/Object/Remote/Connection.pm @@ -31,7 +31,7 @@ has receive_from_fh => ( }, ); -has on_close => (is => 'rw', default => sub {}); +has on_close => (is => 'rw', default => sub { CPS::Future->new }); has child_pid => (is => 'ro'); @@ -50,6 +50,8 @@ has local_objects_by_id => (is => 'ro', default => sub { {} }); has remote_objects_by_id => (is => 'ro', default => sub { {} }); +has outstanding_futures => (is => 'ro', default => sub { {} }); + has _json => ( is => 'lazy', handles => { @@ -119,6 +121,10 @@ sub send { unshift @call, $type => my $future = CPS::Future->new; + my $outstanding = $self->outstanding_futures; + $outstanding->{$future} = $future; + $future->on_ready(sub { delete $outstanding->{$future} }); + $self->_send(\@call); return $future; @@ -202,6 +208,14 @@ sub _receive_data_from { } } } else { + Object::Remote->current_loop + ->unwatch_io( + handle => $self->receive_from_fh, + on_read_ready => 1 + ); + my $outstanding = $self->outstanding_futures; + $_->fail("Connection lost") for values %$outstanding; + %$outstanding = (); $self->on_close->done(); } }