1
+ /**
2
+ * Copyright (c) 2015-present, Parse, LLC.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ * @flow
10
+ */
11
+
1
12
import EventEmitter from './EventEmitter' ;
2
13
import LiveQueryClient from './LiveQueryClient' ;
3
14
import CoreManager from './CoreManager' ;
4
15
import ParsePromise from './ParsePromise' ;
5
16
6
17
function open ( ) {
7
- var LiveQueryController = CoreManager . getLiveQueryController ( ) ;
18
+ const LiveQueryController = CoreManager . getLiveQueryController ( ) ;
8
19
LiveQueryController . open ( ) ;
9
20
}
10
21
11
22
function close ( ) {
12
- var LiveQueryController = CoreManager . getLiveQueryController ( ) ;
23
+ const LiveQueryController = CoreManager . getLiveQueryController ( ) ;
13
24
LiveQueryController . close ( ) ;
14
25
}
15
26
@@ -69,23 +80,19 @@ LiveQuery.on('error', () => {
69
80
70
81
export default LiveQuery ;
71
82
72
- let getSessionToken = ( ) => {
73
- let promiseUser = CoreManager . getUserController ( ) . currentUserAsync ( ) ;
74
- return promiseUser . then ( ( currentUser ) => {
75
- return ParsePromise . as ( currentUser ? currentUser . sessionToken : undefined ) ;
76
- } ) . then ( ( sessionToken ) => {
77
- return ParsePromise . as ( sessionToken ) ;
83
+ function getSessionToken ( ) {
84
+ const controller = CoreManager . getUserController ( ) ;
85
+ return controller . currentUserAsync ( ) . then ( ( currentUser ) => {
86
+ return currentUser ? currentUser . getSessionToken ( ) : undefined ;
78
87
} ) ;
79
- } ;
88
+ }
80
89
81
- let getLiveQueryClient = ( ) => {
82
- return CoreManager . getLiveQueryController ( ) . getDefaultLiveQueryClient ( ) . then ( ( defaultLiveQueryClient ) => {
83
- return ParsePromise . as ( defaultLiveQueryClient ) ;
84
- } ) ;
85
- } ;
90
+ function getLiveQueryClient ( ) {
91
+ return CoreManager . getLiveQueryController ( ) . getDefaultLiveQueryClient ( ) ;
92
+ }
86
93
87
94
let defaultLiveQueryClient ;
88
- let DefaultLiveQueryController = {
95
+ const DefaultLiveQueryController = {
89
96
setDefaultLiveQueryClient ( liveQueryClient : any ) {
90
97
defaultLiveQueryClient = liveQueryClient ;
91
98
} ,
@@ -94,12 +101,13 @@ let DefaultLiveQueryController = {
94
101
return ParsePromise . as ( defaultLiveQueryClient ) ;
95
102
}
96
103
97
- let sessionTokenPromise = getSessionToken ( ) ;
98
- return sessionTokenPromise . then ( ( sessionToken ) => {
104
+ return getSessionToken ( ) . then ( ( sessionToken ) => {
99
105
let liveQueryServerURL = CoreManager . get ( 'LIVEQUERY_SERVER_URL' ) ;
100
106
101
107
if ( liveQueryServerURL && liveQueryServerURL . indexOf ( 'ws' ) !== 0 ) {
102
- throw new Error ( 'You need to set a proper Parse LiveQuery server url before using LiveQueryClient' ) ;
108
+ throw new Error (
109
+ 'You need to set a proper Parse LiveQuery server url before using LiveQueryClient'
110
+ ) ;
103
111
}
104
112
105
113
// If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
@@ -115,9 +123,9 @@ let DefaultLiveQueryController = {
115
123
CoreManager . set ( 'LIVEQUERY_SERVER_URL' , liveQueryServerURL ) ;
116
124
}
117
125
118
- let applicationId = CoreManager . get ( 'APPLICATION_ID' ) ;
119
- let javascriptKey = CoreManager . get ( 'JAVASCRIPT_KEY' ) ;
120
- let masterKey = CoreManager . get ( 'MASTER_KEY' ) ;
126
+ const applicationId = CoreManager . get ( 'APPLICATION_ID' ) ;
127
+ const javascriptKey = CoreManager . get ( 'JAVASCRIPT_KEY' ) ;
128
+ const masterKey = CoreManager . get ( 'MASTER_KEY' ) ;
121
129
// Get currentUser sessionToken if possible
122
130
defaultLiveQueryClient = new LiveQueryClient ( {
123
131
applicationId,
@@ -138,7 +146,7 @@ let DefaultLiveQueryController = {
138
146
LiveQuery . emit ( 'close' ) ;
139
147
} ) ;
140
148
141
- return ParsePromise . as ( defaultLiveQueryClient ) ;
149
+ return defaultLiveQueryClient ;
142
150
} ) ;
143
151
} ,
144
152
open ( ) {
@@ -198,7 +206,10 @@ let DefaultLiveQueryController = {
198
206
getLiveQueryClient ( ) . then ( ( liveQueryClient ) => {
199
207
this . resolve ( liveQueryClient . unsubscribe ( subscription ) ) ;
200
208
} ) ;
201
- }
209
+ } ,
210
+ _clearCachedDefaultClient ( ) {
211
+ defaultLiveQueryClient = null ;
212
+ } ,
202
213
} ;
203
214
204
215
CoreManager . setLiveQueryController ( DefaultLiveQueryController ) ;
0 commit comments