Skip to content

feat: LiveQueryClient.close returns promise when WebSocket closes #1735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions integration/test/ParseLiveQueryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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();
});
});
10 changes: 7 additions & 3 deletions src/LiveQueryClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class LiveQueryClient extends EventEmitter {
}

this.socket = new WebSocketImplementation(this.serverURL);
this.socket.closingPromise = resolvingPromise();

// Bind WebSocket callbacks
this.socket.onopen = () => {
Expand All @@ -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();
};

Expand Down Expand Up @@ -309,20 +311,22 @@ 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;
subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);
}
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
Expand Down
4 changes: 2 additions & 2 deletions src/Socket.weapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module.exports = class SocketWeapp {
this.onmessage(msg);
});

wx.onSocketClose(() => {
this.onclose();
wx.onSocketClose((event) => {
this.onclose(event);
});

wx.onSocketError(error => {
Expand Down