@@ -30,12 +30,14 @@ module.exports = AV => {
3030 this . _client . register ( this ) ;
3131 this . _queryJSON = queryJSON ;
3232 this . _subscriptionId = subscriptionId ;
33- client . on ( 'message' , this . _dispatch . bind ( this ) ) ;
34- client . on ( 'reconnect' , ( ) => {
33+ this . _onMessage = this . _dispatch . bind ( this ) ;
34+ this . _onReconnect = ( ) => {
3535 subscribe ( this . _queryJSON , this . _subscriptionId ) . catch ( error =>
3636 console . error ( `LiveQuery resubscribe error: ${ error . message } ` )
3737 ) ;
38- } ) ;
38+ } ;
39+ client . on ( 'message' , this . _onMessage ) ;
40+ client . on ( 'reconnect' , this . _onReconnect ) ;
3941 } ,
4042 _dispatch ( message ) {
4143 message . forEach ( ( { op, object, query_id : queryId , updatedKeys } ) => {
@@ -89,12 +91,15 @@ module.exports = AV => {
8991 * @return {Promise }
9092 */
9193 unsubscribe ( ) {
92- this . _client . deregister ( this ) ;
94+ const client = this . _client ;
95+ client . off ( 'message' , this . _onMessage ) ;
96+ client . off ( 'reconnect' , this . _onReconnect ) ;
97+ client . deregister ( this ) ;
9398 return request ( {
9499 method : 'POST' ,
95100 path : '/LiveQuery/unsubscribe' ,
96101 data : {
97- id : this . _client . id ,
102+ id : client . id ,
98103 query_id : this . id ,
99104 } ,
100105 } ) ;
@@ -124,15 +129,21 @@ module.exports = AV => {
124129 returnACL,
125130 className : query . className ,
126131 } ;
127- return subscribe ( queryJSON , subscriptionId ) . then (
128- ( { query_id : queryId } ) =>
129- new AV . LiveQuery (
130- queryId ,
131- liveQueryClient ,
132- queryJSON ,
133- subscriptionId
134- )
135- ) ;
132+ const promise = subscribe ( queryJSON , subscriptionId )
133+ . then (
134+ ( { query_id : queryId } ) =>
135+ new AV . LiveQuery (
136+ queryId ,
137+ liveQueryClient ,
138+ queryJSON ,
139+ subscriptionId
140+ )
141+ )
142+ . finally ( ( ) => {
143+ liveQueryClient . deregister ( promise ) ;
144+ } ) ;
145+ liveQueryClient . register ( promise ) ;
146+ return promise ;
136147 } )
137148 ) ;
138149 } ,
0 commit comments