feat: Add ParseQuery.watch to trigger LiveQuery only on update of specific fields (#8028)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user