Skip to content

Commit

Permalink
multi emits from docs
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Oct 30, 2014
1 parent 78be39a commit 446d5ac
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 365 deletions.
45 changes: 33 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,18 @@ exports.spatial = function (fun, bbox, opts, cb) {
}

function addDoc(doc) {
var fulfill;
var promise = new Promise(function (f) {
fulfill = f;
});
var id = doc._id;
var emited = false;
var emited = [];
var i = 0;
function emit(doc) {
emited = true;
fulfill(store.insert(id, calculatebounds(doc)));
if (i++) {
emited.push(store.append(id , calculatebounds(doc)));
} else {
emited.push(store.insert(id , calculatebounds(doc)));
}
}
func(doc, emit);
if (!emited) {
fulfill();
}
return promise;
return Promise.all(emited);
}
var lastSeq;
return viewDB.get('_local/gclastSeq').catch(function () {
Expand Down Expand Up @@ -88,7 +85,31 @@ exports.spatial = function (fun, bbox, opts, cb) {
});
});
}).then(function () {
return store.query(bbox, true);
return new Promise(function (resolve, reject) {
var out = {};
var promises = [];
store.query(bbox).on('data', function (d) {
if (d.id in out) {
out[d.id].bboxen.push(d.bbox);
} else {
if (opts.include_docs) {
promises.push(db.get(d.id).then(function (doc) {
out[d.id].doc = doc;
}));
}
out[d.id] = {
id: d.id,
bboxen: [d.bbox]
};
}
}).on('error', reject).on('end', function () {
resolve(Promise.all(promises).then(function () {
return Object.keys(out).map(function (id) {
return out[id];
});
}));
});
});
}).then(function (resp) {
if (cb) {
return cb(null, resp);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"geojson-bounding-volume": "^0.2.2",
"lie": "^2.7.7",
"spark-md5": "0.0.5",
"async-rtree": "^1.0.1"
"async-rtree": "^1.1.0"
},
"devDependencies": {
"pouchdb": "^3.0.6",
Expand Down
Loading

0 comments on commit 446d5ac

Please sign in to comment.