Skip to content

Commit

Permalink
Wait until we have dedupped the changes feed before emitting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
daleharvey committed Apr 23, 2012
1 parent d3495c0 commit b5d0bf7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/adapters/pouch.idb.js
Expand Up @@ -172,6 +172,9 @@ var IdbPouch = function(opts, callback) {

results.forEach(function(result) {
delete result._bulk_seq;
if (/_local/.test(result.id)) {
return;
}
api.changes.emit(result);
});
call(callback, null, results);
Expand Down Expand Up @@ -540,12 +543,18 @@ var IdbPouch = function(opts, callback) {
if (opts.continuous) {
api.changes.addListener(opts.onChange);
}
results.map(function(c) {
call(opts.onChange, c);
});
return call(opts.complete, null, {results: results});
}
var cursor = event.target.result;
var index = transaction.objectStore(DOC_STORE);
index.get(cursor.value._id).onsuccess = function(event) {
var doc = event.target.result;
if (/_local/.test(doc.id)) {
return cursor['continue']();
}
var c = {
id: doc.id,
seq: cursor.key,
Expand All @@ -566,7 +575,6 @@ var IdbPouch = function(opts, callback) {
return doc.id !== c.id;
});
results.push(c);
call(opts.onChange, c);
cursor['continue']();
};
};
Expand Down
26 changes: 26 additions & 0 deletions tests/test.replication.js
Expand Up @@ -114,6 +114,32 @@ asyncTest("Test checkpoint 2", function() {
});
});

asyncTest("Test checkpoint 3 :)", function() {
var self = this;
var doc = {_id: "3", count: 0};
initDBPair(this.name, this.remote, function(db, remote) {
db.put(doc, {}, function(err, results) {
Pouch.replicate(db, remote, function(err, result) {
ok(result.ok, 'replication was ok');
doc._rev = results.rev;
doc.count++;
db.put(doc, {}, function(err, results) {
doc._rev = results.rev;
doc.count++;
db.put(doc, {}, function(err, results) {
Pouch.replicate(db, remote, function(err, result) {
ok(result.ok, 'replication was ok');
ok(result.docs_written === 1, 'correct # docs written');
start();
});
});
});
});
});
});
});


// CouchDB will not generate a conflict here, it uses a deteministic
// method to generate the revision number, however we cannot copy its
// method as it depends on erlangs internal data representation
Expand Down

0 comments on commit b5d0bf7

Please sign in to comment.