Issue 1240 - replication errors

Hi Dale,

I have completed my first patch for pouchdb, to address issues in replication: primarily #1240.

This is, after all, much more change than I had anticipated and I can understand why you might not accept it. There is a rather long, rambling note to one of the commits.

In its favour: it passes all tests, including a new test for issue 1240. It addresses several other issues, including: 1102, 1087, 942, 793 and 380, at least in part. It significantly reduces latency to target updates in some cases (I got started on this adventure trying to understand why replicating a CouchDB database of several thousand records was slow and frequently failing). If anyone can provide a test case that this implementation fails, I would be happy to fix it (subject to the limitations of my ability).

I would appreciate any feedback and guidance to making this more acceptable.

Regards,
Ian Goodacre