Skip to content

Commit

Permalink
Did some cursor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Apr 26, 2015
1 parent 3745754 commit 9e4c63f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Iridium.njsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{e9a19872-d84f-418a-8332-3ded543fb1fa}</ProjectGuid>
<ProjectHome />
<ProjectView>ShowAllFiles</ProjectView>
<ProjectView>ProjectFiles</ProjectView>
<StartupFile>node_modules\mocha\bin\mocha</StartupFile>
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
Expand Down
15 changes: 12 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.

9 changes: 6 additions & 3 deletions lib/Cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ class Cursor<TDocument extends { _id?: any }, TInstance> {
* @return {Promise} A promise which is resolved when all operations have been dispatched
*/
each(handler: (instance: TInstance) => void, callback?: General.Callback<void>): Bluebird<void> {
var helpers = this.model.helpers;
return new Bluebird<void>((resolve, reject) => {
this.cursor.each((err, item: TDocument) => {
if (err) return reject(err);
if (!item) return resolve(null);
this.model.handlers.documentReceived(this.conditions, item,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial)).then(handler);
this.model.handlers.documentReceived(this.conditions, item, function () { return helpers.wrapDocument.apply(helpers, arguments); }).then(handler);
});
}).nodeify(callback);
}
Expand All @@ -56,12 +57,13 @@ class Cursor<TDocument extends { _id?: any }, TInstance> {
* @return {Promise<TResult[]>} A promise which is fulfilled with the results of the transformations
*/
map<TResult>(transform: (instance: TInstance) => TResult | Bluebird<TResult>, callback?: General.Callback<TResult[]>): Bluebird<TResult[]> {
var helpers = this.model.helpers;
return new Bluebird<TResult[]>((resolve, reject) => {
var promises: Bluebird<TResult>[] = [];
this.cursor.each((err, item: TDocument) => {
if (err) return reject(err);
if (!item) return resolve(Bluebird.all(promises));
promises.push(this.model.handlers.documentReceived(this.conditions, item,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial))
promises.push(this.model.handlers.documentReceived(this.conditions, item, function () { return helpers.wrapDocument.apply(helpers, arguments); })
.then(<(instance) => TResult>transform));
});
}).nodeify(callback);
Expand All @@ -73,13 +75,14 @@ class Cursor<TDocument extends { _id?: any }, TInstance> {
* @return {Promise<TInstance[]>} A promise which resolves with the instances returned by the query
*/
toArray(callback?: General.Callback<TInstance[]>): Bluebird<TInstance[]> {
var helpers = this.model.helpers;
return new Bluebird<TDocument[]>((resolve, reject) => {
this.cursor.toArray((err, results: any[]) => {
if (err) return reject(err);
return resolve(<any>results);
});
}).map<TDocument, TInstance>((document) => {
return this.model.handlers.documentReceived(this.conditions, document,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial));
return this.model.handlers.documentReceived(this.conditions, document, function () { return helpers.wrapDocument.apply(helpers, arguments); });
}).nodeify(callback);
}

Expand Down

0 comments on commit 9e4c63f

Please sign in to comment.