diff --git a/src/ParsePatches.js b/src/ParsePatches.js index b3b677c..6f5c61e 100644 --- a/src/ParsePatches.js +++ b/src/ParsePatches.js @@ -61,6 +61,14 @@ var patches = { return this.equalTo('objectId', objectId).limit(1); }, + /** + * The ParseReact equivalent to Parse.Query(...).count() + */ + observeCount: function() { + this._observeCount = true; + return this; + }, + /** * Patches for Parse.User to watch for user signup / login / logout */ @@ -99,6 +107,9 @@ var ParsePatches = { if (!Parse.Query.prototype.observeOne) { Parse.Query.prototype.observeOne = patches.observeOne; } + if (!Parse.Query.prototype.observeCount) { + Parse.Query.prototype.observeCount = patches.observeCount; + } pointerMethods.forEach(function(method) { var old = Parse.Query.prototype[method]; Parse.Query.prototype[method] = function(attr, value) { diff --git a/src/QueryTools.js b/src/QueryTools.js index 4933c1e..2a35185 100644 --- a/src/QueryTools.js +++ b/src/QueryTools.js @@ -156,6 +156,9 @@ function matchesQuery( if (className !== query.className) { return false; } + if (query._observeCount) { + return false; + } return matchesQuery(object, query._where); } for (var field in query) { diff --git a/src/Subscription.js b/src/Subscription.js index 8569807..55f2cbe 100644 --- a/src/Subscription.js +++ b/src/Subscription.js @@ -91,6 +91,7 @@ class Subscription { pending: boolean; subscribers: { [key: string]: Subscriber }; resultSet: Array; + resultCount: ?number; observationCount: number; constructor(query: ParseQuery) { @@ -102,6 +103,7 @@ class Subscription { this.subscribers = {}; // The Ids of the objects returned by this Subscription's query this.resultSet = []; + this.resultCount = null; this.observationCount = 0; @@ -118,9 +120,13 @@ class Subscription { var oid = 'o' + this.observationCount++; this.subscribers[oid] = callbacks; - var resultSet = this.resultSet.map(extractId); - var data = resultSet.length ? ObjectStore.getDataForIds(resultSet) : []; - callbacks.onNext(this.originalQuery._observeOne ? data[0] : data); + if (this.originalQuery._observeCount) { + callbacks.onNext(this.resultCount); + } else { + var resultSet = this.resultSet.map(extractId); + var data = resultSet.length ? ObjectStore.getDataForIds(resultSet) : []; + callbacks.onNext(this.originalQuery._observeOne ? data[0] : data); + } return oid; } @@ -146,6 +152,19 @@ class Subscription { */ issueQuery() { this.pending = true; + var errorHandler = (err) => { + this.pending = false; + this.pushError(err); + }; + + if (this.originalQuery._observeCount) { + this.originalQuery.count().then((result) => { + this.pending = false; + this.resultCount = result; + this._forEachSubscriber((subscriber) => subscriber.onNext(result)); + }, errorHandler); + return; + } this.originalQuery.find().then((results) => { this.pending = false; this.resultSet = ObjectStore.storeQueryResults( @@ -153,10 +172,7 @@ class Subscription { this.originalQuery ); this.pushData(); - }, (err) => { - this.pending = false; - this.pushError(err); - }); + }, errorHandler); } /**