Skip to content

Add original object to LiveQuery events #712

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 2 commits into from
Dec 30, 2018
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
6 changes: 5 additions & 1 deletion src/LiveQueryClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,11 @@ class LiveQueryClient extends EventEmitter {
if (!subscription) {
break;
}
subscription.emit(data.op, parseObject);
if (data.original) {
delete data.original.__type;
data.original = ParseObject.fromJSON(data.original, false);
}
subscription.emit(data.op, parseObject, data.original);
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/LiveQuerySubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,26 @@ import CoreManager from './CoreManager';
*
* });</pre></p>
*
* <p>Update Event - When an existing ParseObject which fulfills the ParseQuery you subscribe
* <p>Update Event - When an existing ParseObject (original) which fulfills the ParseQuery you subscribe
* is updated (The ParseObject fulfills the ParseQuery before and after changes),
* you'll get this event. The object is the ParseObject which is updated.
* Its content is the latest value of the ParseObject.
*
* Parse-Server 3.1.3+ Required for original object parameter
*
* <pre>
* subscription.on('update', (object) => {
* subscription.on('update', (object, original) => {
*
* });</pre></p>
*
* <p>Enter Event - When an existing ParseObject's old value doesn't fulfill the ParseQuery
* <p>Enter Event - When an existing ParseObject's (original) old value doesn't fulfill the ParseQuery
* but its new value fulfills the ParseQuery, you'll get this event. The object is the
* ParseObject which enters the ParseQuery. Its content is the latest value of the ParseObject.
*
* Parse-Server 3.1.3+ Required for original object parameter
*
* <pre>
* subscription.on('enter', (object) => {
* subscription.on('enter', (object, original) => {
*
* });</pre></p>
*
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/LiveQueryClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,49 @@ describe('LiveQueryClient', () => {
expect(isChecked).toBe(true);
});

it('can handle WebSocket response with original', () => {
const liveQueryClient = new LiveQueryClient({
applicationId: 'applicationId',
serverURL: 'ws://test',
javascriptKey: 'javascriptKey',
masterKey: 'masterKey',
sessionToken: 'sessionToken'
});
// Add mock subscription
const subscription = new events.EventEmitter();
liveQueryClient.subscriptions.set(1, subscription);
const object = new ParseObject('Test');
const original = new ParseObject('Test');
object.set('key', 'value');
original.set('key', 'old');
const data = {
op: 'update',
clientId: 1,
requestId: 1,
object: object._toFullJSON(),
original: original._toFullJSON(),
};
const event = {
data: JSON.stringify(data)
}
// Register checked in advance
let isChecked = false;
subscription.on('update', (parseObject, parseOriginalObject) => {
isChecked = true;
expect(parseObject.get('key')).toEqual('value');
expect(parseObject.get('className')).toBeUndefined();
expect(parseObject.get('__type')).toBeUndefined();

expect(parseOriginalObject.get('key')).toEqual('old');
expect(parseOriginalObject.get('className')).toBeUndefined();
expect(parseOriginalObject.get('__type')).toBeUndefined();
});

liveQueryClient._handleWebSocketMessage(event);

expect(isChecked).toBe(true);
});

it('can handle WebSocket close message', () => {
const liveQueryClient = new LiveQueryClient({
applicationId: 'applicationId',
Expand Down