Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checkpoint events during replication #8324

Merged
merged 3 commits into from Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Expand Up @@ -3,7 +3,7 @@
"extends": "eslint:recommended",

"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 8,
"sourceType": "module"
},

Expand Down
4 changes: 4 additions & 0 deletions packages/node_modules/pouchdb-replication/src/replicate.js
Expand Up @@ -132,6 +132,7 @@ function replicate(src, target, opts, returnValue, result) {
writingCheckpoint = true;
return checkpointer.writeCheckpoint(currentBatch.seq,
session).then(function () {
returnValue.emit('checkpoint', { 'checkpoint': currentBatch.seq });
writingCheckpoint = false;
/* istanbul ignore if */
if (returnValue.cancelled) {
Expand All @@ -149,6 +150,7 @@ function replicate(src, target, opts, returnValue, result) {
function getDiffs() {
var diff = {};
currentBatch.changes.forEach(function (change) {
returnValue.emit('checkpoint', { 'revs_diff': change });
// Couchbase Sync Gateway emits these, but we can ignore them
/* istanbul ignore if */
if (change.id === "_user/") {
Expand Down Expand Up @@ -189,6 +191,7 @@ function replicate(src, target, opts, returnValue, result) {
return;
}
currentBatch = batches.shift();
returnValue.emit('checkpoint', { 'start_next_batch': currentBatch.seq });
getDiffs()
.then(getBatchDocs)
.then(writeDocs)
Expand Down Expand Up @@ -307,6 +310,7 @@ function replicate(src, target, opts, returnValue, result) {
}
pendingBatch.seq = change.seq || lastSeq;
pendingBatch.changes.push(change);
returnValue.emit('checkpoint', { 'pending_batch': pendingBatch.seq });
nextTick(function () {
processPendingBatch(batches.length === 0 && changesOpts.live);
});
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/test.replication_events.js
Expand Up @@ -400,5 +400,37 @@ adapters.forEach(function (adapters) {
// #5607 https://github.com/pouchdb/pouchdb/issues/5607#issuecomment-346078688
// Note: Uppercase 'Unauthorized' response
describe('#5607 handle uppercase third-party error responses', replication401TestFunction('Unauthorized'));

it('Test checkpoint events', async () => {
const docs = {
docs: [
{_id: '0', integer: 0, string: '0'},
{_id: '1', integer: 1, string: '1'},
{_id: '2', integer: 2, string: '2'},
],
};
const remote = new PouchDB(dbs.remote);
const checkpointEvents = [];

await remote.bulkDocs(docs, {});
const replication = PouchDB.replicate(dbs.remote, dbs.name, {});
replication.on('checkpoint', result => checkpointEvents.push(result));
await replication;

checkpointEvents[0]['pending_batch'].should.equal(1);
checkpointEvents[1]['pending_batch'].should.equal(2);
checkpointEvents[2]['pending_batch'].should.equal(3);
checkpointEvents[3]['start_next_batch'].should.equal(3);
checkpointEvents[4]['revs_diff'].should.have.property('id');
checkpointEvents[4]['revs_diff']['id'].should.equal('0');
checkpointEvents[4]['revs_diff']['seq'].should.equal(1);
checkpointEvents[5]['revs_diff'].should.have.property('changes');
checkpointEvents[5]['revs_diff']['id'].should.equal('1');
checkpointEvents[5]['revs_diff']['seq'].should.equal(2);
checkpointEvents[6]['revs_diff'].should.have.property('changes');
checkpointEvents[6]['revs_diff']['id'].should.equal('2');
checkpointEvents[6]['revs_diff']['seq'].should.equal(3);
checkpointEvents[7]['checkpoint'].should.equal(3);
});
});
});