feat: Add ParseQuery.watch to trigger LiveQuery only on update of specific fields (#8028)

This commit is contained in:
Daniel
2023-01-16 22:32:22 +11:00
committed by GitHub
parent 62b3426b14
commit fc92faac75
3 changed files with 117 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import { getCacheController, getDatabaseController } from '../Controllers';
import LRU from 'lru-cache';
import UserRouter from '../Routers/UsersRouter';
import DatabaseController from '../Controllers/DatabaseController';
import { isDeepStrictEqual } from 'util';
class ParseLiveQueryServer {
clients: Map;
@@ -329,6 +330,10 @@ class ParseLiveQueryServer {
} else {
return null;
}
const watchFieldsChanged = this._checkWatchFields(client, requestId, message);
if (!watchFieldsChanged && (type === 'update' || type === 'create')) {
return;
}
res = {
event: type,
sessionToken: client.sessionToken,
@@ -707,6 +712,17 @@ class ParseLiveQueryServer {
return auth;
}
_checkWatchFields(client: any, requestId: any, message: any) {
const subscriptionInfo = client.getSubscriptionInfo(requestId);
const watch = subscriptionInfo?.watch;
if (!watch) {
return true;
}
const object = message.currentParseObject;
const original = message.originalParseObject;
return watch.some(field => !isDeepStrictEqual(object.get(field), original?.get(field)));
}
async _matchesACL(acl: any, client: any, requestId: number): Promise<boolean> {
// Return true directly if ACL isn't present, ACL is public read, or client has master key
if (!acl || acl.getPublicReadAccess() || client.hasMasterKey) {
@@ -888,6 +904,9 @@ class ParseLiveQueryServer {
if (request.query.fields) {
subscriptionInfo.fields = request.query.fields;
}
if (request.query.watch) {
subscriptionInfo.watch = request.query.watch;
}
if (request.sessionToken) {
subscriptionInfo.sessionToken = request.sessionToken;
}