|
1 | 1 | import events from 'events';
|
2 | 2 | import LiveQueryClient from './LiveQueryClient';
|
3 | 3 | import CoreManager from './CoreManager';
|
| 4 | +import ParsePromise from './ParsePromise'; |
4 | 5 |
|
5 | 6 | function open() {
|
6 | 7 | var LiveQueryController = CoreManager.getLiveQueryController();
|
@@ -69,82 +70,96 @@ LiveQuery.on('error', () => {
|
69 | 70 | export default LiveQuery;
|
70 | 71 |
|
71 | 72 | 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 | + }); |
78 | 80 | };
|
79 | 81 |
|
80 | 82 | let getLiveQueryClient = () => {
|
81 |
| - return CoreManager.getLiveQueryController().getDefaultLiveQueryClient(); |
| 83 | + return CoreManager.getLiveQueryController().getDefaultLiveQueryClient().then((defaultLiveQueryClient) => { |
| 84 | + return ParsePromise.as(defaultLiveQueryClient); |
| 85 | + }); |
82 | 86 | };
|
83 | 87 |
|
84 | 88 | let defaultLiveQueryClient;
|
85 | 89 | let DefaultLiveQueryController = {
|
86 | 90 | setDefaultLiveQueryClient(liveQueryClient: any) {
|
87 | 91 | defaultLiveQueryClient = liveQueryClient;
|
88 | 92 | },
|
89 |
| - getDefaultLiveQueryClient(): any { |
| 93 | + getDefaultLiveQueryClient(): ParsePromise { |
90 | 94 | if (defaultLiveQueryClient) {
|
91 |
| - return defaultLiveQueryClient; |
| 95 | + return ParsePromise.as(defaultLiveQueryClient); |
92 | 96 | }
|
93 | 97 |
|
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 | + } |
99 | 105 |
|
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 | + } |
106 | 112 |
|
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); |
127 | 136 | });
|
128 |
| - return defaultLiveQueryClient; |
129 | 137 | },
|
130 | 138 | open() {
|
131 |
| - let liveQueryClient = getLiveQueryClient(); |
132 |
| - liveQueryClient.open(); |
| 139 | + getLiveQueryClient().then((liveQueryClient) => { |
| 140 | + return ParsePromise.as(liveQueryClient.open()); |
| 141 | + }); |
133 | 142 | },
|
134 | 143 | close() {
|
135 |
| - let liveQueryClient = getLiveQueryClient(); |
136 |
| - liveQueryClient.close(); |
| 144 | + getLiveQueryClient().then((liveQueryClient) => { |
| 145 | + return ParsePromise.as(liveQueryClient.close()); |
| 146 | + }); |
137 | 147 | },
|
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 | + }); |
144 | 158 | },
|
145 | 159 | unsubscribe(subscription: any) {
|
146 |
| - let liveQueryClient = getLiveQueryClient(); |
147 |
| - return liveQueryClient.unsubscribe(subscription); |
| 160 | + getLiveQueryClient().then((liveQueryClient) => { |
| 161 | + return ParsePromise.as(liveQueryClient.unsubscribe(subscription)); |
| 162 | + }); |
148 | 163 | }
|
149 | 164 | };
|
150 | 165 |
|
|
0 commit comments