Skip to content

Commit 8cead2c

Browse files
authored
LiveQuery subscribe with sessionToken (#791)
* LiveQuery subscribe with sessionToken * fix tests * improve test
1 parent f7dbc24 commit 8cead2c

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

integration/test/ParseLiveQueryTest.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('Parse LiveQuery', () => {
1212
beforeEach((done) => {
1313
Parse.initialize('integration');
1414
Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse');
15+
Parse.User.enableUnsafeCurrentUser();
1516
Parse.Storage._clear();
1617
clear().then(done).catch(done.fail);
1718
});
@@ -135,4 +136,23 @@ describe('Parse LiveQuery', () => {
135136
await sleep(1000);
136137
assert.equal(count, 1);
137138
});
139+
140+
it('can subscribe to ACL', async (done) => {
141+
const user = await Parse.User.signUp('ooo', 'password');
142+
const ACL = new Parse.ACL(user);
143+
144+
const object = new TestObject();
145+
object.setACL(ACL);
146+
await object.save();
147+
148+
const query = new Parse.Query(TestObject);
149+
query.equalTo('objectId', object.id);
150+
const subscription = await query.subscribe(user.getSessionToken());
151+
subscription.on('update', async (object) => {
152+
assert.equal(object.get('foo'), 'bar');
153+
await Parse.User.logOut();
154+
done();
155+
})
156+
await object.save({ foo: 'bar' });
157+
});
138158
});

src/ParseQuery.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,13 +1483,15 @@ class ParseQuery {
14831483
/**
14841484
* Subscribe this query to get liveQuery updates
14851485
*
1486+
* @param {String} sessionToken (optional) Defaults to the currentUser
14861487
* @return {Promise<LiveQuerySubscription>} Returns the liveQuerySubscription, it's an event emitter
14871488
* which can be used to get liveQuery updates.
14881489
*/
1489-
async subscribe(): Promise<LiveQuerySubscription> {
1490+
async subscribe(sessionToken?: string): Promise<LiveQuerySubscription> {
14901491
const currentUser = await CoreManager.getUserController().currentUserAsync();
1491-
const sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
1492-
1492+
if (!sessionToken) {
1493+
sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
1494+
}
14931495
const liveQueryClient = await CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
14941496
if (liveQueryClient.shouldOpen()) {
14951497
liveQueryClient.open();

src/__tests__/ParseQuery-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,4 +2718,31 @@ describe('ParseQuery LocalDatastore', () => {
27182718
expect(subscription.sessionToken).toBeUndefined();
27192719
expect(subscription.query).toEqual(query);
27202720
});
2721+
2722+
it('can subscribe to query with sessionToken parameter', async () => {
2723+
const mockLiveQueryClient = {
2724+
shouldOpen: function() {
2725+
return true;
2726+
},
2727+
open: function() {},
2728+
subscribe: function(query, sessionToken) {
2729+
return new LiveQuerySubscription('0', query, sessionToken);
2730+
},
2731+
};
2732+
CoreManager.set('UserController', {
2733+
currentUserAsync() {
2734+
return Promise.resolve(null);
2735+
}
2736+
});
2737+
CoreManager.set('LiveQueryController', {
2738+
getDefaultLiveQueryClient() {
2739+
return Promise.resolve(mockLiveQueryClient);
2740+
}
2741+
});
2742+
const query = new ParseQuery('TestObject');
2743+
const subscription = await query.subscribe('r:test');
2744+
expect(subscription.id).toBe('0');
2745+
expect(subscription.sessionToken).toBe('r:test');
2746+
expect(subscription.query).toEqual(query);
2747+
});
27212748
});

0 commit comments

Comments
 (0)