@@ -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