Skip to content

Commit

Permalink
(#74) - use promises instead of callbacks + cleanup
Browse files Browse the repository at this point in the history
This commit cleans up implementation of persistent views introduced in
(#68). This commit significantly decreases complexity of error handling
as well as the general look of the code.

* TaskQueue uses just promises
* removed createKeysLookup which was a strange optimization in very rare
  edge case most likely resulting in code being slower
* defaultsTo - nice way to recover with some value if document was not found
* Promise.all - don't count promises manually
* sequentialize - nice way to make some function calls in sequence (for
  java funs: synchronized)
* minor cleanups
  • Loading branch information
neojski committed Apr 11, 2014
1 parent 6a35373 commit dfe44b0
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 555 deletions.
34 changes: 11 additions & 23 deletions create-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

var upsert = require('./upsert');

module.exports = function (opts, cb) {
module.exports = function (opts) {
var sourceDB = opts.db;
var viewName = opts.viewName;
var mapFun = opts.map;
var reduceFun = opts.reduce;
var randomizer = opts.randomizer;

sourceDB.info(function (err, info) {
if (err) {
return cb(err);
}
return sourceDB.info().then(function (info) {
var PouchDB = sourceDB.constructor;
var depDbName = info.db_name + '-mrview-' + PouchDB.utils.Crypto.MD5(
mapFun.toString() + (reduceFun && reduceFun.toString())) +
Expand All @@ -27,27 +24,18 @@ module.exports = function (opts, cb) {
doc._deleted = false;
return doc;
}
upsert(sourceDB, '_local/mrviews', diffFunction, function (err) {
if (err) {
return cb(err);
}
sourceDB.registerDependentDatabase(depDbName, function (err, res) {
if (err) {
return cb(err);
}
return upsert(sourceDB, '_local/mrviews', diffFunction).then(function () {
return sourceDB.registerDependentDatabase(depDbName).then(function (res) {
var db = res.db;
var view = new View(depDbName, db, sourceDB, mapFun, reduceFun);
view.db.get('_local/lastSeq', function (err, lastSeqDoc) {
if (err) {
if (err.name !== 'not_found') {
return cb(err);
} else {
view.seq = 0;
}
} else {
view.seq = lastSeqDoc.seq;
return view.db.get('_local/lastSeq').then(null, function (err) {
if (err.name === 'not_found') {
return 0;
}
cb(null, view);
throw err;
}).then(function (lastSeqDoc) {
view.seq = lastSeqDoc.seq;
return view;
});
});
});
Expand Down
Loading

0 comments on commit dfe44b0

Please sign in to comment.