Skip to content

Commit 48865c7

Browse files
authored
Adds ability to update a subscription (#2935)
* Adds ability to update a subscription * Adds unsubscribe to the RequestSchema, makes sure to not fire unsubscribe to the client when updating * Fix failing tests * More extensive tests * fix annotation
1 parent 94178df commit 48865c7

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

spec/ParseLiveQueryServer.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,35 @@ describe('ParseLiveQueryServer', function() {
335335
expect(JSON.stringify(args[1])).toBe(unsubscribeRequest);
336336
});
337337

338+
it('can set update command message handler for a parseWebSocket', function() {
339+
var parseLiveQueryServer = new ParseLiveQueryServer(10, 10, {});
340+
// Register mock connect/subscribe/unsubscribe handler for the server
341+
spyOn(parseLiveQueryServer, '_handleUpdateSubscription').and.callThrough();
342+
spyOn(parseLiveQueryServer, '_handleUnsubscribe').and.callThrough();
343+
spyOn(parseLiveQueryServer, '_handleSubscribe').and.callThrough();
344+
345+
// Make mock parseWebsocket
346+
var EventEmitter = require('events');
347+
var parseWebSocket = new EventEmitter();
348+
349+
// Register message handlers for the parseWebSocket
350+
parseLiveQueryServer._onConnect(parseWebSocket);
351+
352+
// Check updateRequest request
353+
var updateRequest = '{"op":"update"}';
354+
// Trigger message event
355+
parseWebSocket.emit('message', updateRequest);
356+
// Make sure _handleUnsubscribe is called
357+
var args = parseLiveQueryServer._handleUpdateSubscription.calls.mostRecent().args;
358+
expect(args[0]).toBe(parseWebSocket);
359+
expect(JSON.stringify(args[1])).toBe(updateRequest);
360+
expect(parseLiveQueryServer._handleUnsubscribe).toHaveBeenCalled();
361+
let unsubArgs = parseLiveQueryServer._handleUnsubscribe.calls.mostRecent().args;
362+
expect(unsubArgs.length).toBe(3);
363+
expect(unsubArgs[2]).toBe(false);
364+
expect(parseLiveQueryServer._handleSubscribe).toHaveBeenCalled();
365+
});
366+
338367
it('can set unknown command message handler for a parseWebSocket', function() {
339368
var parseLiveQueryServer = new ParseLiveQueryServer(10, 10, {});
340369
// Make mock parseWebsocket

src/LiveQuery/ParseLiveQueryServer.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ class ParseLiveQueryServer {
257257
case 'subscribe':
258258
this._handleSubscribe(parseWebsocket, request);
259259
break;
260+
case 'update':
261+
this._handleUpdateSubscription(parseWebsocket, request);
262+
break;
260263
case 'unsubscribe':
261264
this._handleUnsubscribe(parseWebsocket, request);
262265
break;
@@ -471,7 +474,12 @@ class ParseLiveQueryServer {
471474
logger.verbose('Current client number: %d', this.clients.size);
472475
}
473476

474-
_handleUnsubscribe(parseWebsocket: any, request: any): any {
477+
_handleUpdateSubscription(parseWebsocket: any, request: any): any {
478+
this._handleUnsubscribe(parseWebsocket, request, false);
479+
this._handleSubscribe(parseWebsocket, request);
480+
}
481+
482+
_handleUnsubscribe(parseWebsocket: any, request: any, notifyClient: bool = true): any {
475483
// If we can not find this client, return error to client
476484
if (!parseWebsocket.hasOwnProperty('clientId')) {
477485
Client.pushError(parseWebsocket, 2, 'Can not find this client, make sure you connect to server before unsubscribing');
@@ -510,6 +518,10 @@ class ParseLiveQueryServer {
510518
if (classSubscriptions.size === 0) {
511519
this.subscriptions.delete(className);
512520
}
521+
522+
if (!notifyClient) {
523+
return;
524+
}
513525

514526
client.pushUnsubscribe(request.requestId);
515527

src/LiveQuery/RequestSchema.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let general = {
44
'properties': {
55
'op': {
66
'type': 'string',
7-
'enum': ['connect', 'subscribe', 'unsubscribe']
7+
'enum': ['connect', 'subscribe', 'unsubscribe', 'update']
88
},
99
},
1010
};
@@ -78,6 +78,44 @@ let subscribe = {
7878
'additionalProperties': false
7979
};
8080

81+
let update = {
82+
'title': 'Update operation schema',
83+
'type': 'object',
84+
'properties': {
85+
'op': 'update',
86+
'requestId': {
87+
'type': 'number'
88+
},
89+
'query': {
90+
'title': 'Query field schema',
91+
'type': 'object',
92+
'properties': {
93+
'className': {
94+
'type': 'string'
95+
},
96+
'where': {
97+
'type': 'object'
98+
},
99+
'fields': {
100+
"type": "array",
101+
"items": {
102+
"type": "string"
103+
},
104+
"minItems": 1,
105+
"uniqueItems": true
106+
}
107+
},
108+
'required': ['where', 'className'],
109+
'additionalProperties': false
110+
},
111+
'sessionToken': {
112+
'type': 'string'
113+
}
114+
},
115+
'required': ['op', 'requestId', 'query'],
116+
'additionalProperties': false
117+
};
118+
81119
let unsubscribe = {
82120
'title': 'Unsubscribe operation schema',
83121
'type': 'object',
@@ -95,6 +133,7 @@ let RequestSchema = {
95133
'general': general,
96134
'connect': connect,
97135
'subscribe': subscribe,
136+
'update': update,
98137
'unsubscribe': unsubscribe
99138
}
100139

0 commit comments

Comments
 (0)