Skip to content

Adds ability to update a subscription #2935

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 5 commits into from
Oct 31, 2016
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
29 changes: 29 additions & 0 deletions spec/ParseLiveQueryServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,35 @@ describe('ParseLiveQueryServer', function() {
expect(JSON.stringify(args[1])).toBe(unsubscribeRequest);
});

it('can set update command message handler for a parseWebSocket', function() {
var parseLiveQueryServer = new ParseLiveQueryServer(10, 10, {});
// Register mock connect/subscribe/unsubscribe handler for the server
spyOn(parseLiveQueryServer, '_handleUpdateSubscription').and.callThrough();
spyOn(parseLiveQueryServer, '_handleUnsubscribe').and.callThrough();
spyOn(parseLiveQueryServer, '_handleSubscribe').and.callThrough();

// Make mock parseWebsocket
var EventEmitter = require('events');
var parseWebSocket = new EventEmitter();

// Register message handlers for the parseWebSocket
parseLiveQueryServer._onConnect(parseWebSocket);

// Check updateRequest request
var updateRequest = '{"op":"update"}';
// Trigger message event
parseWebSocket.emit('message', updateRequest);
// Make sure _handleUnsubscribe is called
var args = parseLiveQueryServer._handleUpdateSubscription.calls.mostRecent().args;
expect(args[0]).toBe(parseWebSocket);
expect(JSON.stringify(args[1])).toBe(updateRequest);
expect(parseLiveQueryServer._handleUnsubscribe).toHaveBeenCalled();
let unsubArgs = parseLiveQueryServer._handleUnsubscribe.calls.mostRecent().args;
expect(unsubArgs.length).toBe(3);
expect(unsubArgs[2]).toBe(false);
expect(parseLiveQueryServer._handleSubscribe).toHaveBeenCalled();
});

it('can set unknown command message handler for a parseWebSocket', function() {
var parseLiveQueryServer = new ParseLiveQueryServer(10, 10, {});
// Make mock parseWebsocket
Expand Down
14 changes: 13 additions & 1 deletion src/LiveQuery/ParseLiveQueryServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ class ParseLiveQueryServer {
case 'subscribe':
this._handleSubscribe(parseWebsocket, request);
break;
case 'update':
this._handleUpdateSubscription(parseWebsocket, request);
break;
case 'unsubscribe':
this._handleUnsubscribe(parseWebsocket, request);
break;
Expand Down Expand Up @@ -473,7 +476,12 @@ class ParseLiveQueryServer {
logger.verbose('Current client number: %d', this.clients.size);
}

_handleUnsubscribe(parseWebsocket: any, request: any): any {
_handleUpdateSubscription(parseWebsocket: any, request: any): any {
this._handleUnsubscribe(parseWebsocket, request, false);
this._handleSubscribe(parseWebsocket, request);
}

_handleUnsubscribe(parseWebsocket: any, request: any, notifyClient: bool = true): any {
// If we can not find this client, return error to client
if (!parseWebsocket.hasOwnProperty('clientId')) {
Client.pushError(parseWebsocket, 2, 'Can not find this client, make sure you connect to server before unsubscribing');
Expand Down Expand Up @@ -512,6 +520,10 @@ class ParseLiveQueryServer {
if (classSubscriptions.size === 0) {
this.subscriptions.delete(className);
}

if (!notifyClient) {
return;
}

client.pushUnsubscribe(request.requestId);

Expand Down
41 changes: 40 additions & 1 deletion src/LiveQuery/RequestSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let general = {
'properties': {
'op': {
'type': 'string',
'enum': ['connect', 'subscribe', 'unsubscribe']
'enum': ['connect', 'subscribe', 'unsubscribe', 'update']
},
},
};
Expand Down Expand Up @@ -78,6 +78,44 @@ let subscribe = {
'additionalProperties': false
};

let update = {
'title': 'Update operation schema',
'type': 'object',
'properties': {
'op': 'update',
'requestId': {
'type': 'number'
},
'query': {
'title': 'Query field schema',
'type': 'object',
'properties': {
'className': {
'type': 'string'
},
'where': {
'type': 'object'
},
'fields': {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
}
},
'required': ['where', 'className'],
'additionalProperties': false
},
'sessionToken': {
'type': 'string'
}
},
'required': ['op', 'requestId', 'query'],
'additionalProperties': false
};

let unsubscribe = {
'title': 'Unsubscribe operation schema',
'type': 'object',
Expand All @@ -95,6 +133,7 @@ let RequestSchema = {
'general': general,
'connect': connect,
'subscribe': subscribe,
'update': update,
'unsubscribe': unsubscribe
}

Expand Down