Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
replaced remaining write callbacks with drain events
  • Loading branch information
kraih committed Jan 29, 2012
1 parent ea7dbef commit 79746fa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.47 2012-01-28 00:00:00
2.47 2012-01-29 00:00:00
- Added EXPERIMENTAL drain event to Mojo::Content.
- Added EXPERIMENTAL drain event to Mojo::Transaction::WebSocket.
- Added pdf MIME type. (bfaist)
- Improved documentation.
- Improved tests.
Expand Down
23 changes: 18 additions & 5 deletions lib/Mojo/Content.pm
Expand Up @@ -86,10 +86,8 @@ sub generate_body_chunk {
my ($self, $offset) = @_;

# Drain
if (!delete $self->{delay} && !length $self->{body_buffer}) {
my $cb = delete $self->{drain};
$self->$cb($offset) if $cb;
}
$self->emit(drain => $offset)
if !delete $self->{delay} && !length $self->{body_buffer};

# Get chunk
my $chunk = $self->{body_buffer} // '';
Expand Down Expand Up @@ -269,7 +267,7 @@ sub write {
else { $self->{delay} = 1 }

# Drain
$self->{drain} = $cb if $cb;
$self->once(drain => $cb) if $cb;

# Finish
$self->{eof} = 1 if defined $chunk && $chunk eq '';
Expand Down Expand Up @@ -418,6 +416,21 @@ in RFC 2616.
L<Mojo::Content> can emit the following events.
=head2 C<drain>
$content->on(drain => sub {
my ($content, $offset) = @_;
...
});
Emitted once all data has been written. Note that this event is EXPERIMENTAL
and might change without warning!
$content->on(drain => sub {
my $content = shift;
$content->write_chunk(time);
});
=head2 C<body>
$content->on(body => sub {
Expand Down
19 changes: 16 additions & 3 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -216,7 +216,7 @@ sub send_frame {
my ($self, $fin, $type, $payload, $cb) = @_;

# Prepare frame
$self->{drain} = $cb if $cb;
$self->once(drain => $cb) if $cb;
$self->{write} //= '';
$self->{write} .= $self->build_frame($fin, $type, $payload);
$self->{state} = 'write';
Expand Down Expand Up @@ -303,8 +303,7 @@ sub server_write {
$self->{write} //= '';
unless (length $self->{write}) {
$self->{state} = $self->{finished} ? 'finished' : 'read';
my $cb = delete $self->{drain};
$self->$cb if $cb;
$self->emit('drain');
}

# Empty buffer
Expand Down Expand Up @@ -352,6 +351,20 @@ without warning!
L<Mojo::Transaction::WebSocket> inherits all events from L<Mojo::Transaction>
and can emit the following new ones.
=head2 C<drain>
$ws->on(drain => sub {
my $ws = shift;
...
});
Emitted once all data has been sent.
$ws->on(drain => sub {
my $ws = shift;
$ws->send_message(time);
});
=head2 C<frame>
$ws->on(frame => sub {
Expand Down

0 comments on commit 79746fa

Please sign in to comment.