Skip to content

Commit

Permalink
(#848) - add promises
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf authored and nolanlawson committed Jan 15, 2014
1 parent f3e1e00 commit c8dbcb8
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 64 deletions.
52 changes: 26 additions & 26 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module.exports = function (Pouch) {
};
}

api.post = function (doc, opts, callback) {
api.post = utils.toPromise(function (doc, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
Expand All @@ -129,9 +129,9 @@ module.exports = function (Pouch) {
}
return customApi.bulkDocs({docs: [doc]}, opts,
autoCompact(yankError(callback)));
};
});

api.put = function (doc, opts, callback) {
api.put = utils.toPromise(function (doc, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
Expand All @@ -144,9 +144,9 @@ module.exports = function (Pouch) {
}
return customApi.bulkDocs({docs: [doc]}, opts,
autoCompact(yankError(callback)));
};
});

api.putAttachment = function (docId, attachmentId, rev, blob, type, callback) {
api.putAttachment = utils.toPromise(function (docId, attachmentId, rev, blob, type, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('putAttachment', arguments);
return;
Expand Down Expand Up @@ -190,9 +190,9 @@ module.exports = function (Pouch) {

createAttachment(doc);
});
};
});

api.removeAttachment = function (docId, attachmentId, rev, callback) {
api.removeAttachment = utils.toPromise(function (docId, attachmentId, rev, callback) {
api.get(docId, function (err, obj) {
if (err) {
call(callback, err);
Expand All @@ -211,9 +211,9 @@ module.exports = function (Pouch) {
}
api.put(obj, callback);
});
};
});

api.remove = function (doc, opts, callback) {
api.remove = utils.toPromise(function (doc, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
Expand All @@ -225,9 +225,9 @@ module.exports = function (Pouch) {
var newDoc = {_id: doc._id, _rev: doc._rev};
newDoc._deleted = true;
return customApi.bulkDocs({docs: [newDoc]}, opts, yankError(callback));
};
});

api.revsDiff = function (req, opts, callback) {
api.revsDiff = utils.toPromise(function (req, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
Expand Down Expand Up @@ -282,7 +282,7 @@ module.exports = function (Pouch) {
}
});
});
};
});

// compact one document and fire callback
// by compacting we mean removing all revisions which
Expand Down Expand Up @@ -314,7 +314,7 @@ module.exports = function (Pouch) {

// compact the whole database using single document
// compaction
api.compact = function (opts, callback) {
api.compact = utils.toPromise(function (opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
Expand All @@ -338,10 +338,10 @@ module.exports = function (Pouch) {
});
});
}});
};
});

/* Begin api wrappers. Specific functionality to storage belongs in the _[method] */
api.get = function (id, opts, callback) {
api.get = utils.toPromise(function (id, opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('get', arguments);
return;
Expand Down Expand Up @@ -481,9 +481,9 @@ module.exports = function (Pouch) {
call(callback, null, doc);
}
});
};
});

api.getAttachment = function (docId, attachmentId, opts, callback) {
api.getAttachment = utils.toPromise(function (docId, attachmentId, opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('getAttachment', arguments);
return;
Expand All @@ -503,9 +503,9 @@ module.exports = function (Pouch) {
return call(callback, errors.MISSING_DOC);
}
});
};
});

api.allDocs = function (opts, callback) {
api.allDocs = utils.toPromise(function (opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('allDocs', arguments);
return;
Expand Down Expand Up @@ -533,7 +533,7 @@ module.exports = function (Pouch) {
}

return customApi._allDocs(opts, callback);
};
});

function processChange(doc, metadata, opts) {
var changeList = [{rev: doc._rev}];
Expand Down Expand Up @@ -678,21 +678,21 @@ module.exports = function (Pouch) {
return customApi._changes(opts);
};

api.close = function (callback) {
api.close = utils.toPromise(function (callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('close', arguments);
return;
}
return customApi._close(callback);
};
});

api.info = function (callback) {
api.info = utils.toPromise(function (callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('info', arguments);
return;
}
return customApi._info(callback);
};
});

api.id = function () {
return customApi._id();
Expand All @@ -702,7 +702,7 @@ module.exports = function (Pouch) {
return (typeof customApi._type === 'function') ? customApi._type() : opts.adapter;
};

api.bulkDocs = function (req, opts, callback) {
api.bulkDocs = utils.toPromise(function (req, opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('bulkDocs', arguments);
return;
Expand Down Expand Up @@ -737,7 +737,7 @@ module.exports = function (Pouch) {
}

return customApi._bulkDocs(req, opts, autoCompact(callback));
};
});

/* End Wrappers */
var taskqueue = {};
Expand Down
60 changes: 30 additions & 30 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,19 @@ function HttpPouch(opts, callback) {
return genDBUrl(host, '');
};

api.request = function (options, callback) {
api.request = utils.toPromise(function (options, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('request', arguments);
return;
}
options.headers = host.headers;
options.url = genDBUrl(host, options.url);
ajax(options, callback);
};
});

// Sends a POST request to the host calling the couchdb _compact function
// version: The version of CouchDB it is running
api.compact = function (opts, callback) {
api.compact = utils.toPromise(function (opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('compact', arguments);
return;
Expand Down Expand Up @@ -261,12 +261,12 @@ function HttpPouch(opts, callback) {
ping();
}
});
};
});

// Calls GET on the host, which gets back a JSON string containing
// couchdb: A welcome string
// version: The version of CouchDB it is running
api.info = function (callback) {
api.info = utils.toPromise(function (callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('info', arguments);
return;
Expand All @@ -276,12 +276,12 @@ function HttpPouch(opts, callback) {
method: 'GET',
url: genDBUrl(host, '')
}, callback);
};
});

