Skip to content

Commit

Permalink
pouchdb#6230 Don't pollute onChange handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dharders committed Nov 21, 2017
1 parent 5922ec4 commit 21450b3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
13 changes: 5 additions & 8 deletions packages/node_modules/pouchdb-adapter-http/src/index.js
Expand Up @@ -956,11 +956,15 @@ function HttpPouch(opts, callback) {
raw_results_length = res.results.length;
results.last_seq = res.last_seq;
var pending = null;
var lastSeq = null;
// Attach 'pending' property if server supports it (CouchDB 2.0+)
/* istanbul ignore if */
if (typeof res.pending === 'number') {
pending = res.pending;
}
if (typeof results.last_seq === 'string' || typeof results.last_seq === 'number') {
lastSeq = results.last_seq;
}
// For each change
var req = {};
req.query = opts.query_params;
Expand All @@ -974,14 +978,7 @@ function HttpPouch(opts, callback) {
if (returnDocs) {
results.results.push(c);
}
/* istanbul ignore if */
if (typeof pending === 'number') {
c.pending = pending;
}
if (typeof results.last_seq === 'string' || typeof results.last_seq === 'number') {
c.last_seq = results.last_seq;
}
opts.onChange(c);
opts.onChange(c, pending, lastSeq);
}
return ret;
});
Expand Down
8 changes: 4 additions & 4 deletions packages/node_modules/pouchdb-core/src/changes.js
Expand Up @@ -18,10 +18,10 @@ import PouchDB from './setup';

inherits(Changes, EE);

function tryCatchInChangeListener(self, change) {
function tryCatchInChangeListener(self, change, pending, lastSeq) {
// isolate try/catches to avoid V8 deoptimizations
try {
self.emit('change', change);
self.emit('change', change, pending, lastSeq);
} catch (e) {
guardedConsole('error', 'Error in .on("change", function):', e);
}
Expand Down Expand Up @@ -54,12 +54,12 @@ function Changes(db, opts, callback) {
}
db.once('destroyed', onDestroy);

opts.onChange = function (change) {
opts.onChange = function (change, pending, lastSeq) {
/* istanbul ignore if */
if (self.isCancelled) {
return;
}
tryCatchInChangeListener(self, change);
tryCatchInChangeListener(self, change, pending, lastSeq);
};

var promise = new Promise(function (fulfill, reject) {
Expand Down
7 changes: 1 addition & 6 deletions packages/node_modules/pouchdb-replication/src/replicate.js
Expand Up @@ -287,22 +287,17 @@ function replicate(src, target, opts, returnValue, result) {
}


function onChange(change) {
function onChange(change, pending, lastSeq) {
/* istanbul ignore if */
if (returnValue.cancelled) {
return completeReplication();
}
// Attach 'pending' property if server supports it (CouchDB 2.0+)
var pending = change.pending;
delete change.pending;
/* istanbul ignore if */
if (typeof pending === 'number') {
pendingBatch.pending = pending;
}

var lastSeq = change.last_seq;
delete change.last_seq;

var filter = filterChange(opts)(change);
if (!filter) {
return;
Expand Down

0 comments on commit 21450b3

Please sign in to comment.