Skip to content

Commit

Permalink
fixed small initialization bug in Mojo::IOLoop::Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 7, 2012
1 parent f00d57a commit 37b56f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

3.05 2012-07-08
- Improved documentation.
- Fixed small initialization bug in Mojo::IOLoop::Stream.

3.04 2012-07-07
- Improved Mojo::IOLoop performance by reducing stream timeout precision
Expand Down
37 changes: 19 additions & 18 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -49,25 +49,9 @@ sub is_writing {

sub start {
my $self = shift;

# Timeout
my $reactor = $self->reactor;
weaken $self;
$self->{timer} ||= $reactor->recurring(
0.5 => sub {
return unless $self && (my $t = $self->timeout);
$self->emit_safe('timeout')->close if (time - $self->{active}) >= $t;
}
);

# Start streaming
my $handle = $self->{handle};
return $reactor->io($handle => sub { pop() ? $self->_write : $self->_read })
unless $self->{streaming}++;

# Resume streaming
return $self->_startup unless $self->{startup}++;
return unless delete $self->{paused};
$reactor->watch($handle, 1, $self->is_writing);
$self->reactor->watch($self->{handle}, 1, $self->is_writing);
}

sub stop {
Expand Down Expand Up @@ -127,6 +111,23 @@ sub _read {
$self->{active} = time;
}

sub _startup {
my $self = shift;

# Timeout (ignore 0 timeout)
my $reactor = $self->reactor;
weaken $self;
$self->{timer} = $reactor->recurring(
0.5 => sub {
return unless my $t = $self->timeout;
$self->emit_safe('timeout')->close if (time - $self->{active}) >= $t;
}
);

# Start streaming
$reactor->io($self->{handle}, sub { pop() ? $self->_write : $self->_read });
}

# "Oh, I'm in no condition to drive. Wait a minute.
# I don't have to listen to myself. I'm drunk."
sub _write {
Expand Down

0 comments on commit 37b56f4

Please sign in to comment.