Skip to content

Commit

Permalink
Tweaked behaviour of cursor.each to differentiate it from map
Browse files Browse the repository at this point in the history
Now resolves the promise when all handlers have been dispatched but not
necessarily fulfilled.
  • Loading branch information
notheotherben committed Apr 24, 2015
1 parent 1e0e22f commit 710abbe
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 208 deletions.
14 changes: 11 additions & 3 deletions lib/Cursor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Cursor.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions lib/Cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class Cursor<TDocument, TInstance> {

}

/**
* Counts the number of documents which are matched by this cursor
* @param {function(Error, Number)} callback A callback which is triggered when the result is available
*/
count(callback?: general.Callback<number>): Promise<number> {
return new Promise<number>((resolve, reject) => {
this.cursor.count(true,(err, count) => {
Expand All @@ -20,13 +24,17 @@ class Cursor<TDocument, TInstance> {
}).nodeify(callback);
}

/**
* Runs the specified handler over each instance in the query results
* @param {function(Instance)} handler The handler which is triggered for each element in the query
* @param {function(Error)} callback A callback which is triggered when all operations have been dispatched
*/
each(handler: (instance: TInstance) => void, callback?: general.Callback<void>): Promise<void> {
return new Promise<void>((resolve, reject) => {
var promises = [];
this.cursor.each((err, item: TDocument) => {
if (err) return reject(err);
if (!item) return resolve(Promise.all(promises).then(() => null));
promises.push(this.model.handlers.documentReceived(this.conditions, item,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial)).then(handler));
if (!item) return resolve(null);
this.model.handlers.documentReceived(this.conditions, item,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial)).then(handler);
});
}).nodeify(callback);
}
Expand Down
Loading

0 comments on commit 710abbe

Please sign in to comment.