Skip to content

Commit

Permalink
(#2735) - always populate the default constructor and add sync wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Sep 5, 2014
1 parent d3d1f2a commit 4a90552
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
10 changes: 5 additions & 5 deletions lib/replicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ function replicate(repId, src, target, opts, returnValue) {
}
}


exports.toPouch = toPouch;
function toPouch(db, opts) {
var PouchConstructor = opts.PouchConstructor;
if (typeof db === 'string') {
Expand All @@ -563,7 +563,8 @@ function toPouch(db, opts) {
}


exports.replicate = function replicateWrapper(src, target, opts, callback) {
exports.replicate = replicateWrapper;
function replicateWrapper(src, target, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
Expand All @@ -576,6 +577,7 @@ exports.replicate = function replicateWrapper(src, target, opts, callback) {
}
opts = utils.clone(opts);
opts.continuous = opts.continuous || opts.live;
/*jshint validthis:true */
opts.PouchConstructor = opts.PouchConstructor || this;
var replicateRet = new Replication(opts);
toPouch(src, opts).then(function (src) {
Expand All @@ -589,6 +591,4 @@ exports.replicate = function replicateWrapper(src, target, opts, callback) {
opts.complete(err);
});
return replicateRet;
};

exports.toPouch = toPouch;
}
26 changes: 15 additions & 11 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ var replication = require('./replicate');
var replicate = replication.replicate;
var EE = require('events').EventEmitter;

module.exports = Sync;
utils.inherits(Sync, EE);
function Sync(src, target, opts, callback) {
if (!(this instanceof Sync)) {
return new Sync(src, target, opts, callback);
}
var self = this;
module.exports = sync;
function sync(src, target, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
if (typeof opts === 'undefined') {
opts = {};
}
this.canceled = false;
opts = utils.clone(opts);
/*jshint validthis:true */
opts.PouchConstructor = opts.PouchConstructor || this;
src = replication.toPouch(src, opts);
target = replication.toPouch(target, opts);
return new Sync(src, target, opts, callback);
}
function Sync(src, target, opts, callback) {
var self = this;
this.canceled = false;

var onChange, complete;
if ('onChange' in opts) {
onChange = opts.onChange;
Expand All @@ -31,11 +36,10 @@ function Sync(src, target, opts, callback) {
complete = opts.complete;
delete opts.complete;
}
var srcPouch = replication.toPouch(src, opts);
var targPouch = replication.toPouch(target, opts);
this.push = replicate(srcPouch, targPouch, opts);

this.pull = replicate(targPouch, srcPouch, opts);
this.push = replicate(src, target, opts);

this.pull = replicate(target, src, opts);
var emittedCancel = false;
function onCancel(data) {
if (!emittedCancel) {
Expand Down
23 changes: 23 additions & 0 deletions tests/test.sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,28 @@ adapters.forEach(function (adapters) {
});
});
});
it('PouchDB.sync with strings for dbs', function (done) {
var doc1 = {
_id: 'adoc',
foo: 'bar'
};
var doc2 = {
_id: 'anotherdoc',
foo: 'baz'
};
var db = new PouchDB(dbs.name);
var remote = new PouchDB(dbs.remote);
db.put(doc1).then(function () {
return remote.put(doc2);
}).then(function () {
return PouchDB.sync(dbs.name, dbs.remote);
}).then(function (result) {
result.pull.ok.should.equal(true);
result.pull.docs_read.should.equal(1);
result.pull.docs_written.should.equal(1);
result.pull.errors.should.have.length(0);
done();
}, done);
});
});
});

0 comments on commit 4a90552

Please sign in to comment.