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

Implement an equivalent for Parse.Query.count() #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/ParsePatches.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/QueryTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 23 additions & 7 deletions src/Subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Subscription {
pending: boolean;
subscribers: { [key: string]: Subscriber };
resultSet: Array<Id | IdWithOrderingInfo>;
resultCount: ?number;
observationCount: number;

constructor(query: ParseQuery) {
Expand All @@ -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;

Expand All @@ -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;
}
Expand All @@ -146,17 +152,27 @@ 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(
results,
this.originalQuery
);
this.pushData();
}, (err) => {
this.pending = false;
this.pushError(err);
});
}, errorHandler);
}

/**
Expand Down