Alice

 view release on metacpan or  search on metacpan

lib/Alice/HTTP/Stream/XHR.pm  view on Meta::CPAN

package Alice::HTTP::Stream::XHR;

use JSON;
use Time::HiRes qw/time/;
use Any::Moose;

extends 'Alice::HTTP::Stream';

use strict;
use warnings;

my $separator = "xalicex";
our @headers = ('Content-Type' => "multipart/mixed; boundary=$separator; charset=utf-8");

has queue => (
  is  => 'rw',
  isa => 'ArrayRef[HashRef]',
  default => sub { [] },
);

sub clear_queue {$_[0]->queue([])}
sub enqueue {push @{shift->queue}, @_}
sub queue_empty {return @{$_[0]->queue} == 0}

has [qw/delayed started/] => (
  is  => 'rw',
  isa => 'Bool',
  default => 0,
);

has [qw/offset last_send start_time/]=> (
  is  => 'rw',
  isa => 'Num',
  default => 0,
);

has 'timer' => (
  is  => 'rw',
);

has 'writer' => (
  is  => 'rw',
  required => 1,
);

has min_bytes => (
  is => 'ro',
  default => 1024,
);

sub BUILD {
  my $self = shift;

  my $local_time = time;
  my $remote_time = $self->start_time || $local_time;
  $self->offset($local_time - $remote_time);

  # better way to get the AE handle?
  my $hdl = $self->writer->{handle};

  $hdl->{rbuf_max} = 1024 * 10;

  my $close = sub {
    $self->close;
    undef $hdl;
    $self->on_error->();
  };

  $hdl->on_eof($close);
  $hdl->on_error($close);

  $self->send([{type => "identify", id => $self->id}]);
}



( run in 0.929 second using v1.01-cache-2.11-cpan-5a3173703d6 )