Repository: parse-community/Parse-SDK-JS (JavaScript)

Issue:
Ensure LiveQueryClient closes
### New Feature / Enhancement Checklist
<!--
    Check every following box [x] before submitting your issue.
    Click the "Preview" tab for better readability.
    Thanks for contributing to Parse Platform!
-->

- [x] I am not disclosing a [vulnerability](https://github.com/parse-community/parse-server/blob/master/SECURITY.md).
- [x] I am not just asking a [question](https://github.com/parse-community/.github/blob/master/SUPPORT.md).
- [x] I have searched through [existing issues](https://github.com/parse-community/Parse-SDK-JS/issues?q=is%3Aissue).

### Feature / Enhancement Description
<!-- What is the concept of the functionality and how should it be implemented? -->
There are a lot of events that happens when the client closes. I want to know when the websocket itself closes. This is useful for testing and ensure that connections to the server are closed.

### Example Use Case
<!-- What is an example use case in steps (1. / 2. / 3. / etc.) that describes the functionality? -->
Allow close to return a promise
```
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
await client.close();
```

### Alternatives / Workarounds
<!-- Which alternatives or workarounds exist currently? -->
The websocket closes in the background so you will have to set a timeout.
```
client.close();
sleep(1000);
```



Discussion: 
### Thanks for opening this issue!

- ❌ Please edit your post and use the provided template when creating a new issue. This helps everyone to understand your post better and asks for essential information to quicker review the issue.

<!-- parse-issue-bot-meta-tag-id {"requireTemplate":true} -->





Fix (patch):
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 => {