Skip to content

Commit

Permalink
Fix Streamable's error reporting, and make Stream fire events by defa…
Browse files Browse the repository at this point in the history
…ult.
  • Loading branch information
rcaputo committed May 11, 2010
1 parent c5e0e08 commit 7a303d3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
14 changes: 13 additions & 1 deletion eg/roles/Stream.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@ has handle => ( is => 'rw', isa => 'FileHandle', required => 1 );

with 'Streamable' => { handle => 'handle' };

# TODO - Would be nice to alias put_handle() to put().
sub put {
my $self = shift;
$self->put_handle(@_);
}

# Default callback emits an event.
# TODO - Common convention. How to make this generic?
sub on_handle_data {
my ($self, $args) = @_;
warn $args->{data};
$self->emit(
event => "data",
args => $args
);
}

# Default callback emits an event.
# TODO - Common convention. How to make this generic?
sub on_handle_error {
my ($self, $args) = @_;
$self->emit(
event => "data",
args => $args
Expand Down
32 changes: 28 additions & 4 deletions eg/roles/Streamable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@ parameter cb_data => (
lazy => 1,
);

parameter cb_error => (
isa => 'Str',
default => sub {
my $self = shift;
"on_" . $self->handle() . "_error";
},
lazy => 1,
);

role {
my $p = shift;

my $h = $p->handle();
my $cb_data = $p->cb_data();
my $h = $p->handle();
my $cb_data = $p->cb_data();
my $cb_error = $p->cb_error();

with Readable => {
handle => $h,
Expand All @@ -48,8 +58,14 @@ role {
}

return if defined $octet_count;
warn $!;
# TODO - Error callback.

$self->cb_error(
{
errnum => ($! + 0),
errstr => "$!",
errfun => "sysread",
}
);
};

method "put_$h" => sub {
Expand All @@ -70,6 +86,14 @@ role {

# Hard error.
unless (defined $octet_count) {
$self->$cb_error(
{
errnum => ($! + 0),
errstr => "$!",
errfun => "syswrite",
}
);

$self->_emit_failure("syswrite");
return;
}
Expand Down

0 comments on commit 7a303d3

Please sign in to comment.