Skip to content

Commit ceba319

Browse files
committed
Updating Parse.LiveQuery to use currentUserAsync.
1 parent 68ac2aa commit ceba319

File tree

3 files changed

+73
-57
lines changed

3 files changed

+73
-57
lines changed

src/LiveQuerySubscription.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ export default class Subscription extends events.EventEmitter {
102102
* @method unsubscribe
103103
*/
104104
unsubscribe() {
105-
var liveQueryClient = CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
106-
liveQueryClient.unsubscribe(this);
107-
this.emit('close');
105+
CoreManager.getLiveQueryController().getDefaultLiveQueryClient().then((liveQueryClient) => {
106+
liveQueryClient.unsubscribe(this);
107+
this.emit('close');
108+
});
108109
}
109110
}

src/ParseLiveQuery.js

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import events from 'events';
22
import LiveQueryClient from './LiveQueryClient';
33
import CoreManager from './CoreManager';
4+
import ParsePromise from './ParsePromise';
45

56
function open() {
67
var LiveQueryController = CoreManager.getLiveQueryController();
@@ -69,82 +70,96 @@ LiveQuery.on('error', () => {
6970
export default LiveQuery;
7071

7172
let getSessionToken = () => {
72-
let currentUser = CoreManager.getUserController().currentUser();
73-
let sessionToken;
74-
if (currentUser) {
75-
sessionToken = currentUser.getSessionToken();
76-
}
77-
return sessionToken;
73+
let promiseUser = CoreManager.getUserController().currentUserAsync();
74+
let promiseSessionToken = promiseUser.then((currentUser) => {
75+
return ParsePromise.as(currentUser ? currentUser.sessionToken : null);
76+
});
77+
return promiseSessionToken.then((sessionToken) => {
78+
return ParsePromise.as(sessionToken);
79+
});
7880
};
7981

8082
let getLiveQueryClient = () => {
81-
return CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
83+
return CoreManager.getLiveQueryController().getDefaultLiveQueryClient().then((defaultLiveQueryClient) => {
84+
return ParsePromise.as(defaultLiveQueryClient);
85+
});
8286
};
8387

8488
let defaultLiveQueryClient;
8589
let DefaultLiveQueryController = {
8690
setDefaultLiveQueryClient(liveQueryClient: any) {
8791
defaultLiveQueryClient = liveQueryClient;
8892
},
89-
getDefaultLiveQueryClient(): any {
93+
getDefaultLiveQueryClient(): ParsePromise {
9094
if (defaultLiveQueryClient) {
91-
return defaultLiveQueryClient;
95+
return ParsePromise.as(defaultLiveQueryClient);
9296
}
9397

94-
let liveQueryServerURL = CoreManager.get('LIVEQUERY_SERVER_URL');
95-
96-
if (liveQueryServerURL && liveQueryServerURL.indexOf('ws') !== 0) {
97-
throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
98-
}
98+
let sessionTokenPromise = getSessionToken();
99+
return sessionTokenPromise.then((sessionToken) => {
100+
let liveQueryServerURL = CoreManager.get('LIVEQUERY_SERVER_URL');
101+
102+
if (liveQueryServerURL && liveQueryServerURL.indexOf('ws') !== 0) {
103+
throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
104+
}
99105

100-
// If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
101-
if (!liveQueryServerURL) {
102-
let host = CoreManager.get('SERVER_URL').replace(/^https?:\/\//, '');
103-
liveQueryServerURL = 'ws://' + host;
104-
CoreManager.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
105-
}
106+
// If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
107+
if (!liveQueryServerURL) {
108+
let host = CoreManager.get('SERVER_URL').replace(/^https?:\/\//, '');
109+
liveQueryServerURL = 'ws://' + host;
110+
CoreManager.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
111+
}
106112

107-
let applicationId = CoreManager.get('APPLICATION_ID');
108-
let javascriptKey = CoreManager.get('JAVASCRIPT_KEY');
109-
let masterKey = CoreManager.get('MASTER_KEY');
110-
// Get currentUser sessionToken if possible
111-
defaultLiveQueryClient = new LiveQueryClient({
112-
applicationId,
113-
serverURL: liveQueryServerURL,
114-
javascriptKey,
115-
masterKey,
116-
sessionToken: getSessionToken(),
117-
});
118-
// Register a default onError callback to make sure we do not crash on error
119-
defaultLiveQueryClient.on('error', (error) => {
120-
LiveQuery.emit('error', error);
121-
});
122-
defaultLiveQueryClient.on('open', () => {
123-
LiveQuery.emit('open');
124-
});
125-
defaultLiveQueryClient.on('close', () => {
126-
LiveQuery.emit('close');
113+
let applicationId = CoreManager.get('APPLICATION_ID');
114+
let javascriptKey = CoreManager.get('JAVASCRIPT_KEY');
115+
let masterKey = CoreManager.get('MASTER_KEY');
116+
// Get currentUser sessionToken if possible
117+
defaultLiveQueryClient = new LiveQueryClient({
118+
applicationId,
119+
serverURL: liveQueryServerURL,
120+
javascriptKey,
121+
masterKey,
122+
sessionToken,
123+
});
124+
// Register a default onError callback to make sure we do not crash on error
125+
defaultLiveQueryClient.on('error', (error) => {
126+
LiveQuery.emit('error', error);
127+
});
128+
defaultLiveQueryClient.on('open', () => {
129+
LiveQuery.emit('open');
130+
});
131+
defaultLiveQueryClient.on('close', () => {
132+
LiveQuery.emit('close');
133+
});
134+
135+
return ParsePromise.as(defaultLiveQueryClient);
127136
});
128-
return defaultLiveQueryClient;
129137
},
130138
open() {
131-
let liveQueryClient = getLiveQueryClient();
132-
liveQueryClient.open();
139+
getLiveQueryClient().then((liveQueryClient) => {
140+
return ParsePromise.as(liveQueryClient.open());
141+
});
133142
},
134143
close() {
135-
let liveQueryClient = getLiveQueryClient();
136-
liveQueryClient.close();
144+
getLiveQueryClient().then((liveQueryClient) => {
145+
return ParsePromise.as(liveQueryClient.close());
146+
});
137147
},
138-
subscribe(query: any): any {
139-
let liveQueryClient = getLiveQueryClient();
140-
if (liveQueryClient.shouldOpen()) {
141-
liveQueryClient.open();
142-
}
143-
return liveQueryClient.subscribe(query, getSessionToken());
148+
subscribe(query: any): ParsePromise {
149+
return getLiveQueryClient().then((liveQueryClient) => {
150+
if (liveQueryClient.shouldOpen()) {
151+
liveQueryClient.open();
152+
}
153+
let promiseSessionToken = getSessionToken();
154+
return promiseSessionToken.then((sessionToken) => {
155+
return liveQueryClient.subscribe(query, sessionToken);
156+
});
157+
});
144158
},
145159
unsubscribe(subscription: any) {
146-
let liveQueryClient = getLiveQueryClient();
147-
return liveQueryClient.unsubscribe(subscription);
160+
getLiveQueryClient().then((liveQueryClient) => {
161+
return ParsePromise.as(liveQueryClient.unsubscribe(subscription));
162+
});
148163
}
149164
};
150165

src/ParseQuery.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ export default class ParseQuery {
990990
/**
991991
* Subscribe this query to get liveQuery updates
992992
* @method subscribe
993-
* @return {LiveQuerySubscription} Returns the liveQuerySubscription, it's an event emitter
993+
* @return {Parse.Promise} A promise that is resolved with a liveQuerySubscription
994994
* which can be used to get liveQuery updates.
995995
*/
996996
subscribe(): any {

0 commit comments

Comments
 (0)