// Get the document with the given id from the database given by host.
// The id could be solely the _id in the database, or it may be a
// _design/ID or _local/ID path
api.get = function (id, opts, callback) {
api.get = utils.toPromise(function (id, opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('get', arguments);
return;
Expand Down Expand Up @@ -387,10 +387,10 @@ function HttpPouch(opts, callback) {
// Send the document to the callback
utils.call(callback, null, doc, xhr);
});
};
});

// Delete the document given by doc from the database given by host.
api.remove = function (doc, opts, callback) {
api.remove = utils.toPromise(function (doc, opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('remove', arguments);
return;
Expand All @@ -407,10 +407,10 @@ function HttpPouch(opts, callback) {
method: 'DELETE',
url: genDBUrl(host, encodeDocId(doc._id)) + '?rev=' + doc._rev
}, callback);
};
});

// Get the attachment
api.getAttachment = function (docId, attachmentId, opts, callback) {
api.getAttachment = utils.toPromise(function (docId, attachmentId, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
Expand All @@ -423,10 +423,10 @@ function HttpPouch(opts, callback) {
}
opts.auto_encode = false;
api.get(docId + '/' + attachmentId, opts, callback);
};
});

// Remove the attachment given by the id and rev
api.removeAttachment = function (docId, attachmentId, rev, callback) {
api.removeAttachment = utils.toPromise(function (docId, attachmentId, rev, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('removeAttachment', arguments);
return;
Expand All @@ -436,12 +436,12 @@ function HttpPouch(opts, callback) {
method: 'DELETE',
url: genDBUrl(host, encodeDocId(docId) + '/' + attachmentId) + '?rev=' + rev
}, callback);
};
});

// Add the attachment given by blob and its contentType property
// to the document with the given id, the revision given by rev, and
// add it to the database given by host.
api.putAttachment = function (docId, attachmentId, rev, blob, type, callback) {
api.putAttachment = utils.toPromise(function (docId, attachmentId, rev, blob, type, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('putAttachment', arguments);
return;
Expand Down Expand Up @@ -474,11 +474,11 @@ function HttpPouch(opts, callback) {
opts.headers['Content-Type'] = type;
// Add the attachment
ajax(opts, callback);
};
});

// Add the document given by doc (in JSON string format) to the database
// given by host. This fails if the doc has no _id field.
api.put = function (doc, opts, callback) {
api.put = utils.toPromise(function (doc, opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('put', arguments);
return;
Expand Down Expand Up @@ -518,12 +518,12 @@ function HttpPouch(opts, callback) {
url: genDBUrl(host, encodeDocId(doc._id)) + params,
body: doc
}, callback);
};
});

// Add the document given by doc (in JSON string format) to the database
// given by host. This does not assume that doc is a new document (i.e. does not
// have a _id or a _rev field.
api.post = function (doc, opts, callback) {
api.post = utils.toPromise(function (doc, opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('post', arguments);
return;
Expand Down Expand Up @@ -552,11 +552,11 @@ function HttpPouch(opts, callback) {
} else {
api.put(doc, opts, callback);
}
};
});

// Update/create multiple documents given by req in the database
// given by host.
api.bulkDocs = function (req, opts, callback) {
api.bulkDocs = utils.toPromise(function (req, opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('bulkDocs', arguments);
return;
Expand Down Expand Up @@ -586,11 +586,11 @@ function HttpPouch(opts, callback) {
url: genDBUrl(host, '_bulk_docs'),
body: req
}, callback);
};
});

// Get a listing of the documents in the database given
// by host and ordered by increasing id.
api.allDocs = function (opts, callback) {
api.allDocs = utils.toPromise(function (opts, callback) {
// If no options were given, set the callback to be the second parameter
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('allDocs', arguments);
Expand Down Expand Up @@ -670,7 +670,7 @@ function HttpPouch(opts, callback) {
url: genDBUrl(host, '_all_docs' + params),
body: body
}, callback);
};
});

// Get a list of changes made to documents in the database given by host.
// TODO According to the README, there should be two other methods here,
Expand Down Expand Up @@ -896,7 +896,7 @@ function HttpPouch(opts, callback) {
// Given a set of document/revision IDs (given by req), tets the subset of
// those that do NOT correspond to revisions stored in the database.
// See http://wiki.apache.org/couchdb/HttpPostRevsDiff
api.revsDiff = function (req, opts, callback) {
api.revsDiff = utils.toPromise(function (req, opts, callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('revsDiff', arguments);
return;
Expand All @@ -916,15 +916,15 @@ function HttpPouch(opts, callback) {
}, function (err, res) {
utils.call(callback, err, res);
});
};
});

api.close = function (callback) {
api.close = utils.toPromise(function (callback) {
if (!api.taskqueue.ready()) {
api.taskqueue.addTask('close', arguments);
return;
}
utils.call(callback, null);
};
});

api.replicateOnServer = function (target, opts, promise) {
if (!api.taskqueue.ready()) {
Expand Down Expand Up @@ -1013,7 +1013,7 @@ function HttpPouch(opts, callback) {
}

// Delete the HttpPouch specified by the given name.
HttpPouch.destroy = function (name, opts, callback) {
HttpPouch.destroy = utils.toPromise(function (name, opts, callback) {
var host = getHost(name, opts);
opts = opts || {};
if (typeof opts === 'function') {
Expand All @@ -1024,7 +1024,7 @@ HttpPouch.destroy = function (name, opts, callback) {
opts.method = 'DELETE';
opts.url = genDBUrl(host, '');
utils.ajax(opts, callback);
};
});

// HttpPouch is a valid adapter.
HttpPouch.valid = function () {
Expand Down
Loading

0 comments on commit c8dbcb8

Please sign in to comment.