Skip to content

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

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 16 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 43 additions & 0 deletions spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,49 @@ describe('ParseLiveQuery', function () {
await object.save();
});

it('can handle live query with fields', async () => {
await reconfigureServer({
liveQuery: {
classNames: ['Test'],
},
startLiveQueryServer: true,
});
const query = new Parse.Query('Test');
query.select('yolo');
const subscription = await query.subscribe();
const spy = {
create(obj) {
if (!obj.get('yolo')) {
fail('create should not have been called');
}
},
update(object, original) {
if (object.get('yolo') === original.get('yolo')) {
fail('create should not have been called');
}
},
};
const createSpy = spyOn(spy, 'create').and.callThrough();
const updateSpy = spyOn(spy, 'update').and.callThrough();
subscription.on('create', spy.create);
subscription.on('update', spy.update);
const obj = new Parse.Object('Test');
obj.set('foo', 'bar');
await obj.save();
obj.set('foo', 'xyz');
obj.set('yolo', 'xyz');
await obj.save();
const obj2 = new Parse.Object('Test');
obj2.set('foo', 'bar');
obj2.set('yolo', 'bar');
await obj2.save();
obj2.set('foo', 'bart');
await obj2.save();
await new Promise(resolve => setTimeout(resolve, 2000));
expect(createSpy).toHaveBeenCalledTimes(1);
expect(updateSpy).toHaveBeenCalledTimes(1);
});

it('can handle afterEvent set pointers', async done => {
await reconfigureServer({
liveQuery: {
Expand Down
3 changes: 2 additions & 1 deletion spec/ParseLiveQueryServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ describe('ParseLiveQueryServer', function () {
const parseLiveQueryServer = new ParseLiveQueryServer({});
// Make mock request message
const message = generateMockMessage(true);
message.currentParseObject.set('test', 'bar');

const clientId = 1;
const parseWebSocket = {
Expand Down Expand Up @@ -1072,7 +1073,7 @@ describe('ParseLiveQueryServer', function () {
where: {
key: 'value',
},
fields: ['test'],
fields: ['key'],
};
await addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query);
// Mock _matchesSubscription to return matching
Expand Down
24 changes: 23 additions & 1 deletion src/LiveQuery/ParseLiveQueryServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { ParsePubSub } from './ParsePubSub';
import SchemaController from '../Controllers/SchemaController';
import _ from 'lodash';
import { v4 as uuidv4 } from 'uuid';
import { runLiveQueryEventHandlers, getTrigger, runTrigger, resolveError, toJSONwithObjects } from '../triggers';
import {
runLiveQueryEventHandlers,
getTrigger,
runTrigger,
resolveError,
toJSONwithObjects,
} from '../triggers';
import { getAuthForSessionToken, Auth } from '../Auth';
import { getCacheController } from '../Controllers';
import LRU from 'lru-cache';
Expand Down Expand Up @@ -242,6 +248,11 @@ class ParseLiveQueryServer {
continue;
}
requestIds.forEach(async requestId => {
const updatedFields = this._checkFields(client, requestId, message);
if (!updatedFields) {
return;
}

// Set orignal ParseObject ACL checking promise, if the object does not match
// subscription, we do not need to check ACL
let originalACLCheckingPromise;
Expand Down Expand Up @@ -608,6 +619,17 @@ class ParseLiveQueryServer {
return auth;
}

_checkFields(client: any, requestId: any, message: any) {
const subscriptionInfo = client.getSubscriptionInfo(requestId);
const fields = subscriptionInfo?.fields;
if (!fields) {
return true;
}
const object = message.currentParseObject;
const original = message.originalParseObject;
return fields.some(field => 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) {
Expand Down