Skip to content

Commit 012b522

Browse files
committed
fix(livequery): prevent unexpected connection closure when subcribe a query right after an unsubscription
1 parent d1ccc25 commit 012b522

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
"debug": "^3.1.0",
3131
"es6-promise": "4.2.3",
3232
"eventemitter3": "^2.0.3",
33-
"leancloud-realtime": "^4.0.0-beta.2",
34-
"leancloud-realtime-plugin-live-query": "^1.1.0",
33+
"leancloud-realtime": "^4.0.0-beta.5",
34+
"leancloud-realtime-plugin-live-query": "^1.1.1",
3535
"localstorage-memory": "^1.0.1",
3636
"md5": "^2.0.0",
3737
"superagent": "^3.3.1",

src/live-query.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)