Skip to content

Commit be11cbc

Browse files
committed
fix: Add keys and watch to LiveQueryClient.resubscribe
1 parent fc5fab0 commit be11cbc

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

integration/test/ParseLiveQueryTest.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ describe('Parse LiveQuery', () => {
9090
await promise;
9191
});
9292

93+
it('can resubscribe', async () => {
94+
const client = new Parse.LiveQueryClient({
95+
applicationId: 'integration',
96+
serverURL: 'ws://localhost:1337',
97+
javascriptKey: null,
98+
masterKey: null,
99+
sessionToken: null,
100+
});
101+
client.open();
102+
const resubscribeSpy = spyOn(client, 'resubscribe').and.callThrough();
103+
const consoleSpy = spyOn(console, 'error').and.callFake(() => {});
104+
const subscribeRequest = {
105+
op: 'subscribe',
106+
requestId: 1,
107+
query: {
108+
className: 'TestObject',
109+
where: { objectId: 'HEXkuHFm0D' },
110+
keys: ['foo', 'objectId'],
111+
watch: undefined,
112+
unknownField: 'throws Additional properties not allowed error',
113+
},
114+
sessionToken: undefined,
115+
};
116+
await client.connectPromise;
117+
client.socket.send(JSON.stringify(subscribeRequest));
118+
await sleep(1000);
119+
expect(resubscribeSpy).toHaveBeenCalled();
120+
expect(consoleSpy).toHaveBeenCalledWith(
121+
'LiveQuery reconnecting with error:',
122+
'Additional properties not allowed',
123+
'code:',
124+
1
125+
);
126+
await client.close();
127+
});
128+
93129
it('can subscribe to multiple queries', async () => {
94130
const objectA = new TestObject();
95131
const objectB = new TestObject();

src/LiveQueryClient.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const SUBSCRIPTION_EMMITER_TYPES = {
5555
DELETE: 'delete',
5656
};
5757

58+
// Exponentially-growing random delay
5859
const generateInterval = k => {
5960
return Math.random() * Math.min(30, Math.pow(2, k) - 1) * 1000;
6061
};
@@ -291,7 +292,8 @@ class LiveQueryClient {
291292
const query = subscription.query;
292293
const queryJSON = query.toJSON();
293294
const where = queryJSON.where;
294-
const fields = queryJSON.keys ? queryJSON.keys.split(',') : undefined;
295+
const keys = queryJSON.keys?.split(',');
296+
const watch = queryJSON.watch?.split(',');
295297
const className = query.className;
296298
const sessionToken = subscription.sessionToken;
297299
const subscribeRequest = {
@@ -300,7 +302,8 @@ class LiveQueryClient {
300302
query: {
301303
className,
302304
where,
303-
fields,
305+
keys,
306+
watch,
304307
},
305308
sessionToken: undefined as string | undefined,
306309
};
@@ -347,7 +350,6 @@ class LiveQueryClient {
347350
}
348351

349352
_handleWebSocketOpen() {
350-
this.attempts = 1;
351353
const connectRequest = {
352354
op: OP_TYPES.CONNECT,
353355
applicationId: this.applicationId,
@@ -387,6 +389,7 @@ class LiveQueryClient {
387389
break;
388390
case OP_EVENTS.SUBSCRIBED:
389391
if (subscription) {
392+
this.attempts = 1;
390393
subscription.subscribed = true;
391394
subscription.subscribePromise.resolve();
392395
setTimeout(() => subscription.emit(SUBSCRIPTION_EMMITER_TYPES.OPEN, response), 200);
@@ -410,6 +413,7 @@ class LiveQueryClient {
410413
this.additionalProperties = false;
411414
}
412415
if (data.reconnect) {
416+
console.error('LiveQuery reconnecting with error:', data.error, 'code:', data.code);
413417
this._handleReconnect();
414418
}
415419
break;
@@ -485,19 +489,16 @@ class LiveQueryClient {
485489
if (this.state === CLIENT_STATE.DISCONNECTED) {
486490
return;
487491
}
488-
489492
this.state = CLIENT_STATE.RECONNECTING;
490493
const time = generateInterval(this.attempts);
491494

492495
// handle case when both close/error occur at frequent rates we ensure we do not reconnect unnecessarily.
493496
// we're unable to distinguish different between close/error when we're unable to reconnect therefore
494497
// we try to reconnect in both cases
495498
// server side ws and browser WebSocket behave differently in when close/error get triggered
496-
497499
if (this.reconnectHandle) {
498500
clearTimeout(this.reconnectHandle);
499501
}
500-
501502
this.reconnectHandle = setTimeout(
502503
(() => {
503504
this.attempts++;

src/__tests__/LiveQueryClient-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ describe('LiveQueryClient', () => {
748748
expect(error).toEqual(data.error);
749749
});
750750
const spy = jest.spyOn(liveQueryClient, '_handleReconnect');
751+
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
751752
try {
752753
liveQueryClient._handleWebSocketMessage(event);
753754
await liveQueryClient.connectPromise;
@@ -944,6 +945,8 @@ describe('LiveQueryClient', () => {
944945
};
945946
const query = new ParseQuery('Test');
946947
query.equalTo('key', 'value');
948+
query.select(['key']);
949+
query.watch(['key']);
947950
liveQueryClient.subscribe(query);
948951
liveQueryClient.connectPromise.resolve();
949952

@@ -961,6 +964,8 @@ describe('LiveQueryClient', () => {
961964
where: {
962965
key: 'value',
963966
},
967+
keys: ['key'],
968+
watch: ['key'],
964969
},
965970
});
966971
});
@@ -978,6 +983,8 @@ describe('LiveQueryClient', () => {
978983
};
979984
const query = new ParseQuery('Test');
980985
query.equalTo('key', 'value');
986+
query.select(['key']);
987+
query.watch(['key']);
981988
liveQueryClient.subscribe(query, 'mySessionToken');
982989
liveQueryClient.connectPromise.resolve();
983990

@@ -996,6 +1003,8 @@ describe('LiveQueryClient', () => {
9961003
where: {
9971004
key: 'value',
9981005
},
1006+
keys: ['key'],
1007+
watch: ['key'],
9991008
},
10001009
});
10011010
});

0 commit comments

Comments
 (0)