@@ -21,6 +21,7 @@ const OP_TYPES = {
2121 SUBSCRIBE : 'subscribe' ,
2222 UNSUBSCRIBE : 'unsubscribe' ,
2323 ERROR : 'error' ,
24+ QUERY : 'query' ,
2425} ;
2526
2627// The event we get back from LiveQuery server
@@ -34,6 +35,7 @@ const OP_EVENTS = {
3435 ENTER : 'enter' ,
3536 LEAVE : 'leave' ,
3637 DELETE : 'delete' ,
38+ RESULT : 'result' ,
3739} ;
3840
3941// The event the LiveQuery client should emit
@@ -53,10 +55,11 @@ const SUBSCRIPTION_EMMITER_TYPES = {
5355 ENTER : 'enter' ,
5456 LEAVE : 'leave' ,
5557 DELETE : 'delete' ,
58+ RESULT : 'result' ,
5659} ;
5760
5861// Exponentially-growing random delay
59- const generateInterval = k => {
62+ const generateInterval = ( k : number ) : number => {
6063 return Math . random ( ) * Math . min ( 30 , Math . pow ( 2 , k ) - 1 ) * 1000 ;
6164} ;
6265
@@ -123,13 +126,15 @@ class LiveQueryClient {
123126 emit : any ;
124127
125128 /**
126- * @param {object } options
127- * @param {string } options.applicationId - applicationId of your Parse app
128- * @param {string } options.serverURL - <b>the URL of your LiveQuery server</b>
129- * @param {string } options.javascriptKey (optional)
130- * @param {string } options.masterKey (optional) Your Parse Master Key. (Node.js only!)
131- * @param {string } options.sessionToken (optional)
132- * @param {string } options.installationId (optional)
129+ * Creates a new LiveQueryClient instance.
130+ *
131+ * @param options - Configuration options for the LiveQuery client
132+ * @param options.applicationId - The applicationId of your Parse app
133+ * @param options.serverURL - The URL of your LiveQuery server (must start with 'ws' or 'wss')
134+ * @param options.javascriptKey - (Optional) The JavaScript key for your Parse app
135+ * @param options.masterKey - (Optional) Your Parse Master Key (Node.js only!)
136+ * @param options.sessionToken - (Optional) Session token for authenticated requests
137+ * @param options.installationId - (Optional) Installation ID for the client
133138 */
134139 constructor ( {
135140 applicationId,
@@ -138,6 +143,13 @@ class LiveQueryClient {
138143 masterKey,
139144 sessionToken,
140145 installationId,
146+ } : {
147+ applicationId : string ;
148+ serverURL : string ;
149+ javascriptKey ?: string ;
150+ masterKey ?: string ;
151+ sessionToken ?: string ;
152+ installationId ?: string ;
141153 } ) {
142154 if ( ! serverURL || serverURL . indexOf ( 'ws' ) !== 0 ) {
143155 throw new Error (
@@ -162,8 +174,8 @@ class LiveQueryClient {
162174 const EventEmitter = CoreManager . getEventEmitter ( ) ;
163175 this . emitter = new EventEmitter ( ) ;
164176
165- this . on = ( eventName , listener ) => this . emitter . on ( eventName , listener ) ;
166- this . emit = ( eventName , ...args ) => this . emitter . emit ( eventName , ...args ) ;
177+ this . on = ( eventName : string , listener : ( ... args : unknown [ ] ) => void ) => this . emitter . on ( eventName , listener ) ;
178+ this . emit = ( eventName : string , ...args : unknown [ ] ) => this . emitter . emit ( eventName , ...args ) ;
167179 // adding listener so process does not crash
168180 // best practice is for developer to register their own listener
169181 this . on ( 'error' , ( ) => { } ) ;
@@ -212,14 +224,14 @@ class LiveQueryClient {
212224 subscribeRequest . sessionToken = sessionToken ;
213225 }
214226
215- const subscription = new LiveQuerySubscription ( this . requestId , query , sessionToken ) ;
227+ const subscription = new LiveQuerySubscription ( this . requestId , query , sessionToken , this ) ;
216228 this . subscriptions . set ( this . requestId , subscription ) ;
217229 this . requestId += 1 ;
218230 this . connectPromise
219231 . then ( ( ) => {
220232 this . socket . send ( JSON . stringify ( subscribeRequest ) ) ;
221233 } )
222- . catch ( error => {
234+ . catch ( ( error : Error ) => {
223235 subscription . subscribePromise . reject ( error ) ;
224236 } ) ;
225237
@@ -425,6 +437,18 @@ class LiveQueryClient {
425437 }
426438 break ;
427439 }
440+ case OP_EVENTS . RESULT : {
441+ if ( subscription ) {
442+ const objects = data . results . map ( ( json : Record < string , unknown > ) => {
443+ if ( ! json . className && subscription . query ) {
444+ json . className = subscription . query . className ;
445+ }
446+ return ParseObject . fromJSON ( json , false ) ;
447+ } ) ;
448+ subscription . emit ( SUBSCRIPTION_EMMITER_TYPES . RESULT , objects ) ;
449+ }
450+ break ;
451+ }
428452 default : {
429453 // create, update, enter, leave, delete cases
430454 if ( ! subscription ) {
0 commit comments