From 78e68f6ac147a852b077fd33b9fc16fe7e8e61de Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 28 Dec 2018 17:20:36 -0600 Subject: [PATCH 1/2] Add original object to LiveQuery events --- src/LiveQueryClient.js | 6 +++- src/LiveQuerySubscription.js | 8 ++--- src/__tests__/LiveQueryClient-test.js | 43 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/LiveQueryClient.js b/src/LiveQueryClient.js index 9904e3a61..0d41f2567 100644 --- a/src/LiveQueryClient.js +++ b/src/LiveQueryClient.js @@ -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); } } } diff --git a/src/LiveQuerySubscription.js b/src/LiveQuerySubscription.js index 3593bc74e..9622f9654 100644 --- a/src/LiveQuerySubscription.js +++ b/src/LiveQuerySubscription.js @@ -37,22 +37,22 @@ import CoreManager from './CoreManager'; * * });

* - *

Update Event - When an existing ParseObject which fulfills the ParseQuery you subscribe + *

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. * *

- * subscription.on('update', (object) => {
+ * subscription.on('update', (object, original) => {
  *
  * });

* - *

Enter Event - When an existing ParseObject's old value doesn't fulfill the ParseQuery + *

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. * *

- * subscription.on('enter', (object) => {
+ * subscription.on('enter', (object, original) => {
  *
  * });

* diff --git a/src/__tests__/LiveQueryClient-test.js b/src/__tests__/LiveQueryClient-test.js index 6c289d777..ad2afaa54 100644 --- a/src/__tests__/LiveQueryClient-test.js +++ b/src/__tests__/LiveQueryClient-test.js @@ -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', From 370e0872db7f1c0993889b5deab19819bfaa66ff Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Sun, 30 Dec 2018 10:11:03 -0600 Subject: [PATCH 2/2] Parse Server 3.1.3 requirement --- src/LiveQuerySubscription.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/LiveQuerySubscription.js b/src/LiveQuerySubscription.js index 9622f9654..b2013a26b 100644 --- a/src/LiveQuerySubscription.js +++ b/src/LiveQuerySubscription.js @@ -42,6 +42,8 @@ import CoreManager from './CoreManager'; * 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 + * *
  * subscription.on('update', (object, original) => {
  *
@@ -51,6 +53,8 @@ import CoreManager from './CoreManager';
  * 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
+ *
  * 
  * subscription.on('enter', (object, original) => {
  *