Skip to content
This repository was archived by the owner on Jan 26, 2021. It is now read-only.

Commit 6d4f105

Browse files
committed
Equivalent for Parse.Query.count(), for now not backed by a store
1 parent 455f448 commit 6d4f105

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/ParsePatches.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ var patches = {
6161
return this.equalTo('objectId', objectId).limit(1);
6262
},
6363

64+
/**
65+
* The ParseReact equivalent to Parse.Query(...).count()
66+
*/
67+
observeCount: function() {
68+
this._observeCount = true;
69+
return this;
70+
},
71+
6472
/**
6573
* Patches for Parse.User to watch for user signup / login / logout
6674
*/
@@ -99,6 +107,9 @@ var ParsePatches = {
99107
if (!Parse.Query.prototype.observeOne) {
100108
Parse.Query.prototype.observeOne = patches.observeOne;
101109
}
110+
if (!Parse.Query.prototype.observeCount) {
111+
Parse.Query.prototype.observeCount = patches.observeCount;
112+
}
102113
pointerMethods.forEach(function(method) {
103114
var old = Parse.Query.prototype[method];
104115
Parse.Query.prototype[method] = function(attr, value) {

src/QueryTools.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ function matchesQuery(
156156
if (className !== query.className) {
157157
return false;
158158
}
159+
if (query._observeCount) {
160+
return false;
161+
}
159162
return matchesQuery(object, query._where);
160163
}
161164
for (var field in query) {

src/Subscription.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class Subscription {
9191
pending: boolean;
9292
subscribers: { [key: string]: Subscriber };
9393
resultSet: Array<Id | IdWithOrderingInfo>;
94+
resultCount: ?number;
9495
observationCount: number;
9596

9697
constructor(query: ParseQuery) {
@@ -102,6 +103,7 @@ class Subscription {
102103
this.subscribers = {};
103104
// The Ids of the objects returned by this Subscription's query
104105
this.resultSet = [];
106+
this.resultCount = null;
105107

106108
this.observationCount = 0;
107109

@@ -118,9 +120,13 @@ class Subscription {
118120
var oid = 'o' + this.observationCount++;
119121
this.subscribers[oid] = callbacks;
120122

121-
var resultSet = this.resultSet.map(extractId);
122-
var data = resultSet.length ? ObjectStore.getDataForIds(resultSet) : [];
123-
callbacks.onNext(this.originalQuery._observeOne ? data[0] : data);
123+
if (this.originalQuery._observeCount) {
124+
callbacks.onNext(this.resultCount);
125+
} else {
126+
var resultSet = this.resultSet.map(extractId);
127+
var data = resultSet.length ? ObjectStore.getDataForIds(resultSet) : [];
128+
callbacks.onNext(this.originalQuery._observeOne ? data[0] : data);
129+
}
124130

125131
return oid;
126132
}
@@ -146,17 +152,27 @@ class Subscription {
146152
*/
147153
issueQuery() {
148154
this.pending = true;
155+
var errorHandler = (err) => {
156+
this.pending = false;
157+
this.pushError(err);
158+
};
159+
160+
if (this.originalQuery._observeCount) {
161+
this.originalQuery.count().then((result) => {
162+
this.pending = false;
163+
this.resultCount = result;
164+
this._forEachSubscriber((subscriber) => subscriber.onNext(result));
165+
}, errorHandler);
166+
return;
167+
}
149168
this.originalQuery.find().then((results) => {
150169
this.pending = false;
151170
this.resultSet = ObjectStore.storeQueryResults(
152171
results,
153172
this.originalQuery
154173
);
155174
this.pushData();
156-
}, (err) => {
157-
this.pending = false;
158-
this.pushError(err);
159-
});
175+
}, errorHandler);
160176
}
161177

162178
/**

0 commit comments

Comments
 (0)