Skip to content

Commit 979d660

Browse files
authored
feat: LiveQueryClient.close returns promise when WebSocket closes (#1735)
1 parent f79680c commit 979d660

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

integration/test/ParseLiveQueryTest.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ describe('Parse LiveQuery', () => {
99
Parse.User.enableUnsafeCurrentUser();
1010
});
1111

12+
afterEach(async () => {
13+
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
14+
client.state = 'closed';
15+
await client.close();
16+
});
17+
1218
it('can subscribe to query', async done => {
1319
const object = new TestObject();
1420
await object.save();
@@ -66,9 +72,9 @@ describe('Parse LiveQuery', () => {
6672
const query = new Parse.Query(TestObject);
6773
query.equalTo('objectId', object.id);
6874
const subscription = await client.subscribe(query);
69-
subscription.on('update', async object => {
75+
subscription.on('update', async (object) => {
7076
assert.equal(object.get('foo'), 'bar');
71-
client.close();
77+
await client.close();
7278
done();
7379
});
7480
await subscription.subscribePromise;
@@ -279,7 +285,8 @@ describe('Parse LiveQuery', () => {
279285
await expectAsync(subscription.subscribePromise).toBeRejectedWith(
280286
new Parse.Error(141, 'not allowed to subscribe')
281287
);
282-
client.close();
288+
client.state = 'closed';
289+
await client.close();
283290
});
284291

285292
it('connectPromise does throw', async () => {
@@ -306,6 +313,7 @@ describe('Parse LiveQuery', () => {
306313
await expectAsync(subscription.subscribePromise).toBeRejectedWith(
307314
new Parse.Error(141, 'not allowed to connect')
308315
);
309-
client.close();
316+
client.state = 'closed';
317+
await client.close();
310318
});
311319
});

src/LiveQueryClient.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ class LiveQueryClient extends EventEmitter {
258258
}
259259

260260
this.socket = new WebSocketImplementation(this.serverURL);
261+
this.socket.closingPromise = resolvingPromise();
261262

262263
// Bind WebSocket callbacks
263264
this.socket.onopen = () => {
@@ -268,7 +269,8 @@ class LiveQueryClient extends EventEmitter {
268269
this._handleWebSocketMessage(event);
269270
};
270271

271-
this.socket.onclose = () => {
272+
this.socket.onclose = (event) => {
273+
this.socket.closingPromise.resolve(event);
272274
this._handleWebSocketClose();
273275
};
274276

@@ -309,20 +311,22 @@ class LiveQueryClient extends EventEmitter {
309311
* This method will close the WebSocket connection to this LiveQueryClient,
310312
* cancel the auto reconnect and unsubscribe all subscriptions based on it.
311313
*
314+
* @returns {Promise | undefined} CloseEvent {@link https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close_event}
312315
*/
313-
close() {
316+
close(): ?Promise {
314317
if (this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED) {
315318
return;
316319
}
317320
this.state = CLIENT_STATE.DISCONNECTED;
318-
this.socket.close();
321+
this.socket?.close();
319322
// Notify each subscription about the close
320323
for (const subscription of this.subscriptions.values()) {
321324
subscription.subscribed = false;
322325
subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);
323326
}
324327
this._handleReset();
325328
this.emit(CLIENT_EMMITER_TYPES.CLOSE);
329+
return this.socket?.closingPromise;
326330
}
327331

328332
// ensure we start with valid state if connect is called again after close

src/Socket.weapp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module.exports = class SocketWeapp {
1313
this.onmessage(msg);
1414
});
1515

16-
wx.onSocketClose(() => {
17-
this.onclose();
16+
wx.onSocketClose((event) => {
17+
this.onclose(event);
1818
});
1919

2020
wx.onSocketError(error => {

0 commit comments

Comments
 (0)