Skip to content
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

Fixes LiveQuery behavior on multiple subscriptions #758

Merged
merged 5 commits into from
Mar 22, 2019
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 integration/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const api = new ParseServer({
appId: 'integration',
masterKey: 'notsosecret',
serverURL: 'http://localhost:1337/parse', // Don't forget to change to https if needed
cloud: `${__dirname}/cloud/main.js`
cloud: `${__dirname}/cloud/main.js`,
liveQuery: {
classNames: ['TestObject', 'DiffObject'],
},
startLiveQueryServer: true,
});

// Serve the Parse API on the /parse URL prefix
Expand Down
148 changes: 148 additions & 0 deletions integration/test/ParseLiveQueryTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
'use strict';

const assert = require('assert');
const clear = require('./clear');
const Parse = require('../../node');

const TestObject = Parse.Object.extend('TestObject');
const DiffObject = Parse.Object.extend('DiffObject');

describe('Parse LiveQuery', () => {
beforeEach((done) => {
Parse.initialize('integration');
Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse');
Parse.Storage._clear();
clear().then(done).catch(done.fail);
});

it('can subscribe to query', async () => {
const object = new TestObject();
await object.save();

const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
const subscription = await query.subscribe();

subscription.on('update', object => {
assert.equal(object.get('foo'), 'bar');
})
object.set({ foo: 'bar' });
await object.save();
});

it('can subscribe to multiple queries', async (done) => {
const objectA = new TestObject();
const objectB = new TestObject();
await Parse.Object.saveAll([objectA, objectB]);

const queryA = new Parse.Query(TestObject);
const queryB = new Parse.Query(TestObject);
queryA.equalTo('objectId', objectA.id);
queryB.equalTo('objectId', objectB.id);
const subscriptionA = await queryA.subscribe();
const subscriptionB = await queryB.subscribe();
let count = 0;
subscriptionA.on('update', object => {
count++;
assert.equal(object.get('foo'), 'bar');
})
subscriptionB.on('update', object => {
count++;
assert.equal(object.get('foo'), 'baz');
})
await objectA.save({ foo: 'bar' });
await objectB.save({ foo: 'baz' });

setTimeout(() => {
assert.equal(count, 2);
done();
}, 100);
});

it('can subscribe to multiple queries different class', async (done) => {
const objectA = new TestObject();
const objectB = new DiffObject();
await Parse.Object.saveAll([objectA, objectB]);

const queryA = new Parse.Query(TestObject);
const queryB = new Parse.Query(DiffObject);
queryA.equalTo('objectId', objectA.id);
queryB.equalTo('objectId', objectB.id);
const subscriptionA = await queryA.subscribe();
const subscriptionB = await queryB.subscribe();
let count = 0;
subscriptionA.on('update', object => {
count++;
assert.equal(object.get('foo'), 'bar');
})
subscriptionB.on('update', object => {
count++;
assert.equal(object.get('foo'), 'baz');
})
await objectA.save({ foo: 'bar' });
await objectB.save({ foo: 'baz' });

setTimeout(() => {
expect(count).toBe(2);
done();
}, 1000);
});

it('can unsubscribe to multiple queries different class', async (done) => {
const objectA = new TestObject();
const objectB = new DiffObject();
await Parse.Object.saveAll([objectA, objectB]);

const queryA = new Parse.Query(TestObject);
const queryB = new Parse.Query(DiffObject);
queryA.equalTo('objectId', objectA.id);
queryB.equalTo('objectId', objectB.id);
const subscriptionA = await queryA.subscribe();
const subscriptionB = await queryB.subscribe();
let count = 0;
subscriptionA.on('update', () => {
count++;
})
subscriptionB.on('update', object => {
count++;
assert.equal(object.get('foo'), 'baz');
})
subscriptionA.unsubscribe();
await objectA.save({ foo: 'bar' });
await objectB.save({ foo: 'baz' });

setTimeout(() => {
assert.equal(count, 1);
done();
}, 1000);
});

it('can unsubscribe with await to multiple queries different class', async (done) => {
const objectA = new TestObject();
const objectB = new DiffObject();
await Parse.Object.saveAll([objectA, objectB]);

const queryA = new Parse.Query(TestObject);
const queryB = new Parse.Query(DiffObject);
queryA.equalTo('objectId', objectA.id);
queryB.equalTo('objectId', objectB.id);
const subscriptionA = await queryA.subscribe();
const subscriptionB = await queryB.subscribe();
let count = 0;
subscriptionA.on('update', () => {
count++;
})
subscriptionB.on('update', object => {
count++;
assert.equal(object.get('foo'), 'baz');
})
await subscriptionA.unsubscribe();
await objectA.save({ foo: 'bar' });
await objectB.save({ foo: 'baz' });

setTimeout(() => {
assert.equal(count, 1);
done();
}, 1000);
});
});
7 changes: 6 additions & 1 deletion integration/test/helper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const ParseServer = require('parse-server').ParseServer;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
beforeAll((done) => {
const { app } = require('../server');
app.listen(1337, () => {
const httpServer = require('http').createServer(app);

httpServer.listen(1337, () => {
console.log('parse-server running on port 1337.');
done();
});
ParseServer.createLiveQueryServer(httpServer);
});
7 changes: 3 additions & 4 deletions src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,9 @@ module.exports = {

setLiveQueryController(controller: any) {
requireMethods('LiveQueryController', [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you help me understand what this change is please?

Copy link
Member

@dplewis dplewis Mar 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those function (subscribe, unsubscribe, open, close) already exists in the LiveQueryClient.

In the LiveQueryController those were just wrapper functions. Instead of using wrapper functions call the ones from the LiveQueryClient (Which you can get from the LiveQueryController).

setDefaultLiveQueryClient, getDefaultLiveQueryClient, _clearCachedDefaultClient already existed but weren't documented.

'subscribe',
'unsubscribe',
'open',
'close',
'setDefaultLiveQueryClient',
'getDefaultLiveQueryClient',
'_clearCachedDefaultClient',
], controller);
config['LiveQueryController'] = controller;
},
Expand Down
4 changes: 0 additions & 4 deletions src/LiveQueryClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ class LiveQueryClient extends EventEmitter {
this.socket.send(JSON.stringify(subscribeRequest));
});

// adding listener so process does not crash
// best practice is for developer to register their own listener
subscription.on('error', () => {});

return subscription;
}

Expand Down
8 changes: 6 additions & 2 deletions src/LiveQuerySubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ class Subscription extends EventEmitter {
this.id = id;
this.query = query;
this.sessionToken = sessionToken;

// adding listener so process does not crash
// best practice is for developer to register their own listener
this.on('error', () => {});
}

/**
* closes the subscription
* Close the subscription
*/
unsubscribe() {
unsubscribe(): Promise {
return CoreManager.getLiveQueryController().getDefaultLiveQueryClient().then((liveQueryClient) => {
liveQueryClient.unsubscribe(this);
this.emit('close');
Expand Down
Loading