Skip to content

Commit ebc1081

Browse files
authored
Add installationId to LiveQuery (#977)
* Add installationId to LiveQuery * remove console.log * Add tests * combine requests
1 parent bcd8646 commit ebc1081

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

src/LiveQueryClient.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class LiveQueryClient extends EventEmitter {
122122
javascriptKey: ?string;
123123
masterKey: ?string;
124124
sessionToken: ?string;
125+
installationId: ?string;
126+
additionalProperties: boolean;
125127
connectPromise: Promise;
126128
subscriptions: Map;
127129
socket: any;
@@ -134,13 +136,15 @@ class LiveQueryClient extends EventEmitter {
134136
* @param {string} options.javascriptKey (optional)
135137
* @param {string} options.masterKey (optional) Your Parse Master Key. (Node.js only!)
136138
* @param {string} options.sessionToken (optional)
139+
* @param {string} options.installationId (optional)
137140
*/
138141
constructor({
139142
applicationId,
140143
serverURL,
141144
javascriptKey,
142145
masterKey,
143-
sessionToken
146+
sessionToken,
147+
installationId,
144148
}) {
145149
super();
146150

@@ -157,6 +161,8 @@ class LiveQueryClient extends EventEmitter {
157161
this.javascriptKey = javascriptKey;
158162
this.masterKey = masterKey;
159163
this.sessionToken = sessionToken;
164+
this.installationId = installationId;
165+
this.additionalProperties = true;
160166
this.connectPromise = resolvingPromise();
161167
this.subscriptions = new Map();
162168
this.state = CLIENT_STATE.INITIALIZED;
@@ -334,6 +340,9 @@ class LiveQueryClient extends EventEmitter {
334340
masterKey: this.masterKey,
335341
sessionToken: this.sessionToken
336342
};
343+
if (this.additionalProperties) {
344+
connectRequest.installationId = this.installationId;
345+
}
337346
this.socket.send(JSON.stringify(connectRequest));
338347
}
339348

@@ -373,6 +382,12 @@ class LiveQueryClient extends EventEmitter {
373382
} else {
374383
this.emit(CLIENT_EMMITER_TYPES.ERROR, data.error);
375384
}
385+
if (data.error === 'Additional properties not allowed') {
386+
this.additionalProperties = false;
387+
}
388+
if (data.reconnect) {
389+
this._handleReconnect();
390+
}
376391
break;
377392
case OP_EVENTS.UNSUBSCRIBED:
378393
// We have already deleted subscription in unsubscribe(), do nothing here

src/ParseLiveQuery.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ const DefaultLiveQueryController = {
8686
if (defaultLiveQueryClient) {
8787
return defaultLiveQueryClient;
8888
}
89-
const currentUser = await CoreManager.getUserController().currentUserAsync();
89+
const [currentUser, installationId] = await Promise.all([
90+
CoreManager.getUserController().currentUserAsync(),
91+
CoreManager.getInstallationController().currentInstallationId()
92+
]);
9093
const sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
9194

9295
let liveQueryServerURL = CoreManager.get('LIVEQUERY_SERVER_URL');
@@ -115,6 +118,7 @@ const DefaultLiveQueryController = {
115118
javascriptKey,
116119
masterKey,
117120
sessionToken,
121+
installationId,
118122
});
119123
defaultLiveQueryClient.on('error', (error) => {
120124
LiveQuery.emit('error', error);

src/__tests__/LiveQueryClient-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,38 @@ describe('LiveQueryClient', () => {
490490
expect(isChecked).toBe(true);
491491
});
492492

493+
it('can handle WebSocket reconnect on error event', () => {
494+
const liveQueryClient = new LiveQueryClient({
495+
applicationId: 'applicationId',
496+
serverURL: 'ws://test',
497+
javascriptKey: 'javascriptKey',
498+
masterKey: 'masterKey',
499+
sessionToken: 'sessionToken'
500+
});
501+
expect(liveQueryClient.additionalProperties).toBe(true);
502+
const data = {
503+
op: 'error',
504+
code: 1,
505+
reconnect: true,
506+
error: 'Additional properties not allowed',
507+
};
508+
const event = {
509+
data: JSON.stringify(data)
510+
}
511+
let isChecked = false;
512+
liveQueryClient.on('error', function(error) {
513+
isChecked = true;
514+
expect(error).toEqual(data.error);
515+
});
516+
const spy = jest.spyOn(liveQueryClient, '_handleReconnect');
517+
liveQueryClient._handleWebSocketMessage(event);
518+
519+
expect(isChecked).toBe(true);
520+
expect(liveQueryClient._handleReconnect).toHaveBeenCalledTimes(1);
521+
expect(liveQueryClient.additionalProperties).toBe(false);
522+
spy.mockRestore();
523+
});
524+
493525
it('can subscribe', async () => {
494526
const liveQueryClient = new LiveQueryClient({
495527
applicationId: 'applicationId',

src/__tests__/ParseLiveQuery-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
jest.dontMock('../ParseLiveQuery');
1111
jest.dontMock('../CoreManager');
12+
jest.dontMock('../InstallationController');
1213
jest.dontMock('../LiveQueryClient');
1314
jest.dontMock('../LiveQuerySubscription');
1415
jest.dontMock('../ParseObject');
@@ -30,6 +31,11 @@ describe('ParseLiveQuery', () => {
3031
beforeEach(() => {
3132
const controller = CoreManager.getLiveQueryController();
3233
controller._clearCachedDefaultClient();
34+
CoreManager.set('InstallationController', {
35+
currentInstallationId() {
36+
return Promise.resolve('1234');
37+
}
38+
});
3339
});
3440

3541
it('fails with an invalid livequery server url', (done) => {
@@ -63,6 +69,8 @@ describe('ParseLiveQuery', () => {
6369
expect(client.applicationId).toBe('appid');
6470
expect(client.javascriptKey).toBe('jskey');
6571
expect(client.sessionToken).toBe(undefined);
72+
expect(client.installationId).toBe('1234');
73+
expect(client.additionalProperties).toBe(true);
6674
done();
6775
});
6876
});

0 commit comments

Comments
 (0)