AnyEvent-CouchDB

 view release on metacpan or  search on metacpan

lib/AnyEvent/CouchDB/Stream.pm  view on Meta::CPAN

                if ($handle) {
                    $handle->on_error(
                        sub {
                            undef $handle;
                            $on_error->( $_[2] );
                        }
                    );
                    $handle->on_eof(
                        sub {
                            undef $handle;
                            $on_eof->(@_);
                        }
                    );
                    my $reader;
                    $reader = sub {
                        my ( $handle, $json ) = @_;
                        $set_timeout->();
                        if ($json) {
                            try {
                              $on_change->(JSON::decode_json($json));
                            } 
                            catch {
                              # I'm not sure why, but sometimes
                              # some weird characters show up
                              # in between the JSON objects.
                            };
                        }
                        else {
                            $on_keepalive->();
                        }
                        $handle->push_read( line => $reader );
                    };
                    $handle->push_read( line => $reader );
                    $self->{guard} = AnyEvent::Util::guard {
                        $on_eof->();
                        $handle->destroy if $handle;
                        undef $reader;
                    };
                }
           }
        );
    }
    $self;
}

1;
__END__

=head1 NAME

AnyEvent::CouchDB::Stream - Watch changes from a CouchDB database.

=head1 SYNOPSIS

  use AnyEvent::CouchDB::Stream;
  my $listener = AnyEvent::CouchDB::Stream->new(
      url       => 'http://localhost:5984',
      database  => 'test',
      on_change => sub {
          my $change = shift;
          warn "document $change->{_id} updated";
      },
      on_keepalive => sub {
          warn "ping\n";
      },
      timeout => 1,
  );

=head1 DESCRIPTION

AnyEvent::CouchDB::Stream is an interface to the CouchDB B<changes> database API.

=head2 OPTIONS

=over 4

=item B<url>

URL of the CouchDB host.

=item B<database>

Name of the CouchDB database.

=item B<timeout>

Number of seconds to wait before timing out.  On timeout, The on_error 
code ref will be called with an argument of 'timeout'.

=item B<filter>

Name of the filter to execute on this notifier.

=item B<since>

Number to fetch changes from. Defaults to 1.

=item B<on_change>

A code ref to execute when a change notification is received. It is mandatory.

=item B<on_keepalive>

A code ref to execute when keepalive is called.

=item B<on_error>

A code ref to execute on error. Code ref is passed the error message.

=item B<on_eof>

A code ref to execute on eof

=item B<headers>

An optional hashref of headers that should be used for the HTTP request.
Defaults to C< { 'Content-Type' => 'application/json' } >.

=item B<heartbeat>

The interval in milliseconds between newlines sent from the server to ensure that an open connection is still being maintained



( run in 1.880 second using v1.01-cache-2.11-cpan-f56aa216473 )