diff --git a/integration/test/ParseLiveQueryTest.js b/integration/test/ParseLiveQueryTest.js index de066757c..ec7575e47 100644 --- a/integration/test/ParseLiveQueryTest.js +++ b/integration/test/ParseLiveQueryTest.js @@ -9,6 +9,12 @@ describe('Parse LiveQuery', () => { Parse.User.enableUnsafeCurrentUser(); }); + afterEach(async () => { + const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient(); + client.state = 'closed'; + await client.close(); + }); + it('can subscribe to query', async done => { const object = new TestObject(); await object.save(); @@ -66,9 +72,9 @@ describe('Parse LiveQuery', () => { const query = new Parse.Query(TestObject); query.equalTo('objectId', object.id); const subscription = await client.subscribe(query); - subscription.on('update', async object => { + subscription.on('update', async (object) => { assert.equal(object.get('foo'), 'bar'); - client.close(); + await client.close(); done(); }); await subscription.subscribePromise; @@ -279,7 +285,8 @@ describe('Parse LiveQuery', () => { await expectAsync(subscription.subscribePromise).toBeRejectedWith( new Parse.Error(141, 'not allowed to subscribe') ); - client.close(); + client.state = 'closed'; + await client.close(); }); it('connectPromise does throw', async () => { @@ -306,6 +313,7 @@ describe('Parse LiveQuery', () => { await expectAsync(subscription.subscribePromise).toBeRejectedWith( new Parse.Error(141, 'not allowed to connect') ); - client.close(); + client.state = 'closed'; + await client.close(); }); }); diff --git a/src/LiveQueryClient.js b/src/LiveQueryClient.js index 17b4ab40c..893ba365e 100644 --- a/src/LiveQueryClient.js +++ b/src/LiveQueryClient.js @@ -258,6 +258,7 @@ class LiveQueryClient extends EventEmitter { } this.socket = new WebSocketImplementation(this.serverURL); + this.socket.closingPromise = resolvingPromise(); // Bind WebSocket callbacks this.socket.onopen = () => { @@ -268,7 +269,8 @@ class LiveQueryClient extends EventEmitter { this._handleWebSocketMessage(event); }; - this.socket.onclose = () => { + this.socket.onclose = (event) => { + this.socket.closingPromise.resolve(event); this._handleWebSocketClose(); }; @@ -309,13 +311,14 @@ class LiveQueryClient extends EventEmitter { * This method will close the WebSocket connection to this LiveQueryClient, * cancel the auto reconnect and unsubscribe all subscriptions based on it. * + * @returns {Promise | undefined} CloseEvent {@link https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close_event} */ - close() { + close(): ?Promise { if (this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED) { return; } this.state = CLIENT_STATE.DISCONNECTED; - this.socket.close(); + this.socket?.close(); // Notify each subscription about the close for (const subscription of this.subscriptions.values()) { subscription.subscribed = false; @@ -323,6 +326,7 @@ class LiveQueryClient extends EventEmitter { } this._handleReset(); this.emit(CLIENT_EMMITER_TYPES.CLOSE); + return this.socket?.closingPromise; } // ensure we start with valid state if connect is called again after close diff --git a/src/Socket.weapp.js b/src/Socket.weapp.js index 35affd6f3..d8d3884e7 100644 --- a/src/Socket.weapp.js +++ b/src/Socket.weapp.js @@ -13,8 +13,8 @@ module.exports = class SocketWeapp { this.onmessage(msg); }); - wx.onSocketClose(() => { - this.onclose(); + wx.onSocketClose((event) => { + this.onclose(event); }); wx.onSocketError(error => {