Skip to content

Commit

Permalink
Added beforeUnsubscribe Trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
sadortun committed Jun 6, 2021
1 parent 129f7bf commit 2e70b92
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/LiveQuery/ParseLiveQueryServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ class ParseLiveQueryServer {
this._handleSubscribe(parseWebsocket, request);
}

_handleUnsubscribe(parseWebsocket: any, request: any, notifyClient: boolean = true): any {
async _handleUnsubscribe(parseWebsocket: any, request: any, notifyClient: boolean = true): any {
// If we can not find this client, return error to client
if (!Object.prototype.hasOwnProperty.call(parseWebsocket, 'clientId')) {
Client.pushError(
Expand Down Expand Up @@ -839,11 +839,35 @@ class ParseLiveQueryServer {
return;
}

const subscription = subscriptionInfo.subscription;
const className = subscription.className;
const trigger = getTrigger(className, 'beforeUnsubscribe', Parse.applicationId);
if (trigger) {
const auth = await this.getAuthFromClient(client, request.requestId, request.sessionToken);
if (auth && auth.user) {
request.user = auth.user;
}

const parseQuery = new Parse.Query(className);
parseQuery.withJSON(request.query);
request.query = subscription.query;
request.requestId = request.requestId;
request.sessionToken = subscriptionInfo.sessionToken;
request.useMasterKey = client.hasMasterKey;
request.installationId = client.installationId;

await runTrigger(trigger, `beforeUnsubscribe.${className}`, request, auth);

const query = request.query.toJSON();
if (query.keys) {
query.fields = query.keys.split(',');
}
request.query = query;
}

// Remove subscription from client
client.deleteSubscriptionInfo(requestId);
// Remove client from subscription
const subscription = subscriptionInfo.subscription;
const className = subscription.className;
subscription.deleteClientSubscription(parseWebsocket.clientId, requestId);
// If there is no client which is subscribing this subscription, remove it from subscriptions
const classSubscriptions = this.subscriptions.get(className);
Expand Down
44 changes: 44 additions & 0 deletions src/cloud-code/Parse.Cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,50 @@ ParseCloud.onLiveQueryEvent = function (handler) {
triggers.addLiveQueryEventHandler(handler, Parse.applicationId);
};



/**
* Registers a before live query subscription function.
*
* **Available in Cloud Code only.**
*
* If you want to use beforeUnsubscribe for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1.
* ```
* Parse.Cloud.beforeUnsubscribe('MyCustomClass', (request) => {
* // code here
* }, (request) => {
* // validation code here
* });
*
* Parse.Cloud.beforeUnsubscribe(Parse.User, (request) => {
* // code here
* }, { ...validationObject });
*```
*
* @method beforeUnsubscribe
* @name Parse.Cloud.beforeUnsubscribe
* @param {(String|Parse.Object)} arg1 The Parse.Object subclass to register the before subscription function for. This can instead be a String that is the className of the subclass.
* @param {Function} func The function to run before a subscription. This function can be async and should take one parameter, a {@link Parse.Cloud.TriggerRequest}.
* @param {(Object|Function)} validator An optional function to help validating cloud code. This function can be an async function and should take one parameter a {@link Parse.Cloud.TriggerRequest}, or a {@link Parse.Cloud.ValidatorObject}.
*/
ParseCloud.beforeUnsubscribe = function (parseClass, handler, validationHandler) {
validateValidator(validationHandler);
var className = getClassName(parseClass);
triggers.addTrigger(
triggers.Types.beforeUnsubscribe,
className,
handler,
Parse.applicationId,
validationHandler
);
};

ParseCloud.onLiveQueryEvent = function (handler) {
triggers.addLiveQueryEventHandler(handler, Parse.applicationId);
};



/**
* Registers an after live query server event function.
*
Expand Down
1 change: 1 addition & 0 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const Types = {
afterDeleteFile: 'afterDeleteFile',
beforeConnect: 'beforeConnect',
beforeSubscribe: 'beforeSubscribe',
beforeUnsubscribe: 'beforeUnsubscribe',
afterEvent: 'afterEvent',
};

Expand Down

0 comments on commit 2e70b92

Please sign in to comment.